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

I was wondering, what APIs are available if I'm writing a farm-based sharepoint solution, for sharepoint 2013.

Basically I read about this thing called Page Layouts, and they seem like page templates. I want to generate an instance of it through my own code, in C#, how is that possible?

Thanks.

share|improve this question

1 Answer 1

Get the relevant Publishing web which contains GetAvailablePageLayouts, and find the one which you should use to create your page. See sample code:

PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);    
string pageName = "YourCustomPageLayout.aspx";    
PageLayout[] pageLayouts = publishingWeb.GetAvailablePageLayouts();    
PageLayout currPageLayout = pageLayouts[0];    
PublishingPageCollection pages = publishingWeb.GetPublishingPages();    
PublishingPage newPage = pages.Add(pageName,currPageLayout);    
newPage.ListItem[FieldId.PublishingPageContent] = "This is test content";    
newPage.ListItem.Update();    
newPage.Update();    
newPage.CheckIn("This is just a comment");

Also check this example

Update:

You can use the SPLimitedWebPartManager object to get the Web Part whose property you want to edit.

using (SPLimitedWebPartManager webPartManager = site.GetLimitedWebPartManager("yourpage.aspx", PersonalizationScope.Shared))
webPartManager.WebParts[0].webpartProperty = "Some example";
// Save the changes to the Web Part.
webPartManager.SaveChanges(webPartManager.WebParts[0]);
share|improve this answer
    
Thanks for the reply, but it looks like PublishingWeb isn't available in Sharepoint solutions projects? At least I can't even find the reference assembly to add to the project, is it only available in Apps? –  user17061 May 28 '13 at 1:45
1  
You need to add a reference to C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Publishing.dll –  Per Jakobsen May 28 '13 at 7:06
    
Cool thanks, another question I have is. With the way I'm approaching it, I'm hoping to have a base page layout that has where all the webparts are structured, how do I dig out a web-part from inside the page layout to configure some properties on it, or replace a query that it's pointing to? Thanks –  user17061 May 28 '13 at 20:19
    
See my updated response –  Falak Mahmood May 29 '13 at 7:42
    
This is a great solution; one I've been using and it works great. One thing you may want to add is to Publish the page as well newPage.Publish("Comment");. By far that's most annoying part of SharePoint Publishing Pages, the Check-In then Publish 1-2 punch. Don't know why they just can't offer a combo action. Has anyone found a better way to pick out a specific page layout other than LINQing the list to find it by name? –  EHorodyski Jun 21 '13 at 13:13

Your Answer

 
discard

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