I have created a simple Visual Web Part in Visual Studio 2010. How can I now add this web part programatically to a specific Sharepoint 2010 page? I already have code that generates a One Colu开发者_运维百科mn Wiki Page with a list view web part.
Use the SPLimitedWebPartManager object
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);
Try this or this. Jan Tielens post, while old is still relevant.
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
;)
精彩评论