Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting – HostForLIFE :: Add, Update And Remove Web Part Using CSOM
Introduction
Web parts are the building blocks of the pages by which you can modify the content, appearance and behavior of pages of a SharePoint site.
In this blog we are going to discuss how to Add, Update and Remove the web part using csom. Here I have used Content Editor Web Part in Wiki page.
Adding the web part in a wikipage
Step 1
First set the credentials by providing the user credentials.
Step 2
Using the client context, load the web.
Step 3
Get the object of the file using server relative url of the wiki page in which you want to add the web part. Example (/sites/Team Site/Site Pages/wikiPage.aspx).
Step 4
Get the ‘limitedWebPartManager‘ by using object file.
Step 5
Design the Xml string for importing the web part as definition of web part in ‘limitedWebPartManager’. In xml I have added the Title of web part as ‘My Web Part’.
Step 6
Then add the web part order and left zone of the page.
Follow the below code snippet,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<span class="keyword">using</span> System; <span class="keyword">using</span> Microsoft.SharePoint.Client; <span class="keyword">using</span> Microsoft.SharePoint.Client.WebParts; <span class="keyword">using</span> System.Security; <span class="keyword">namespace</span> webpartadd { <span class="keyword">class</span> Program { <span class="keyword">static</span> <span class="keyword">void</span> Main(<span class="keyword">string</span>[] args) { <span class="keyword">string</span> userName = <span class="string">"userName@tenantName.onmicrosoft.com"</span>; <span class="keyword">string</span> password = <span class="string">"PassWord"</span>; <span class="keyword">string</span> siteUrl = <span class="string">"https://tenantName.sharepoint.com/sites/TeamSite"</span>; SecureString securePassword = <span class="keyword">new</span> SecureString(); <span class="keyword">foreach</span>(<span class="keyword">char</span> c <span class="keyword">in</span> password) { securePassword.AppendChar(c); } var credentials = <span class="keyword">new</span> SharePointOnlineCredentials(userName, securePassword); <span class="keyword">using</span>(ClientContext clientContext = <span class="keyword">new</span> ClientContext(siteUrl)) { <span class="keyword">try</span> { clientContext.Credentials = credentials; clientContext.Load(clientContext.Web); clientContext.ExecuteQuery(); File oFile = clientContext.Web.GetFileByServerRelativeUrl(<span class="string">"/sites/TeamSite/SitePages/wikiPage.aspx"</span>); LimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared); <span class="keyword">string</span> xmlWebPart = <span class="string">"<?xml version=\"1.0\" encoding=\"utf-8\"?>"</span> + <span class="string">"<WebPart xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""</span> + <span class="string">" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""</span> + <span class="string">" xmlns=\"http://schemas.microsoft.com/WebPart/v2\">"</span> + <span class="string">"<Title>My Web Part</Title><FrameType>Default</FrameType>"</span> + <span class="string">"<Description>Use for formatted text, tables, and images.</Description>"</span> + <span class="string">"<IsIncluded>true</IsIncluded><ZoneID></ZoneID><PartOrder>0</PartOrder>"</span> + <span class="string">"<FrameState>Normal</FrameState><Height /><Width /><AllowRemove>true</AllowRemove>"</span> + <span class="string">"<AllowZoneChange>true</AllowZoneChange><AllowMinimize>true</AllowMinimize>"</span> + <span class="string">"<AllowConnect>true</AllowConnect><AllowEdit>true</AllowEdit>"</span> + <span class="string">"<AllowHide>true</AllowHide><IsVisible>true</IsVisible><DetailLink /><HelpLink />"</span> + <span class="string">"<HelpMode>Modeless</HelpMode><Dir>Default</Dir><PartImageSmall />"</span> + <span class="string">"<MissingAssembly>Cannot import this Web Part.</MissingAssembly>"</span> + <span class="string">"<PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge><IsIncludedFilter />"</span> + <span class="string">"<Assembly>Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, "</span> + <span class="string">"PublicKeyToken=94de0004b6e3fcc5</Assembly>"</span> + <span class="string">"<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>"</span> + <span class="string">"<ContentLink xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" />"</span> + <span class="string">"<Content xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\">"</span> + <span class="string">"<![CDATA[This is a first paragraph!<DIV> </DIV>And this is a second paragraph.]]></Content>"</span> + <span class="string">"<PartStorage xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" /></WebPart>"</span>; WebPartDefinition oWebPartDefinition = limitedWebPartManager.ImportWebPart(xmlWebPart); limitedWebPartManager.AddWebPart(oWebPartDefinition.WebPart, <span class="string">"Left"</span>, 1); clientContext.ExecuteQuery(); } <span class="keyword">catch</span> (Exception e) { Console.WriteLine(e); } } Console.WriteLine(<span class="string">"Succcesfully added webpart"</span>); } } } |
In the below image webpat is added in a wikipage,
Updating the web part in a wikipage
Step 1
First set the credentials by providing the user credentials.
Step 2
Using the client context, load the web.
Step 3
Get the object of the file using the server relative URL of the wiki page in which you want to add the web part. Example (/sites/team Site/Site Pages/wikiPage.aspx).
Step 4
Get the ‘limitedWebPartManager‘ by using object file.
Step 5
Load the web part using LINQ query expression to return only the title of each Web Part.
Step 6
Check the page having at least one web part for updating.
Step 7
Get the web part definition by using the order value.
Step 8
Change the web part Title then save the web part by calling ‘SaveWebPartChanges()‘.
Follow the below code snippet,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<span class="keyword">using</span> System; <span class="keyword">using</span> Microsoft.SharePoint.Client; <span class="keyword">using</span> Microsoft.SharePoint.Client.WebParts; <span class="keyword">using</span> System.Security; <span class="keyword">namespace</span> UpdateWebpart { <span class="keyword">class</span> Program { <span class="keyword">static</span> <span class="keyword">void</span> Main(<span class="keyword">string</span>[] args) { <span class="keyword">string</span> userName = <span class="string">"userName@tenantName.onmicrosoft.com"</span>; <span class="keyword">string</span> password = <span class="string">"PassWord"</span>; <span class="keyword">string</span> siteUrl = <span class="string">"https://tenantName.sharepoint.com/sites/TeamSite"</span>; SecureString securePassword = <span class="keyword">new</span> SecureString(); <span class="keyword">foreach</span>(<span class="keyword">char</span> c <span class="keyword">in</span> password) { securePassword.AppendChar(c); } var cred = <span class="keyword">new</span> SharePointOnlineCredentials(userName, securePassword); <span class="keyword">using</span>(ClientContext clientContext = <span class="keyword">new</span> ClientContext(siteUrl)) { <span class="keyword">try</span> { clientContext.Credentials = cred; clientContext.Load(clientContext.Web); clientContext.ExecuteQuery(); File oFile = clientContext.Web.GetFileByServerRelativeUrl(<span class="string">"/sites/TeamSite/SitePages/wikiPage.aspx"</span>); LimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared); clientContext.Load(limitedWebPartManager.WebParts, wps => wps.Include(wp => wp.WebPart.Title)); clientContext.ExecuteQuery(); <span class="keyword">if</span> (limitedWebPartManager.WebParts.Count == 0) { <span class="keyword">throw</span> <span class="keyword">new</span> Exception(<span class="string">"No Web Parts on this page."</span>); } WebPartDefinition oWebPartDef = limitedWebPartManager.WebParts[0]; WebPart oWebPart = oWebPartDef.WebPart; oWebPart.Title = <span class="string">"New WebPart Title"</span>; oWebPartDef.SaveWebPartChanges(); clientContext.ExecuteQuery(); } <span class="keyword">catch</span> (Exception e) { Console.WriteLine(e); } } Console.WriteLine(<span class="string">"Succcesfully updated webpart"</span>); } } } |
In the below image web pat title is update in a wikipage,
Removing the web part from a wikipage
Step 1
First set the credentials by providing the user credentials.
Step 2
Using the client context and, load the web.
Step 3
Get the object of the file using server relative URL of the wiki page in which you want to add the web part. Example (/sites/team Site/Site Pages/wikiPage.aspx).
Step 4
Get the ‘limitedWebPartManager‘ by using object file.
Step 5
Load the web part.
Step 6
Check the page having at least one web part for deleting.
Step 7
Get the web part definition by using the order value.
Step 8
Delete the web part by calling ‘DeleteWebPart ()‘.
Follow the below code snippet,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
using System; using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.WebParts; using System.Security; namespace RemoveWebpart { class Program { static void Main(string[] args) { string userName = "userName@tenantName.onmicrosoft.com"; string password = "PassWord"; string siteUrl = "https://tenantName.sharepoint.com/sites/TeamSite"; SecureString securePassword = new SecureString(); foreach(char c in password) { securePassword.AppendChar(c); } var cred = new SharePointOnlineCredentials(userName, securePassword); using(ClientContext clientContext = new ClientContext(siteUrl)) { try { clientContext.Credentials = cred; clientContext.Load(clientContext.Web); clientContext.ExecuteQuery(); File oFile = clientContext.Web.GetFileByServerRelativeUrl("/sites/TeamSite/SitePages/wikiPage.aspx"); LimitedWebPartManager ltdWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared); clientContext.Load(ltdWebPartManager.WebParts); clientContext.ExecuteQuery(); if (ltdWebPartManager.WebParts.Count == 0) { throw new Exception("No Web Parts to delete."); } WebPartDefinition webPartDef = ltdWebPartManager.WebParts[0]; webPartDef.DeleteWebPart(); clientContext.ExecuteQuery(); } catch (Exception e) { Console.WriteLine(e); } Console.WriteLine("Succcesfully deleted webpart"); } } } } |
In the below image web pat is deleted from the wikipage,
Print article | This entry was posted by Peter on May 5, 2021 at 1:59 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |