0

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.

2
  • 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);
    – Roxanne
    Commented May 11, 2011 at 14:01
  • 2
    Roxanne: This is the second time you've done this - did you mean to edit your post and add that code?
    – James Love
    Commented May 11, 2011 at 17:48

1 Answer 1

2
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.

1
  • Actually, I want to add the build in WP thats in wp gallery "contact details" or contactwp.dwp
    – Roxanne
    Commented May 11, 2011 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.