Expand Minimize

Use AddWebPartToWikiPage to add WebParts to Wiki Pages

PnP has an implementation to add web parts to wiki pages.

CheckId SPC029001
TypeName UseAddWebPartToWikiPage
Severity Warning
Type Assembly

Standard SharePoint CSOM

var webPartl = @"<?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""> </WebPart>"; var webpart = new WebPartEntity {WebPartXml = webPartXml}; var webPartPage = "/sites/demo/sitepages/home.aspx"; var col = 1; var row = 1; web.Context.Load(webPartPage, wp => wp.ListItemAllFields); web.Context.ExecuteQueryRetry(); string wikiField = (string)webPartPage.ListItemAllFields["WikiField"]; var wpdNew = AddWebPart(webPartPage, webPart, "wpz", 0); // Close all BR tags var brRegex = new Regex("
", RegexOptions.IgnoreCase); wikiField = brRegex.Replace(wikiField, "<br/>"); var xd = new XmlDocument(); xd.PreserveWhitespace = true; xd.LoadXml(wikiField); // Sometimes the wikifield content seems to be surrounded by an additional div? var layoutsTable = xd.SelectSingleNode("div/div/table") as XmlElement ?? xd.SelectSingleNode("div/table") as XmlElement; var layoutsZoneInner = layoutsTable.SelectSingleNode($"tbody/tr[{row}]/td[{col}]/div/div") as XmlElement; // - wpBoxDiv var wpBoxDiv = xd.CreateElement("div"); layoutsZoneInner.AppendChild(wpBoxDiv); var attribute = xd.CreateAttribute("class"); wpBoxDiv.Attributes.Append(attribute); attribute.Value = "ms-rtestate-read ms-rte-wpbox"; attribute = xd.CreateAttribute("contentEditable"); wpBoxDiv.Attributes.Append(attribute); attribute.Value = "false"; // - div1 var div1 = xd.CreateElement("div"); wpBoxDiv.AppendChild(div1); div1.IsEmpty = false; attribute = xd.CreateAttribute("class"); div1.Attributes.Append(attribute); attribute.Value = "ms-rtestate-read " + wpdNew.Id.ToString("D"); attribute = xd.CreateAttribute("id"); div1.Attributes.Append(attribute); attribute.Value = "div_" + wpdNew.Id.ToString("D"); // - div2 var div2 = xd.CreateElement("div"); wpBoxDiv.AppendChild(div2); div2.IsEmpty = false; attribute = xd.CreateAttribute("style"); div2.Attributes.Append(attribute); attribute.Value = "display:none"; attribute = xd.CreateAttribute("id"); div2.Attributes.Append(attribute); attribute.Value = "vid_" + wpdNew.Id.ToString("D"); var listItem = webPartPage.ListItemAllFields; listItem["WikiField"] = xd.OuterXml; listItem.Update(); web.Context.ExecuteQueryRetry(); return wpdNew;
Using PnP
var webPartXml = @"<?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""> </WebPart>"; var webpartEntity = new WebPartEntity {WebPartXml = webPartXml}; web.AddWebPartToWikiPage("/sites/demo/sitepages/home.aspx", webpartEntity, 1, 1, false);

To suppress this violation in managed code add the following attribute to the method which contains the instruction (available since SPCAF version v5.2). Learn more about SuppressMessage here.

// Important: Ensure to have #define CODE_ANALYSIS at the beginning of your .cs file
[SuppressMessage("Rencore.Analyzers.Assemblies.Rules.PnP.PnPBestPracticesGroup", "SPC029001:UseAddWebPartToWikiPage", Justification = "Provide reason for suppression here")]
Disclaimer: The views and opinions expressed in this documentation and in SPCAF do not necessarily reflect the opinions and recommendations of Microsoft or any member of Microsoft. SPCAF and RENCORE are registered trademarks of Rencore. All other trademarks, service marks, collective marks, copyrights, registered names, and marks used or cited by this documentation are the property of their respective owners.