How to add web part to SitePages / Home.aspx using CSOM

Has anyone been able to add a web part to a Wiki page using CSOM?

Reference Information. Home.aspx is a Wiki page, and all of its WP files are in the rich text area (this is actually the "WikiField" column). Technically, they are located in a hidden zone of the wpz web part, and in addition to that, there is always a placeholder with a WP ID in the WikiField column.

I changed the existing server-side code seen at http://blog.mastykarz.nl/programmatically-adding-web-parts-rich-content-sharepoint-2010/ and http://640kbisenough.com/2014/06/26 / sharepoint-2013-moving-webparts-programmatically-to-rich-content-zone / :

using System; using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.WebParts; public class Class1 { void DeployWebPart(ClientContext clientContext, string zone, string pattern, string position) { List library = clientContext.Web.GetList("/sites/site/SitePages/"); clientContext.Load(library); clientContext.ExecuteQuery(); CamlQuery query = CamlQuery.CreateAllItemsQuery(100); ListItemCollection itemColl = library.GetItems(query); clientContext.Load(itemColl, items => items.Include(i => i.Id, i => i.DisplayName, i => i["WikiField"])); clientContext.ExecuteQuery(); ListItem item = itemColl.Where(i => i.DisplayName == "Home").First(); clientContext.ExecuteQuery(); File page = item.File; LimitedWebPartManager lwm = page.GetLimitedWebPartManager(PersonalizationScope.Shared); string xmlWebPart = @"<webParts>...</webParts>"; WebPartDefinition wpd = lwm.ImportWebPart(xmlWebPart); WebPartDefinition realWpd = lwm.AddWebPart(wpd.WebPart, "wpz", 0); List targetList = clientContext.Web.GetList("/sites/site/Announcements/"); clientContext.Load(targetList, l => l.Views); clientContext.Load(realWpd); clientContext.ExecuteQuery(); string wpId = String.Format("g_{0}", realWpd.Id.ToString().Replace('-', '_')); if (zone == "wpz") { string htmlcontent = String.Format(CultureInfo.InvariantCulture, "<div class=\"ms-rtestate-read ms-rte-wpbox\" contenteditable=\"false\"><div class=\"ms-rtestate-notify ms-rtestate-read {0}\" id=\"div_{0}\"></div><div id=\"vid_{0}\" style=\"display:none;\"></div></div>", new object[] { realWpd.Id.ToString("D") }); string content = item["WikiField"] as string; System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(System.Text.RegularExpressions.Regex.Escape(pattern)); if (position == "before") { content = regex.Replace(content, (htmlcontent + pattern), 1); } else { content = regex.Replace(content, (pattern + htmlcontent), 1); } item.Update(); clientContext.ExecuteQuery(); } } } 

Everything works fine until the last .Update () and clientContext.ExecuteQuery () elements are involved. Before updating (), a new placeholder is correctly inserted into the contents of the WikiField. But after updating (), the contents of WikiField are returned to their original state (!)

Note. Alternatively, you can add WP to another zone (for example, "Bottom"). In this case, WP is displayed on the page. But it has two main drawbacks: the newly created zone is poorly formatted and WP cannot be moved or even deleted.

Thanks for any input.

+5
source share
1 answer

The following example shows how to add a web part to an Enterprise Wiki page:

 public static void AddWebPartIntoWikiPage(ClientContext context, string pageUrl, string webPartXml) { var page = context.Web.GetFileByServerRelativeUrl(pageUrl); var webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared); var importedWebPart = webPartManager.ImportWebPart(webPartXml); var webPart = webPartManager.AddWebPart(importedWebPart.WebPart, "wpz", 0); context.Load(webPart); context.ExecuteQuery(); string marker = String.Format(CultureInfo.InvariantCulture, "<div class=\"ms-rtestate-read ms-rte-wpbox\" contentEditable=\"false\"><div class=\"ms-rtestate-read {0}\" id=\"div_{0}\"></div><div style='display:none' id=\"vid_{0}\"></div></div>", webPart.Id); ListItem item = page.ListItemAllFields; context.Load(item); context.ExecuteQuery(); item["PublishingPageContent"] = marker; item.Update(); context.ExecuteQuery(); } 

Using

 var webPartXml = System.IO.File.ReadAllText(filePath); using (var ctx = new ClientContext(webUri)) { AddWebPartIntoWikiPage(ctx, wikiPageUrl,webPartXml); } 

Result

enter image description here

+3
source

Source: https://habr.com/ru/post/1208218/


All Articles