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.

Adding/Removing a web part

If a SharePoint website is created with a site definition and now it is required to add/remove a web part in that SharePoint website, then first of all we need to modify the site definition to add/remove the respective web part, but this will not add/remove the respective 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 respective web part added/removed. 

There are two approaches to add/remove a web part in an existing SharePoint website: Manual Approach, and By Using C# Code.

1. Manual Approach

A web part can be added/ removed from a SharePoint website by using following steps:

Adding a web part

· Locate the web page in which a web part is to be added

· Go to Site Actions->Edit Page to open Edit mode of that page

· Locate the web part zone in which the web part is to added

· Click on “Add a Web Part” button in the respective web part zone. A list of web parts will be displayed in a new window

· Locate and select the web part which is required to be added

· Click the “Add” button. The selected web part will be added in the respective web part zone

· Click on “Exit Edit Mode” to open the normal view of that page

Removing a web part

· Locate the web page from where the respective web part is to be removed.

· Go to Site Actions->Edit Page to open Edit mode of that page

· Locate the web part to be removed

· Go to Edit->Delete on that web part

· Clicking on “Delete” will remove the respective web part from this web page

· 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 add/remove 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

Adding a web part

Following code snippet can be used to add 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);
      
  
    
//create new webpart object
           
    
WebPartToBeAdded wpNew = new WebPartToBeAdded();
      

    
//set properties of new webpart object
     
    
wpNew.ZoneID = "WebPartZoneIDWhereWebPartIsToBeAdded";
     
    
wpNew.Title = "Web Part Title";
     
    
wpNew.ChromeState = System.Web.UI.WebControls.WebParts.PartChromeState.Normal;
     
    
wpNew.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
      

    
//add new webpart object to webparts collection
     
    
webparts.Add(wpNew);
      

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

Removing a web part

Following code snippet can be used to remove a web part from 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 which is to be removed
       if (wp.Title == "TitleOfWebPartWhichIsToBeRemoved")
       {
         //delete webpart
         webparts.Delete(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 add/remove a web part.