////// 向页面添加Webpart /// /// 站点 /// 页面地址 /// Webpart名称 /// WebPartZone的ID /// ZoneIndex /// 字典类型的杂项名称和值 ///public static string AddWebPartToPage(SPWeb web, string pageUrl, string webPartName, string zoneID, int zoneIndex, Dictionary properties) { using (Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager( pageUrl, PersonalizationScope.Shared)) { using (WebPart webPart = CreateWebPart(web, webPartName, webPartManager)) { Microsoft.SharePoint.WebPartPages.SPUserCodeWebPart codeWebPart = webPart.WebBrowsableObject as Microsoft.SharePoint.WebPartPages.SPUserCodeWebPart; if (properties != null) { foreach (string key in properties.Keys) { foreach (Microsoft.SharePoint.WebPartPages.SPUserCodeProperty item in codeWebPart.Properties) { if (item.Name == key) { item.Value = properties[key]; } } } } webPart.ChromeType = PartChromeType.None;//设置部件版式类型 设置 无 webPartManager.AddWebPart(webPart, zoneID, zoneIndex); return webPart.ID; } } } public static WebPart CreateWebPart(SPWeb web, string webPartName, Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager webPartManager) { SPQuery qry = new SPQuery(); qry.Query = String.Format(" ", webPartName); SPList webPartGallery = null; if (null == web.ParentWeb) { webPartGallery = web.GetCatalog( SPListTemplateType.WebPartCatalog); } else { webPartGallery = web.Site.RootWeb.GetCatalog( SPListTemplateType.WebPartCatalog); } SPListItemCollection webParts = webPartGallery.GetItems(qry); XmlReader xmlReader = new XmlTextReader(webParts[0].File.OpenBinaryStream()); string errorMsg; WebPart webPart = webPartManager.ImportWebPart(xmlReader, out errorMsg); return webPart; } {0}
调用
Dictionaryproperity = new Dictionary (); properity.Add("CalendarURL", "/SystemSetting/Lists/EnterpriseCalendar/AllItems.aspx"); CommonUtil.AddWebPartToPage(systemWeb, "/SystemSetting/SitePages/CalendarView.aspx", "EnterpriseCalendarViewWebPart.webpart", "Left", 0, properity);