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:
- "<?xml version="1.0" encoding="utf-8"?>
- <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">
- <Title>MyInfo</Title>
- <FrameType>None</FrameType>
- <Description>Allows authors to enter rich text content.</Description>
- <IsIncluded>true</IsIncluded>
- <ZoneID>BottomZone</ZoneID>
- <PartOrder>0</PartOrder>
- <FrameState>Normal</FrameState>
- <Height />
- <Width />
- <AllowRemove>true</AllowRemove>
- <AllowZoneChange>true</AllowZoneChange>
- <AllowMinimize>true</AllowMinimize>
- <AllowConnect>true</AllowConnect>
- <AllowEdit>true</AllowEdit>
- <AllowHide>true</AllowHide>
- <IsVisible>true</IsVisible>
- <DetailLink />
- <HelpLink />
- <HelpMode>Modeless</HelpMode>
- <Dir>Default</Dir>
- <PartImageSmall />
- <MissingAssembly>Cannot import this Web Part.</MissingAssembly>
- <PartImageLarge>/_layouts/15/images/mscontl.gif</PartImageLarge>
- <IsIncludedFilter />
- <Assembly>Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
- <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
- <ContentLink xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">/Style Library/Txt/MyInfo.txt</ContentLink>
- <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
- <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
- </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:
- var page = webpartContext.Web.GetFileByServerRelativeUrl("/sites/SPMySite/Person.aspx");
- webpartContext.Load(page);
- webpartContext.ExecuteQuery();
- if (page.CheckOutType != CheckOutType.Online)
- {
-
- page.CheckOut();
- }
-
- var wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
- webpartContext.Load(wpm.WebParts,
- wps => wps.Include(wp => wp.WebPart.Title));
- webpartContext.ExecuteQuery();
- var availableWebparts = wpm.WebParts;
-
- var filteredWebParts = from isWPAvail in availableWebparts
- where isWPAvail.WebPart.Title == Title
- select isWPAvail;
- if (filteredWebParts.Count() <= 0)
- {
-
- var importedWebPart = wpm.ImportWebPart(wpXML);
-
- var webPart = wpm.AddWebPart(importedWebPart.WebPart, Zone, 8);
- webpartContext.ExecuteQuery();
- }
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.