2
votes

I am attempting to create pages in a Sharepoint 2010 pages library via the client object model but I cannot find any examples on how to do it. I have tried two approaches:

The first is to treat the Pages library as a list and try to add a list item.

static void createPage(Web w, ClientContext ctx)
{
    List pages = w.Lists.GetByTitle("Pages");
    //ListItem page = pages.GetItemById(0);
    ListItemCreationInformation lici = new ListItemCreationInformation();
    ListItem li = pages.AddItem(lici);
    li["Title"] = "hello";
    li.Update();
    ctx.ExecuteQuery();            
}

As expected, this failed with the error message:

To add an item to a document library, use SPFileCollection.Add()

The next approach I tried was to add it as a file. The problem is that the FileCreationInformation object is expecting a byte array and I am not sure what to pass to it.

static void createPage(Web w, ClientContext ctx)
{
    List pages = w.Lists.GetByTitle("Pages");
    FileCreationInformation file = new FileCreationInformation();
    file.Url = "testpage.aspx";
    file.Content = new byte[0];
    file.Overwrite = true;
    ctx.Load(pages.RootFolder.Files.Add(file));
    ctx.ExecuteQuery();    
}

The piece of code above will add an item in the Pages library but opening the file brings up a blank page which I cannot edit. From reading various topics, I suspect that it may only be possible to add pages via server side code. Any thoughts?

Thanks

2

2 Answers

2
votes

The problem is that the FileCreationInformation object is expecting a byte array and I am not sure what to pass to it.

You could you whatever method you want to get the page contents into a string (read it from a file, create it using a StringBuilder, etc) and then convert the string to a byte array using

System.Text.Encoding.ASCII.GetBytes()

0
votes

First of all, Publishing API is not supported via Client Side Object Model (CSOM) in SharePoint 2010. But you could consider the following approach that demonstrates how to create a publishing page using SharePoint 2010 CSOM.

How to create a publishing page using SharePoint 2010 CSOM

public static void CreatePublishingPage(ClientContext ctx, string listTitle, string pageName, string pageContent)
{
        const string publishingPageTemplate = "<%@ Page Inherits=\"Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c\" %> <%@ Reference VirtualPath=\"~TemplatePageUrl\" %> <%@ Reference VirtualPath=\"~masterurl/custom.master\" %>";
        var pagesList = ctx.Web.Lists.GetByTitle(listTitle);
        var fileInfo = new FileCreationInformation
        {
            Url = pageName,
            Content = Encoding.UTF8.GetBytes(publishingPageTemplate),
            Overwrite = true
        };
        var pageFile = pagesList.RootFolder.Files.Add(fileInfo);
        var pageItem = pageFile.ListItemAllFields;

        if (!ctx.Site.IsPropertyAvailable("ServerRelativeUrl"))
        {
            ctx.Load(ctx.Site);
            ctx.ExecuteQuery();
        }
        pageItem["PublishingPageLayout"] =  string.Format("{0}_catalogs/masterpage/ArticleLeft.aspx, ArticleLeft",ctx.Site.ServerRelativeUrl);
        pageItem["PublishingPageContent"] = pageContent;
        pageItem.Update();
        ctx.ExecuteQuery();
}

Usage

using (var ctx = new ClientContext(url))
{ 
     ctx.Credentials = new NetworkCredential("username", "password", "domain");
     CreatePublishingPage(ctx, "Pages", "Greetings.aspx", "Welcome to SharePoint!"); 
}