SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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.Personal‌​isationScope.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 May 11 '11 at 14:01
Roxanne: This is the second time you've done this - did you mean to edit your post and add that code? – James Love May 11 '11 at 17:48
up vote 2 down vote accepted
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.

share|improve this answer
Actually, I want to add the build in WP thats in wp gallery "contact details" or contactwp.dwp – Roxanne May 11 '11 at 18:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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