3
我已經在Visual Studio 2010中創建了一個簡單的可視Web部件。現在如何將該Web部件以編程方式添加到特定Sharepoint 2010頁面?我已經有了一個代碼,可以用列表視圖Web部件生成一列Wiki頁面。SharePoint 2010 - 將可視Web部件添加到頁面C#
我已經在Visual Studio 2010中創建了一個簡單的可視Web部件。現在如何將該Web部件以編程方式添加到特定Sharepoint 2010頁面?我已經有了一個代碼,可以用列表視圖Web部件生成一列Wiki頁面。SharePoint 2010 - 將可視Web部件添加到頁面C#
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);
嘗試this或this。 Jan Tielens post,而舊的仍然有關。
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
;)
感謝但這只是增加了一個ListViewWebPart。我需要添加一個已部署爲.wsp的可視Web部件。在編輯模式'插入 - Web部件 - 自定義 - VisualWebPart'中,我可以通過瀏覽器手動添加它 – Colbs
將ListViewWebPart替換爲可視Web部件中類的名稱。任何從System.Web.UI.WebControls.WebParts.WebPart繼承的webpart都可以使用SPLimitedWebPartManager.AddWebPart。 –
代碼下面的兩個鏈接顯示瞭如何從您的Web部件庫加載。 – Nat