3

I have just started using Sharepoint Foundation 2010 and I'm trying to write a function from c# to add pages to a site.

I got some code working to create a new site, but I can't seem to find any documentation about adding a page to an existing site using the client object model.

This is probably a simple question but if anyone can help me out I would appreciated it.

Thanks.

Update

This is what I have so far:

private void createPage()
    {
        ClientContext context = new ClientContext(url);
        Site siteCollection = context.Site;
        Web site = context.Web;

        List pages = site.Lists.GetByTitle("Pages");
        FileCreationInformation fileCreateInfo = new FileCreationInformation();
        fileCreateInfo.Url = "NewPage";
        fileCreateInfo.Content = System.Text.Encoding.ASCII.GetBytes("Test");
        context.Load(pages.RootFolder.Files.Add(fileCreateInfo));

        context.ExecuteQuery();
        context.Dispose();
    }

But I get a server exception "List 'Pages' does not exist at site with URL"

Matt
  • 322
  • 6
  • 12

3 Answers3

2

This is what I eventually did to add my page. Essentially I just needed to find the appropriate list title. These are just the names of the Document Libraries on the site.

private void createPage()
    {
        ClientContext context = new ClientContext(URL);
        Site siteCollection = context.Site;
        Web site = context.Web;

        List pages = site.Lists.GetByTitle("Site Pages");

        Microsoft.SharePoint.Client.
        FileCreationInformation fileCreateInfo = new FileCreationInformation();
        fileCreateInfo.Url = "NewPage.aspx";
        context.Load(pages.RootFolder.Files.Add(fileCreateInfo));

        context.ExecuteQuery();
        context.Dispose();
    }
Matt
  • 322
  • 6
  • 12
  • I know this is old, but when I try this code, I get an error: `The 'parameters.Content' argument cannot be null. Parameter name: parameters.Content` – jasonscript Mar 27 '14 at 03:39
0

This Code works for me. It Creates an Page("NewPage.aspx") With the Content Test.

private void createPage()
    {
        ClientContext context = new ClientContext(URL);
        Site siteCollection = context.Site;
        Web site = context.Web;

        List pages = site.Lists.GetByTitle("Site Pages");

        Microsoft.SharePoint.Client.
        FileCreationInformation fileCreateInfo = new FileCreationInformation();
        fileCreateInfo.Url = "NewPage.aspx";
        fileCreateInfo.Content = System.Text.Encoding.ASCII.GetBytes("Test");
        context.Load(pages.RootFolder.Files.Add(fileCreateInfo));

        context.ExecuteQuery();
        context.Dispose();
    }
0

If you are talking about creating a new site page I would recommend looking at this tutorial:

http://blogs.msdn.com/b/kaevans/archive/2010/06/28/creating-a-sharepoint-site-page-with-code-behind-using-visual-studio-2010.aspx

Take a second and make sure you actually want to add this through code. As someone who recently started development with SharePoint I can tell you that there is a fairly steep learning curve when working with the Object Model. Also, tasks that are easy to do through the UI may be quite difficult using code.

Good luck!!!

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486