http://blog.mastykarz.nl/preventing-provisioning-duplicate-web-parts-instances-on-feature-reactivation/
using (SPSite site = new SPSite("http://sharepoint")) { SPList list = site.GetCatalog(SPListTemplateType.MasterPageCatalog); SPListItemCollection items = list.Items; List<string> webParts = new List<string>(); // find the right Page Layout foreach (SPListItem item in items) { if (item.Name.Equals("CustomPageLayout.aspx", StringComparison.CurrentCultureIgnoreCase)) { SPFile file = item.File; // get the Web Part Manager for the Page Layout SPLimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared); // iterate through all Web Parts and remove duplicates while (wpm.WebParts.Count > 0) { StringBuilder sb = new StringBuilder(); XmlWriterSettings xws = new XmlWriterSettings(); xws.OmitXmlDeclaration = true; XmlWriter xw = XmlWriter.Create(sb, xws); System.Web.UI.WebControls.WebParts.WebPart wp = wpm.WebParts[0]; wpm.ExportWebPart(wp, xw); xw.Flush(); string md5Hash = getMd5Hash(sb.ToString()); if (webParts.Contains(md5Hash)) wpm.DeleteWebPart(wp); else webParts.Add(md5Hash); } } } }
No comments:
Post a Comment