Have you ever found a scenario where you would like to edit the configurations of native webpart but don’t want to mess it by creating a custom one? In this case you should consider the use of second webpart to do all the messy configurations.

The following example shows how you you can create a custom webpart that modifies the search query string of a content search web part that exists in the same page.

Setup: Add a visual web part to your SharePoint project and add the Newton-King Json.NET reference to your project

  1. On Visual Studio, open your project solution, right click your project folder and select “Add Item “
  2. In the Add New Item menu select “Office/SharePoint” ->  VisualWebPart, give a name ex: “WebPartConfigurator”
  3. Add James Newton-King Json.NET .dll to your project.  This will make it extremely easy to edit the search query once you get it from the target webpart. (you can find it here: http://james.newtonking.com/json)

Code: Follow 3 steps -> Get the the LimitedWebPartManager, get the target content search  webpart, configure the  query string in the target content search webpart.

1. Add the following to your class header:

using System.Linq; //For accessing the web part
using Newtonsoft.Json.Linq; // For editing the webparts search query (Json). Don't forget to add a reference to this lib on your project.

2. Uncomment PageLoad method and add the following code:

//Get the page
SPFile membersPage = SPContext.Current.File;
SPLimitedWebPartManager limitedWebpartManager;

//Get the page's Limited Web Part Manager using (limitedWebPartManager = membersPage.GetLimitedWebPartManager(PersonalizationScope.Shared)) {                     ////Gets all the the webpart with "YOUR_WEB_PART_NAME_HERE" name if they exist                     System.Collections.Generic.List<System.Web.UI.WebControls.WebParts.WebPart> searchWebParts = limitedWebPartManager.WebParts.Cast<System.Web.UI.WebControls.WebParts.WebPart>().Where(webpart => webpart.Title == "YOUR_WEB_PART_NAME").ToList();                      ////If any web part is found runs the configuration method "ConfigureWebpart" on it                     if (membersWebParts.Count > 0)                     {                         foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in membersWebParts)                         {                             ////Run the configuration method on your target webpart ConfigureWebPart(webPart); ////Save all the changes made to your target webpart!!!!!!!                             limitedWebPartManager.SaveChanges(webPart);                         }                                                  } } ////Updates all the changes made to the page elements membersPage.Update();

3. Add your ConfigureWebPart method. In this case we will change the query string using Newtons-King Json.net lib.

private void ConfigureMembersWebPart(System.Web.UI.WebControls.WebParts.WebPart webPart)
{
            ////Cast the webpart to a ContentSearchWebPart (ResultScriptWebPart in .Net)
            ResultScriptWebPart membersWebPart = (ResultScriptWebPart)webPart;
            
            ////Get the query object
            JObject query = JObject.Parse(membersWebPart.DataProviderJSON);
 
            ////Edit the query object
            query.Property("QueryTemplate").Value = "YOUR_QUERY_HERE";
 
            ////Set the webpart's query object to the newly edited object.
            membersWebPart.DataProviderJSON = query.ToString();
}

Finishing: Add the custom web part to the page of the target webpart.

LEAVE A REPLY

Please enter your comment!
Please enter your name here