I am trying to add the Web Parts to Page Instance of the Page Layout using a Feature.I know it should be through AllUsersWebPart element .I could find the reference of adding a Default Web Part to Page Layout (PortalLayouts feature does it). Also I have seen same in onet.xml . I wanted to know if it possible to create an instance of the page based on the Page layout and add web part to it using the feature.
views:
501answers:
1
+1
A:
Inside a feature activation event handler ...
Get the Page to add the webpart (in this case - default.aspx)
SPFile thePage = curWeb.RootFolder.Files["default.aspx"];
Get the Webpart Manager
//get the web part manager
SPLimitedWebPartManager theMan = thePage.GetLimitedWebPartManager
(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
Then create a webpart instance
//add a new ThisWeekInPictures web part
ThisWeekInPicturesWebPart wpPix = new ThisWeekInPicturesWebPart();
wpPix.ImageLibrary = "Shared Pictures";
wpPix.Title = "My Pictures";
Then a Webpart Action
WebPartAction wpa = new WebPartAction(wpPix, WebPartAction.ActionType.Add,
"MiddleRightZone", 10)
And finnally add the webpart ...
theMan.AddWebPart(wpa.wp, wpa.zoneID, wpa.zoneIndex);
And it's done ... a new webpart in our page, I hope this helps ;)
For more informations please check this article http://blogs.msdn.com/sharepoint/archive/2007/03/22/customizing-moss-2007-my-sites-within-the-enterprise.aspx
Paulo Oliveira
2009-08-31 14:08:34