/// <summary> /// Provisions the web parts. /// </summary> /// <param name="manager">The manager.</param> protected virtual void ProvisionWebParts(SPLimitedWebPartManager manager) { // Guard if (manager == null) { throw new ArgumentNullException("manager"); } ImageWebPart webPart = new ImageWebPart { ImageLink = "/_layouts/images/docset_welcomepage_big.png" }; manager.AddWebPart(webPart, "WebPartZone_TopLeft", 1); this.AddWebPart(manager, this.Web.Url + "/_catalogs/wp/documentsetproperties.dwp", "WebPartZone_Top", 2); this.AddWebPart( manager, this.Web.Url + "/_catalogs/wp/documentsetcontents.dwp", "WebPartZone_CenterMain", 2); }
/// <summary> /// Replaces the content of a <see cref="ImageWebPart"/> web part. /// </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, ImageWebPart wp, Regex regex, ref SPLimitedWebPartManager manager, ref bool wasCheckedOut, ref bool modified) { if (string.IsNullOrEmpty(wp.ImageLink) && string.IsNullOrEmpty(wp.AlternativeText)) return wp; bool isAltTextMatch = false; if (!string.IsNullOrEmpty(wp.AlternativeText)) isAltTextMatch = regex.IsMatch(wp.AlternativeText); bool isLinkMatch = false; if (!string.IsNullOrEmpty(wp.ImageLink)) isLinkMatch = regex.IsMatch(wp.ImageLink); if (!isAltTextMatch && !isLinkMatch) return wp; string altTextContent = wp.AlternativeText; string linkContent = wp.ImageLink; string altTextResult = altTextContent; string linkResult = linkContent; if (!string.IsNullOrEmpty(altTextContent)) altTextResult = regex.Replace(altTextContent, settings.ReplaceString); if (!string.IsNullOrEmpty(linkContent)) linkResult = regex.Replace(linkContent, settings.ReplaceString); if (isAltTextMatch) Logger.Write("Match found: File={0}, WebPart={1}, Replacement={2} => {3}", file.ServerRelativeUrl, wp.Title, altTextContent, altTextResult); if (isLinkMatch) Logger.Write("Match found: File={0}, WebPart={1}, Replacement={2} => {3}", file.ServerRelativeUrl, wp.Title, linkContent, linkResult); 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 = (ImageWebPart)manager.WebParts[wp.ID]; if (isAltTextMatch) wp.AlternativeText = altTextResult; if (isLinkMatch) wp.ImageLink = linkResult; modified = true; } return wp; }