How to update the web part properties at the runtime in SharePoint 2010?

1) SharePoint 2010 page have the SPLimitedWEbPartManager which manages the web part instances in particular page.

2) You have get the instance of the SPLimitedWebpart manager from a page and traverse all the web part.

3) Find the web part, In my case I will looking for xsltListViewWebPart.

3) You can use indexer also to find the web part.

4) Change the property value and Save the updated web part in the Web part manager instance.

5) Please don’t forget to update the SPWeb as well. Following is the code:-

Code Snippet
  1. SPWeb blogweb = properties.Feature.Parent as SPWeb;
  2.             string Page = blogweb.Url + "/default.aspx";
  3.             SPLimitedWebPartManager blogWPM = blogweb.GetLimitedWebPartManager(Page, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
  4.             SPLimitedWebPartCollection webparts = blogWPM.WebParts;
  5.             foreach (Microsoft.SharePoint.WebPartPages.WebPart webpart in webparts)
  6.             {
  7.  
  8.                 if (webpart.GetType() == typeof(Microsoft.SharePoint.WebPartPages.XsltListViewWebPart))
  9.                 {
  10.                     XsltListViewWebPart lstWebPart = webpart as XsltListViewWebPart;
  11.                     lstWebPart.XslLink = "blog.xsl";
  12.                     blogWPM.SaveChanges(webpart);
  13.                     blogweb.Update();
  14.                 }
  15.             }

About Navdeep Madan

Working as a sharepoint, web solution consultant
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

5 Responses to How to update the web part properties at the runtime in SharePoint 2010?

  1. Styro George says:

    Pretty nice post. I just stumbled upon your weblog and wanted to say that I’ve truly enjoyed browsing your blog posts. After all I will be subscribing to your feed and I hope you write again very soon!

  2. Cameron Seibel says:

    You can use the web part StorageKey property of the web part (as long as you inherit from Microsoft.SharePoint.WebPartPages.WebPart) so you don’t have to traverse all the web parts on the page.

    The code can look like
    XsltListViewWebPart lstWebPart = (XsltListViewWebPart)blogWPM.WebParts[StorageKey];

    This also helps if there are two of the same type of web parts on the same page. You can also use the web part id.

  3. thanks for information Cameron. but what will be the value of storage key. if you post the code with example that will very helpful.

    • Cameron Seibel says:

      The value of the storage key is the Guid that the SharePoint database saves all the property information for your web part.

      Sorry for formatting. I’m new to this blog stuff.

      public class MyWebPart Microsoft.SharePoint.WebPartPages.WebPart : IWebEditable //For a custom EditorPart
      {

      //Gets the StorageKey
      public string StorageId
      {
      get{return this.StorageKey.toString();
      }

      }

      public class MyEditorPart: EditorPart
      {

      public MyWebPart ParentPart
      {
      get{ return (MyWebPart)this.WebPartToEdit;
      }

      public MyEditorPart() { }

      }

      public class SomeOtherClass
      {
      public static void DoStuffToWebPart(string webUrl, string StorageId)
      {
      using(SPWeb web = new SPSite(webUrl).OpenWeb()){

      using (SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(PublishingWeb.IsPublishingWeb(web) ? PublishingWeb.GetPublishingWeb(web).DefaultPage.Url : web.ServerRelativeUrl + “default.aspx”, System.Web.UI.WebControls.WebParts.Personalization.Shared))
      {
      web.AllowUnsafeUpdates = true;

      System.Web.UI.WebControls.WebParts.WebPart part = SPLimitedWebPartCollection[new Guid(StorageId)];

      ((MyWebPart)part).MyProperty=SomeProperty;

      manager.SaveChanges(part);
      }
      }
      }
      }

Leave a comment