public static void ProvisionWebpart(SPWeb web, WebpartPageDefinitionCollection collection) { foreach (var item in collection) { try { string pageUrl = item.PageUrl; if (!string.IsNullOrEmpty(item.RootPath) && !string.IsNullOrEmpty(item.FileName)) { if (item.RootPath.EndsWith(".aspx")) { item.PageUrl = item.RootPath; pageUrl = item.RootPath; } else { pageUrl = string.Format("{0}/{1}", item.RootPath.TrimEnd('/'), SPEncode.UrlEncode(item.FileName.TrimStart('/'))); item.PageUrl = pageUrl; } } if (!IsAbsoluteUrl(pageUrl)) { pageUrl = string.Format("{0}/{1}", web.Url.TrimEnd('/'), pageUrl.TrimStart('/')); } web.EnsureWebpartPage(item.PageUrl, item.Title, item.Overwrite, item.UseFormFolder); using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager( pageUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared)) { foreach (var wp in item.Webparts) { try { using (var concerateWP = wp.CreateWebPart(web, webPartManager)) { if (concerateWP == null) { continue; } try { if (!wp.AllowDuplicate && webPartManager.WebParts.Cast <System.Web.UI.WebControls.WebParts.WebPart>().Any(p => p.Title == concerateWP.Title)) { continue; } } catch (Exception) { //throw; } webPartManager.AddWebPart(concerateWP, wp.ZoneId, wp.Index == 0 ?webPartManager.WebParts.Count + 1: wp.Index); CreateDefaultWebPart(web, webPartManager, wp, concerateWP); } } catch (Exception ex) { } } } } catch (Exception ex) { } } }
public static void ProvisionWebpart(SPWeb web, WebpartPageDefinitionCollection collection) { foreach (var item in collection) { try { string pageUrl = item.PageUrl; if (!string.IsNullOrEmpty(item.RootPath) && !string.IsNullOrEmpty(item.FileName)) { if (item.RootPath.EndsWith(".aspx")) { item.PageUrl = item.RootPath; pageUrl = item.RootPath; } else { pageUrl = string.Format("{0}/{1}", item.RootPath.TrimEnd('/'), SPEncode.UrlEncode(item.FileName.TrimStart('/'))); item.PageUrl = pageUrl; } } if (!IsAbsoluteUrl(pageUrl)) { pageUrl = string.Format("{0}/{1}", web.Url.TrimEnd('/'), pageUrl.TrimStart('/')); } web.EnsureWebpartPage(item.PageUrl, item.Title, item.Overwrite); using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager( pageUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared)) { foreach (var wp in item.Webparts) { try { using (var concerateWP = wp.CreateWebPart(web, webPartManager)) { if (concerateWP == null) continue; try { if (!wp.AllowDuplicate && webPartManager.WebParts.Cast<System.Web.UI.WebControls.WebParts.WebPart>().Any(p => p.Title == concerateWP.Title)) { continue; } } catch (Exception) { //throw; } webPartManager.AddWebPart(concerateWP, wp.ZoneId, wp.Index ==0 ?webPartManager.WebParts.Count+1: wp.Index); CreateDefaultWebPart(web, webPartManager, wp, concerateWP); } } catch (Exception ex) { } } } } catch (Exception ex) { } } }