private void HandleWikiPageProvision(ListItem listItem, WebPartDefinitionBase webpartModel) { if (!webpartModel.AddToPageContent) { return; } TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "AddToPageContent = true. Handling wiki/publishig page provision case."); var context = listItem.Context; var targetFieldName = string.Empty; if (listItem.FieldValues.ContainsKey(BuiltInInternalFieldNames.WikiField)) { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "WikiField field is detected. Switching to wiki page web part provision."); targetFieldName = BuiltInInternalFieldNames.WikiField; } else if (listItem.FieldValues.ContainsKey(BuiltInInternalFieldNames.PublishingPageLayout)) { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "PublishingPageLayout field is detected. Switching to publishin page web part provision."); targetFieldName = BuiltInInternalFieldNames.PublishingPageContent; } else { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "Not PublishingPageLayout field, nor WikiField is detected. Skipping."); return; } var wikiTemplate = new StringBuilder(); var wpId = webpartModel.Id .Replace("g_", string.Empty) .Replace("_", "-"); var content = listItem[targetFieldName] == null ? string.Empty : listItem[targetFieldName].ToString(); wikiTemplate.AppendFormat( "<div class='ms-rtestate-read ms-rte-wpbox' contentEditable='false'>"); wikiTemplate.AppendFormat(" <div class='ms-rtestate-read {0}' id='div_{0}'>", wpId); wikiTemplate.AppendFormat(" </div>"); wikiTemplate.AppendFormat("</div>"); var wikiResult = wikiTemplate.ToString(); if (string.IsNullOrEmpty(content)) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Page content is empty Generating new one."); content = wikiResult; listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { if (content.ToUpper().IndexOf(wpId.ToUpper()) == -1) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Replacing web part with ID: [{0}] on the page content.", wpId); content += wikiResult; listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { TraceService.WarningFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find web part ID: [{0}] the page content. Provision won't add web part on the page content.", new object[] { wpId }); } } }
private void HandleWikiPageProvision(ListItem listItem, WebPartDefinitionBase webpartModel, Guid?currentWebPartStoreKey, Guid?oldWebParStoreKey) { if (!webpartModel.AddToPageContent) { return; } TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "AddToPageContent = true. Handling wiki/publishig page provision case."); var context = listItem.Context; var targetFieldName = string.Empty; if (listItem.FieldValues.ContainsKey(BuiltInInternalFieldNames.WikiField)) { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "WikiField field is detected. Switching to wiki page web part provision."); targetFieldName = BuiltInInternalFieldNames.WikiField; } else if (listItem.FieldValues.ContainsKey(BuiltInInternalFieldNames.PublishingPageLayout)) { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "PublishingPageLayout field is detected. Switching to publishin page web part provision."); targetFieldName = BuiltInInternalFieldNames.PublishingPageContent; } else { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "Not PublishingPageLayout field, nor WikiField is detected. Skipping."); return; } // any on the page? var existingWebPartId = string.Empty; // current from the new provision var upcomingWebPartId = string.Empty; // weird, by some web part ignor ID from the XML // so webpartStoreKey from the previous CSOM adding web part to the page must be used // M2 covers that fact with the regression testing, so we know what are they // and we have NOD idea why it happens if (ShouldUseWebPartStoreKeyForWikiPage) { upcomingWebPartId = currentWebPartStoreKey.ToString() .Replace("g_", string.Empty) .Replace("_", "-");; } else { // get from the model upcomingWebPartId = webpartModel.Id.ToString() .Replace("g_", string.Empty) .Replace("_", "-");; } if (!oldWebParStoreKey.HasGuidValue()) { // first provision existingWebPartId = currentWebPartStoreKey.ToString() .Replace("g_", string.Empty) .Replace("_", "-"); } else { // second provision, // we had web part on the page and can reuse that ID to relink on wiki content existingWebPartId = oldWebParStoreKey.ToString() .Replace("g_", string.Empty) .Replace("_", "-"); } var content = listItem[targetFieldName] == null ? string.Empty : listItem[targetFieldName].ToString(); var wikiTemplate = new StringBuilder(); // actual ID will be replaced later wikiTemplate.AppendFormat("<div class='ms-rtestate-read ms-rte-wpbox' contentEditable='false'>"); wikiTemplate.Append(" <div class='ms-rtestate-read {0}' id='div_{0}'>"); wikiTemplate.AppendFormat(" </div>"); wikiTemplate.AppendFormat("</div>"); var wikiTemplateOutput = wikiTemplate.ToString(); if (string.IsNullOrEmpty(content)) { // page is empty, pre-generating HTML // pushing web part as the current WebPart Key TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Page content is empty Generating new one."); content = string.Format(wikiTemplateOutput, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { // there is a content on the page // there might be some web parts too if (oldWebParStoreKey.HasGuidValue()) { // there was an old web part on the page // checking if markup has the ID if (content.ToUpper().IndexOf(existingWebPartId.ToUpper()) != -1) { // was old web part on the page? // yes, replacing ID TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, string.Format("Replacing web part with ID: [{0}] to [{1}] on the page content.", existingWebPartId, upcomingWebPartId), null); content = content.Replace(existingWebPartId, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } // original from the definigion? else if (content.ToUpper().IndexOf(upcomingWebPartId.ToUpper()) != -1) { // yes, replacing ID TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, string.Format("Replacing web part with ID: [{0}] to [{1}] on the page content.", existingWebPartId, upcomingWebPartId), null); // do nothing listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { // adding new, from scratch TraceService.WarningFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find web part ID: [{0}] the page content. Adding a new one.", new object[] { existingWebPartId }); content = content + string.Format(wikiTemplateOutput, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } } else { // first provision of the web part on the page if (content.ToUpper().IndexOf(upcomingWebPartId.ToUpper()) != -1) { // do nothing listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { // adding new, from scratch TraceService.WarningFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find web part ID: [{0}] the page content. Adding a new one.", new object[] { existingWebPartId }); content = content + string.Format(wikiTemplateOutput, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } } } }
private void HandleWikiPageProvision(ListItem listItem, WebPartDefinitionBase webpartModel, Guid?webPartStoreKey, Guid?oldWebParKey) { if (!webpartModel.AddToPageContent) { return; } TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "AddToPageContent = true. Handling wiki/publishig page provision case."); var context = listItem.Context; var targetFieldName = string.Empty; if (listItem.FieldValues.ContainsKey(BuiltInInternalFieldNames.WikiField)) { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "WikiField field is detected. Switching to wiki page web part provision."); targetFieldName = BuiltInInternalFieldNames.WikiField; } else if (listItem.FieldValues.ContainsKey(BuiltInInternalFieldNames.PublishingPageLayout)) { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "PublishingPageLayout field is detected. Switching to publishin page web part provision."); targetFieldName = BuiltInInternalFieldNames.PublishingPageContent; } else { TraceService.Information((int)LogEventId.ModelProvisionCoreCall, "Not PublishingPageLayout field, nor WikiField is detected. Skipping."); return; } var wikiTemplate = new StringBuilder(); var existingWebPartId = string.Empty; var definitionWebPartId = webpartModel.Id.ToString() .Replace("g_", string.Empty) .Replace("_", "-");; var upcomingWebPartId = definitionWebPartId; // aa.... // extremely unfortunate if (webpartModel is XsltListViewWebPartDefinition) { upcomingWebPartId = webPartStoreKey.ToString() .Replace("g_", string.Empty) .Replace("_", "-");; } if (!oldWebParKey.HasGuidValue()) { // first provision existingWebPartId = webPartStoreKey.ToString() .Replace("g_", string.Empty) .Replace("_", "-"); } else { // second, so that we had web part and use that ID existingWebPartId = oldWebParKey.ToString() .Replace("g_", string.Empty) .Replace("_", "-"); } var content = listItem[targetFieldName] == null ? string.Empty : listItem[targetFieldName].ToString(); // actual ID will be replaced later wikiTemplate.AppendFormat("<div class='ms-rtestate-read ms-rte-wpbox' contentEditable='false'>"); wikiTemplate.Append(" <div class='ms-rtestate-read {0}' id='div_{0}'>"); wikiTemplate.AppendFormat(" </div>"); wikiTemplate.AppendFormat("</div>"); var wikiTemplateOutput = wikiTemplate.ToString(); if (string.IsNullOrEmpty(content)) { // page is empty, pre-generating HTML // pushing web part as the current WebPart Key TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Page content is empty Generating new one."); content = string.Format(wikiTemplateOutput, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { // we had web part on the page // so we need to change the ID if (oldWebParKey.HasGuidValue()) { // was old on the page? if (content.ToUpper().IndexOf(existingWebPartId.ToUpper()) != -1) { // yes, replacing ID TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, string.Format("Replacing web part with ID: [{0}] to [{1}] on the page content.", existingWebPartId, upcomingWebPartId), null); content = content.Replace(existingWebPartId, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } // original from the definigion? else if (content.ToUpper().IndexOf(definitionWebPartId.ToUpper()) != -1) { // yes, replacing ID TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, string.Format("Replacing web part with ID: [{0}] to [{1}] on the page content.", existingWebPartId, upcomingWebPartId), null); content = content.Replace(definitionWebPartId, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { // adding new, from scratch TraceService.WarningFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find web part ID: [{0}] the page content. Adding a new one.", new object[] { existingWebPartId }); content = string.Format(wikiTemplateOutput, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } } else { // first provision, no web parts on the wiki page // there should be a definition based web part id in the template // updatting to a upcoming ID if (content.ToUpper().IndexOf(definitionWebPartId.ToUpper()) != -1) { // yes, replacing ID TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, string.Format("Replacing web part with ID: [{0}] to [{1}] on the page content.", definitionWebPartId, upcomingWebPartId), null); content = content.Replace(definitionWebPartId, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } else { // adding new, from scratch TraceService.WarningFormat((int)LogEventId.ModelProvisionCoreCall, "Cannot find web part ID: [{0}] the page content. Adding a new one.", new object[] { existingWebPartId }); content = string.Format(wikiTemplateOutput, upcomingWebPartId); listItem[targetFieldName] = content; listItem.Update(); context.ExecuteQueryWithTrace(); } } } }