How do I add web part on publishing pages. The publishing pages are in Pages list. I like to add the built in contact web part. Also like to add few web part and backend of these web part are list sitting inside the same web.
-
Is this SP2007 way? using Microsoft.SharePoint; using Microsoft.SharePoint.WebPartPages; SPWeb web = site.OpenWeb(); SPList list = web.Lists["List Name"]; SPFile webPartPage = web.Files["default.aspx"]; //or web.GetFile() depending on where the page is SPLimitedWebpartManager webpartManager = webPartPage.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalisationScope.Shared); ListViewWebPart wp = new ListViewWebPart(); wp.ListName = list.ID.ToString("B").ToUpper(); wp.ViewGuid = list.Views["View name"].ID.ToString("B").ToUpper(); weppartManager.AddWebPart(wp, "Zone name", 0);– RoxanneCommented May 11, 2011 at 14:01
-
2Roxanne: This is the second time you've done this - did you mean to edit your post and add that code?– James LoveCommented May 11, 2011 at 17:48
Add a comment
|
1 Answer
SPWeb web = ...
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
PublishingPage page = pWeb.GetPublishingPage(<pageUrl>);
SPFile file = page.ListItem.File;
SPLimitedWebPartManager mgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
mgr.AddWebPart(<webPart>, <zoneId>, <zoneIndex>);
Do not forget to dispose where it is necessary.
-
Actually, I want to add the build in WP thats in wp gallery "contact details" or contactwp.dwp– RoxanneCommented May 11, 2011 at 18:11