Each SharePoint site is created based on a specific site definition which contains the description of default content and configuration of that site, like the description of all the web pages, custom lists, document libraries, etc to be added by default when the site is created. Site definition also contains a list of web parts with default property values for each web page and these web parts will be added in the respective web pages at the time of site creation

If a SharePoint website is created and it is required to change the property values of a specific web part (e.g. title, width, height, etc. of web part)  in that SharePoint website, then first of all we need to modify the side definition to change the property value of the respective web part, but this will not change the property value of that web part in the already created SharePoint website, while this will be done to ensure that all the newly created SharePoint websites with this site definition must have the new property value for that web part.

There are two approaches to change the property value of a web part in an existing SharePoint website: Manual Approach, and By Using C# Code

1. Manual Approach

  • Locate the web page which contains the web part whose property value is to be changed
  • Go to "Site Actions->Edit Page" to open Edit mode of that page
  • Locate the web part whose property value is to be changed
  • Go to "Edit->Modify Shared Web Part" on that web part, an editor part will open in which all the properties with values will be listed for that respective web part
  • Locate the property and change its value
  • Click on “Apply” button to save the changes
  • Click on “Exit Edit Mode” to open the normal view of that page

A single SharePoint application may contains hundreds or thousands or even more websites and if it is required to change the property values of a specific web part in all the existing websites then this manual approach is not feasible. In that case this should be done through coding.

2. By Using C# Code

Following code snippet can be used to change the property values of a web part in an existing SharePoint website.

using (SPSite spSiteTest = new SPSite(“SiteURL”)
{
  using (SPWeb spWebTest = spSiteTest.OpenWeb())
  {
    SPWebPartCollection webparts = spWebTest.GetWebPartCollection("WebPageURL", Storage.Shared);
   
    for (int k = 0; k < webparts.Count; k++)
    {
      //get reference to webpart
      Microsoft.SharePoint.WebPartPages.WebPart wp = webparts[k];

      //check webpart Title to find webpart whose value is to be changed
      if (wp.Title == "TitleOfWebPartWhoseValueIsToBeChanged")
      {
        //set new property values of the webpart object
        wpNew.Width = "600"; wpNew.Title = "New Web Part Title";

        //Call SaveChanges method of SPWebPartCollection object
        webparts.SaveChanges(wp.StorageKey); 

        //update spWeb object 
        spWebTest.Update(); 
      } 
    } 
  } 
}

So, just loop through all the websites in a SharePoint application and use the above code snippet to change the property values of a specific web part.