2011-08-22 76 views

回答

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,而舊的仍然有關。

+0

感謝但這只是增加了一個ListViewWebPart。我需要添加一個已部署爲.wsp的可視Web部件。在編輯模式'插入 - Web部件 - 自定義 - VisualWebPart'中,我可以通過瀏覽器手動添加它 – Colbs

+0

將ListViewWebPart替換爲可視Web部件中類的名稱。任何從System.Web.UI.WebControls.WebParts.WebPart繼承的webpart都可以使用SPLimitedWebPartManager.AddWebPart。 –

+0

代碼下面的兩個鏈接顯示瞭如何從您的Web部件庫加載。 – Nat

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 

;)

相關問題