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();
}
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!!!
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();
}
精彩评论