Category Archives: Webparts

Auto Hide WebPart – How to

Next time I want my webpart to hide itself I won’t use:

this.hidden = true;

because that will result in:

System.InvalidOperationException: The Visible property cannot be set on Web Part XXXXXXX

Instead, I will do the following:

SPFile myPage= SPContext.Current.File;
SPLimitedWebPartManager limitedWebPartManager;

   //Get the page's limited webpart manager
   using (limitedWebPartManager = myPage.GetLimitedWebPartManager(PersonalizationScope.Shared))
      System.Collections.Generic.List configuratorWebParts = limitedWebPartManager.WebParts.Cast().Where(webpart => webpart.Title == "TITLE OF MY WEBPART").ToList();

     if (configuratorWebParts.Count > 0)
       foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in configuratorWebParts)
           webPart.Hidden = true;


and voila!

Configure a native webpart using another webpart

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:

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 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.