SharePoint 2010 - 将可视Web部件添加到页面C#

时间:2011-08-22 18:10:58

标签: sharepoint

我在Visual Studio 2010中创建了一个简单的Visual Web部件。现在,我如何以编程方式将此Web部件添加到特定的Sharepoint 2010页面?我已经有了生成带有列表视图Web部件的One Column Wiki页面的代码。

2 个答案:

答案 0 :(得分:3)

使用SPLimitedWebPartManager对象

SPLimitedWebPartManager webPartManager = homePage.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
SPList announcementsList = currentWeb.Lists["Announcements"];
ListViewWebPart announcementsWebPart = new ListViewWebPart();
announcementsWebPart = new ListViewWebPart();
announcementsWebPart.Title = announcementsList.Title;
announcementsWebPart.ZoneID = "Left";
announcementsWebPart.ListName = announcementsList.ID.ToString("B").ToUpper();
announcementsWebPart.TitleUrl = announcementsList.DefaultViewUrl;
announcementsWebPart.ViewGuid = announcementsList.DefaultView.ID.ToString("B").ToUpper();
webPartManager.AddWebPart(announcementsWebPart, announcementsWebPart.ZoneID, 1);
webPartManager.SaveChanges(announcementsWebPart); 

尝试thisthisJan Tielens post,虽然旧的仍然相关。

答案 1 :(得分:1)

1 - create sharepoint empty project
2 - create site definition
3 - create a webpart
4 - in onet.xml insert your webpart
5 - create a page to handle the webpart in onet folder
6 - create feature Site scope
7 - Deploy
8 - in sharepoint -> Create new Site / You site definiton / and voulá magic