Adding Web Part to SharePoint Page using CSOM

In this article, you will learn how to add web part to a page programmatically.

The web parts can be appended to a page using the webpart xml content. Each and every webpart present on SharePoint is represented by xml content. This can be witnessed by exporting the webpart from any page. In this article, you will learn how to add the webpart using CSOM approach.

The following denotes a content editor webpart xml content.

XML:
  1. "<?xml version="1.0" encoding="utf-8"?>  
  2. <WebPart xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/WebPart/v2">  
  3. <Title>MyInfo</Title>  
  4. <FrameType>None</FrameType>  
  5. <Description>Allows authors to enter rich text content.</Description>  
  6. <IsIncluded>true</IsIncluded>  
  7. <ZoneID>BottomZone</ZoneID>  
  8. <PartOrder>0</PartOrder>  
  9. <FrameState>Normal</FrameState>  
  10. <Height />  
  11. <Width />  
  12. <AllowRemove>true</AllowRemove>  
  13. <AllowZoneChange>true</AllowZoneChange>  
  14. <AllowMinimize>true</AllowMinimize>  
  15. <AllowConnect>true</AllowConnect>  
  16. <AllowEdit>true</AllowEdit>  
  17. <AllowHide>true</AllowHide>  
  18. <IsVisible>true</IsVisible>  
  19. <DetailLink />  
  20. <HelpLink />  
  21. <HelpMode>Modeless</HelpMode>  
  22. <Dir>Default</Dir>  
  23. <PartImageSmall />  
  24. <MissingAssembly>Cannot import this Web Part.</MissingAssembly>  
  25. <PartImageLarge>/_layouts/15/images/mscontl.gif</PartImageLarge>  
  26. <IsIncludedFilter />  
  27. <Assembly>Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>  
  28. <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>  
  29. <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">/Style Library/Txt/MyInfo.txt</ContentLink>  
  30. <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />  
  31. <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />  
  32. </WebPart>  
The following steps shown the implementation part.
  • Using client context, try getting the page details using server relative url.
  • If necessary, check out the file.
  • With GetLimitedWebPartManager method, get the webpart details on the page.
  • Check if the webpart already exists
  • If not present, import the webpart xml (similar to the one shown above) and add it to the page

The following code snippet shows the CSOM code for the above implementation approach.

CODE:
  1. var page = webpartContext.Web.GetFileByServerRelativeUrl("/sites/SPMySite/Person.aspx");  
  2. webpartContext.Load(page);  
  3. webpartContext.ExecuteQuery();  
  4. if (page.CheckOutType != CheckOutType.Online)  
  5. {  
  6.     // Check out  
  7.     page.CheckOut();  
  8. }  
  9. // Gets the webparts available on the page  
  10. var wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared);  
  11. webpartContext.Load(wpm.WebParts,  
  12.     wps => wps.Include(wp => wp.WebPart.Title));  
  13. webpartContext.ExecuteQuery();  
  14. var availableWebparts = wpm.WebParts;  
  15. // Check if the current webpart already exists.  
  16. var filteredWebParts = from isWPAvail in availableWebparts  
  17. where isWPAvail.WebPart.Title == Title  
  18. select isWPAvail;  
  19. if (filteredWebParts.Count() <= 0)  
  20. {  
  21.     // Import the webpart xml  
  22.     var importedWebPart = wpm.ImportWebPart(wpXML); //wpXML denotes the webpart xml file.  
  23.     // Add it to page  
  24.     var webPart = wpm.AddWebPart(importedWebPart.WebPart, Zone, 8);  
  25.     webpartContext.ExecuteQuery();  
  26. }  
The webpart is successfully added to Person.aspx page. Try opening the page and check the webpart added. Any webpart type can be added to the page using the above approach. The appropriate webpart xml should be generated by the developer before adding it to the page.
Ebook Download
View all
Learn
View all