public GuidInstance GetStorageKey(SPWebPartInstance webPart) { if (webPart == null) { throw new JavaScriptException(this.Engine, "Error", "A web part must be supplied as the first argument."); } var result = m_limitedWebPartManager.GetStorageKey(webPart.WebPart); return(new GuidInstance(this.Engine.Object.InstancePrototype, result)); }
public static WebPart AddWebPartToWikiPage(SPListItem item, string webPartXml, string title, int row, int col, bool addSpace, Hashtable customReplaceText, PartChromeType chromeType, bool publish) { string wikiField = (string)item["WikiField"]; XmlDocument xd = new XmlDocument(); xd.PreserveWhitespace = true; xd.LoadXml(wikiField); // Sometimes the wikifield content seems to be surrounded by an additional div? XmlElement layoutsTable = xd.SelectSingleNode("div/div/table") as XmlElement; if (layoutsTable == null) { layoutsTable = xd.SelectSingleNode("div/table") as XmlElement; } XmlElement layoutsZoneInner = layoutsTable.SelectSingleNode(string.Format("tbody/tr[{0}]/td[{1}]/div/div", row, col)) as XmlElement; if (layoutsZoneInner == null) { throw new ArgumentException("Unable to locate row and/or column to insert HTML into."); } bool checkBackIn = false; if (item.File.InDocumentLibrary) { bool ignoreCheckOut = item.File.CheckOutType == SPFile.SPCheckOutType.None && !item.ParentList.ForceCheckout; if (!ignoreCheckOut) { if (!Utilities.IsCheckedOut(item) || !Utilities.IsCheckedOutByCurrentUser(item)) { checkBackIn = true; item.File.CheckOut(); } } // If it's checked out by another user then this will throw an informative exception so let it do so. } SPLimitedWebPartManager limitedWebPartManager = null; WebPart wpdNew = null; try { limitedWebPartManager = item.File.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared); wpdNew = SPCmdletImportWebPart.AddWebPart(limitedWebPartManager, item.File, webPartXml, customReplaceText); wpdNew.ChromeType = chromeType; limitedWebPartManager.AddWebPart(wpdNew, "wpz", 0); Guid storageKey = limitedWebPartManager.GetStorageKey(wpdNew); // - space element XmlElement space = xd.CreateElement("p"); XmlText text = xd.CreateTextNode(" "); space.AppendChild(text); // - wpBoxDiv XmlElement wpBoxDiv = xd.CreateElement("div"); layoutsZoneInner.AppendChild(wpBoxDiv); if (addSpace) { layoutsZoneInner.AppendChild(space); } XmlAttribute 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 XmlElement div1 = xd.CreateElement("div"); wpBoxDiv.AppendChild(div1); div1.IsEmpty = false; attribute = xd.CreateAttribute("class"); div1.Attributes.Append(attribute); attribute.Value = "ms-rtestate-read " + storageKey.ToString("D"); attribute = xd.CreateAttribute("id"); div1.Attributes.Append(attribute); attribute.Value = "div_" + storageKey.ToString("D"); // - div2 XmlElement 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_" + storageKey.ToString("D"); item["WikiField"] = xd.OuterXml; item.Update(); if (wpdNew.Title != title) { wpdNew.Title = title; limitedWebPartManager.SaveChanges(wpdNew); } } finally { if (limitedWebPartManager != null) { limitedWebPartManager.Dispose(); } SPFile file = item.File; if (file.InDocumentLibrary && Utilities.IsCheckedOut(file.Item) && (checkBackIn || publish)) { file.CheckIn("Checking in changes to page due to new web part being added: " + title); } if (publish && file.InDocumentLibrary) { file.Publish("Publishing changes to page due to new web part being added: " + title); if (file.Item.ModerationInformation != null) { file.Approve("Approving changes to page due to new web part being added: " + title); } } } return(wpdNew); }