public void AddWikiOrPublishingPageWebParts(ClientContext ctx, Web web, string contentFieldName) { var rootWeb = ctx.Site.RootWeb; rootWeb.EnsureProperties(w => w.Url, w => w.ServerRelativeUrl); if (WebParts == null || WebParts.Count == 0) { var pageContent = ListItemFieldValues.Find(p => p.FieldName == contentFieldName)?.Value ?? String.Empty; if (web.ServerRelativeUrl != "/") { File.ListItemAllFields[contentFieldName] = pageContent.Replace("{@WebUrl}", web.Url).Replace("{@WebServerRelativeUrl}", web.ServerRelativeUrl).Replace("{@SiteUrl}", rootWeb.Url).Replace("{@SiteServerRelativeUrl}", rootWeb.ServerRelativeUrl); } else { //In this case the web is the root web File.ListItemAllFields[contentFieldName] = pageContent.Replace("{@WebUrl}", web.Url).Replace("{@WebServerRelativeUrl}", "").Replace("{@SiteUrl}", web.Url).Replace("{@SiteServerRelativeUrl}", ""); } File.ListItemAllFields.Update(); return; } var newIdMappings = new Dictionary <string, string>(); var limitedWebPartManager = File.GetLimitedWebPartManager(PersonalizationScope.Shared); AddWikiOrPublishingContentPageWebParts(ctx, web, rootWeb, newIdMappings, limitedWebPartManager); UpdateWikiOrPublishingContentWithStorageKeys(newIdMappings, web, rootWeb, contentFieldName); LoadWebPartManagerFromSharePoint(ctx, limitedWebPartManager); MoveWebPartsToWikiOrPublishingContentEditorWebPartZone(ctx, newIdMappings, limitedWebPartManager); SetPageListViews(ctx, web, newIdMappings); }
private void UpdateWikiOrPublishingContentWithStorageKeys(Dictionary <string, string> newIdMappings, Web web, string contentFieldName) { var pageContent = ListItemFieldValues.Find(p => p.FieldName == contentFieldName)?.Value ?? String.Empty; File.ListItemAllFields[contentFieldName] = WikiPageUtility.GetUpdatedWikiContentText(pageContent, WikiPageWebPartStorageKeyMappings, newIdMappings) .Replace("{@WebServerRelativeUrl}", web.ServerRelativeUrl != "/" ? web.ServerRelativeUrl : ""); File.ListItemAllFields.Update(); File.Context.ExecuteQuery(); }