How To Programmatically Change Content Search Web Part QueryText

23 Apr

The other day I needed to programmatically change the QueryText attribute for my custom Content Search Web Part.

I came across this nice post from David Rei that saved me a lot of time.

Here’s the code I used to programmatically add a language choice to my query.

        protected override void OnLoad(EventArgs e)
        {
            if (this.AppManager != null)
            {
                if (this.AppManager.QueryGroups.ContainsKey(this.QueryGroupName) &&
                    this.AppManager.QueryGroups[this.QueryGroupName].DataProvider != null)
                {
                    this.AppManager.QueryGroups[this.QueryGroupName].DataProvider.BeforeSerializeToClient +=
                        new BeforeSerializeToClientEventHandler(EnhanceQuery);
                }
            }

            base.OnLoad(e);
        }

        private void EnhanceQuery(object sender, BeforeSerializeToClientEventArgs e)
        {
            DataProviderScriptWebPart dataProvider = sender as DataProviderScriptWebPart;
            string currentQueryText = dataProvider.QueryTemplate;
            string token = "{LANGUAGE}";
            if (currentQueryText.Contains(token))
            {
                dataProvider.QueryTemplate = currentQueryText.Replace(token, GetCurrentLanguage());
            }
        }

Hope this helps,

Cheers

Advertisements

7 Responses to “How To Programmatically Change Content Search Web Part QueryText”

  1. Adam Yoder May 29, 2013 at 10:32 #

    I’m trying this same approach, but I’m getting the following build error. Did you run into this when you were trying to derive your webpart from the ContentBySearchWebPart class?

    Error 1 The base class or interface ‘Microsoft.SharePoint.WebPartPages.WebPart’ in assembly ‘Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ referenced by type ‘Microsoft.Office.Server.Search.WebControls.ScriptWebPart’ could not be resolved c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.Office.Server.Search.dll SearchContentWebpart

  2. Rafael R. Mascarenhas May 29, 2013 at 10:42 #

    I haven’t encounter this error but it’s odd that it says Microsoft.SharePoint.WebPartPages.WebPart because when creating a custom WebPart you should inherit from System.Web.UI.WebControls.WebParts.WebPart but in this case your class should be inheriting from Microsoft.Office.Server.Search.WebControls.ContentBySearchWebPart

    Go to the link in my post and check the source code.

    Cheers.

    • Adam Yoder May 29, 2013 at 10:49 #

      I am inheriting from that class though.. that’s what is odd. Here is my full class. Do you see what I’m missing?

      using System;
      using System.ComponentModel;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using Microsoft.SharePoint;
      using Microsoft.SharePoint.WebControls;
      using Microsoft.Office.Server.Search;
      using Microsoft.Office.Server.Search.WebControls;

      namespace SearchContentWebpart.SearchContentWebpart
      {
      [ToolboxItemAttribute(false)]

      public class SearchContentWebpart : Microsoft.Office.Server.Search.WebControls.ContentBySearchWebPart
      {

      protected override void OnLoad(EventArgs e)
      {
      if (this.AppManager != null)
      {
      if (this.AppManager.QueryGroups.ContainsKey(this.QueryGroupName) &&
      this.AppManager.QueryGroups[this.QueryGroupName].DataProvider != null)
      {
      this.AppManager.QueryGroups[this.QueryGroupName].DataProvider.BeforeSerializeToClient +=
      new BeforeSerializeToClientEventHandler(EnhanceQuery);
      }
      }

      base.OnLoad(e);
      }

      private void EnhanceQuery(object sender, BeforeSerializeToClientEventArgs e)
      {
      DataProviderScriptWebPart dataProvider = sender as DataProviderScriptWebPart;
      dataProvider.QueryTemplate = “Multitest1:Finance”;
      }
      }
      }

      • Rafael R. Mascarenhas May 29, 2013 at 12:29 #

        Have you checked in your file system if that DLL is there or not? (The one in the error)

      • Adam Yoder May 29, 2013 at 13:01 #

        I figured it out.. I had the solution as a Sandboxed solution and once I changed it to Farm, it worked. Thanks for your help.

  3. James d February 5, 2015 at 03:53 #

    What about paging in anonymous mode?

    This solution doesn’t work if you set a control display template to “Control_ListWithPaging”.

    At first, it is loaded correctly but while clicking on next button, the query added is being ignored(only in anonymous mode).

Trackbacks/Pingbacks

  1. Date Formats in SharePoint 2013 Advanced Search | Ketul's Weblog - November 21, 2014

    […] not really work out which if construct was actually invoked to return 1033, anyways thanks to the Blog Post about changing language token in Content Search WebPart, I learned about DataProviderScriptWebPart and I realized that FallbackLanguage (which is the first […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: