public static void AddCEWP(SPWeb web, string pageUrl, string zoneId,int zoneIndex, string title, PartChromeState chromState, PartChromeType chromeType) { ContentEditorWebPart contentEditor = new ContentEditorWebPart(); contentEditor.ZoneID = zoneId; contentEditor.Title = title; contentEditor.ChromeState = chromState; contentEditor.ChromeType = chromeType; AddWebPart(web, contentEditor, pageUrl, zoneIndex); }
/// <summary> /// Adds custom HTML and Javascript to list view page. /// </summary> /// <param name="spWeb">Web site on which the changes are to be made.</param> /// <param name="viewPageUrl">Web application-relative URL of the list view page.</param> /// <param name="scriptPageUrl">Site-relative URL of the page that contains HTML and script.</param> private void AddCustomScriptToViewPage(SPWeb spWeb, string viewPageUrl, string scriptPageUrl) { SPFile file = spWeb.GetFile(viewPageUrl); using (SPLimitedWebPartManager webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared)) { ContentEditorWebPart contentEditorWebPart = new ContentEditorWebPart(); contentEditorWebPart.AllowEdit = false; contentEditorWebPart.ContentLink = SPUrlUtility.CombineUrl(spWeb.Url, scriptPageUrl); webPartManager.AddWebPart(contentEditorWebPart, "ZoneLeft", 0); } file.Update(); }
public static void EnsureContentEditorWebPart(this SPWeb web, string fileUrl, string zoneId, string webPartTitle, string webPartContent, int zoneIndex = 0) { using (var manager = web.GetLimitedWebPartManager(fileUrl, PersonalizationScope.Shared)) { var webPart = manager.WebParts.OfType <ContentEditorWebPart>().FirstOrDefault(x => x.Title == webPartTitle); if (webPart != null) { return; } webPart = new ContentEditorWebPart(); webPart.Title = webPartTitle; XmlDocument xmlDoc = new XmlDocument(); XmlElement xmlElement = xmlDoc.CreateElement("Root"); xmlElement.InnerText = webPartContent; webPart.Content = xmlElement; manager.AddWebPart(webPart, zoneId, zoneIndex); } }
public static void AddWebPartContentEditor(SPWeb web, string pageUrl, string zoneID, int zoneIndex, string testForContentEditor, string link, PartChromeType cromeType, string title) { try { ContentEditorWebPart contentEditor = new ContentEditorWebPart(); XmlDocument xmlDoc = new XmlDocument(); XmlElement xmlElement = xmlDoc.CreateElement("HtmlContent"); xmlElement.InnerText = testForContentEditor; contentEditor.Content = xmlElement; using (SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared)) { contentEditor.ChromeType = cromeType; contentEditor.ContentLink = link; contentEditor.Title = title; manager.AddWebPart(contentEditor, zoneID, zoneIndex); } } catch (Exception ee) { EssnLog.logInfo("Error on AddWebPartContentEditor in FeatureActivated."); EssnLog.logExc(ee); } }
public static void CreateContentEditorWebPart(SPLimitedWebPartManager webPartManager, string Content, string zone, int zoneIndex, PartChromeType chromeType, string webPartTitle) { // validation webPartManager.RequireNotNull("webPartManager"); Content.RequireNotNullOrEmpty("Content"); zone.RequireNotNullOrEmpty("zone"); webPartTitle.RequireNotNullOrEmpty("webPartTitle"); Guid storageKey = Guid.NewGuid(); string wpId = String.Format("g_{0}", storageKey.ToString().Replace('-', '_')); XmlDocument doc = new XmlDocument(); XmlElement div = doc.CreateElement("div"); div.InnerText = Content; ContentEditorWebPart cewp = new ContentEditorWebPart { Content = div, ID = wpId, Title = webPartTitle }; cewp.ChromeType = chromeType; webPartManager.AddWebPart(cewp, zone, zoneIndex); webPartManager.SaveChanges(cewp); }
/// <summary> /// Replaces the content of a <see cref="ContentEditorWebPart"/>. /// </summary> /// <param name="web">The web that the file belongs to.</param> /// <param name="file">The file that the web part is associated with.</param> /// <param name="settings">The settings object containing user provided parameters.</param> /// <param name="wp">The web part whose content will be replaced.</param> /// <param name="regex">The regular expression object which contains the search pattern.</param> /// <param name="manager">The web part manager. This value may get updated during this method call.</param> /// <param name="wasCheckedOut">if set to <c>true</c> then the was checked out prior to this method being called.</param> /// <param name="modified">if set to <c>true</c> then the web part was modified as a result of this method being called.</param> /// <returns>The modified web part. This returned web part is what must be used when saving any changes.</returns> internal static WebPart ReplaceValues(SPWeb web, SPFile file, Settings settings, ContentEditorWebPart wp, Regex regex, ref SPLimitedWebPartManager manager, ref bool wasCheckedOut, ref bool modified) { if (wp.Content.FirstChild == null && string.IsNullOrEmpty(wp.ContentLink)) return wp; // The first child of a the content XmlElement for a ContentEditorWebPart is a CDATA section // so we want to work with that to make sure we don't accidentally replace the CDATA text itself. bool isContentMatch = false; if (wp.Content.FirstChild != null) isContentMatch = regex.IsMatch(wp.Content.FirstChild.InnerText); bool isLinkMatch = false; if (!string.IsNullOrEmpty(wp.ContentLink)) isLinkMatch = regex.IsMatch(wp.ContentLink); if (!isContentMatch && !isLinkMatch) return wp; string content; if (isContentMatch) content = wp.Content.FirstChild.InnerText; else content = wp.ContentLink; string result = content; if (!string.IsNullOrEmpty(content)) result = regex.Replace(content, settings.ReplaceString); Logger.Write("Match found: File={0}, WebPart={1}, Replacement={2} => {3}", file.ServerRelativeUrl, wp.Title, content, result); if (!settings.Test) { if (file.CheckOutType == SPFile.SPCheckOutType.None) { file.CheckOut(); wasCheckedOut = false; } // We need to reset the manager and the web part because a checkout (now or from an earlier call) // could mess things up so safest to just reset every time. manager.Web.Dispose(); // manager.Dispose() does not dispose of the SPWeb object and results in a memory leak. manager.Dispose(); manager = web.GetLimitedWebPartManager(file.Url, PersonalizationScope.Shared); wp.Dispose(); wp = (ContentEditorWebPart)manager.WebParts[wp.ID]; if (isContentMatch) wp.Content = GetDataAsXmlElement("Content", "http://schemas.microsoft.com/WebPart/v2/ContentEditor", result); else wp.ContentLink = result; modified = true; } return wp; }