Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

1 Answer 1

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

 
discard

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.