简体   繁体   中英

How do you create a page using the API for sharepoint 2010?

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"

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();
    }

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();
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM