简体   繁体   中英

Adding Web Parts to the Page Instance based on Page Layout using Feature

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM