SharePoint【学习笔记】-- 页面上添加WebPart(代码篇)

 01. 添加List View Webpart

SPList oList = web.Lists[Constant._CUSTOMER_CONTRACT_LIST_TITLE]; 

ListViewWebPart oListViewWebPart = new ListViewWebPart(); 

oListViewWebPart.ZoneID = "Left"; 

oListViewWebPart.ListName = oList.ID.ToString("B").ToUpper(); 

oListViewWebPart.ViewGuid = oList.Views[0].ID.ToString("B").ToUpper(); 

SPWebPartCollection collWebParts = web.GetWebPartCollection("default.aspx", Storage.Shared); 

System.Guid guid = collWebParts.Add(oListViewWebPart); 

 

02. 添加自定义WebPart

string url = web.Url + "/Lists/SafetyLab/ByLocation.aspx"; 

SPFile file = web.GetFile(url); 

SPLimitedWebPartManager webPartManager = 

file.GetLimitedWebPartManager(PersonalizationScope.Shared); 

Microsoft.SharePoint.WebPartPages.WebPart wpReport = new ALU.C0370A.VPPI.ReportWebPart(); 

webPartManager.AddWebPart(wpReport, "Main", 0); 

 

你可能感兴趣的:(SharePoint)