Sharepoint 2010 通过客户端对象模型创建SharePoint 2010页面

Sharepoint 2010 通过客户端对象模型创建SharePoint 2010页面,sharepoint-2010,sharepointdocumentlibrary,sharepoint-clientobject,Sharepoint 2010,Sharepointdocumentlibrary,Sharepoint Clientobject,我试图通过客户端对象模型在Sharepoint 2010页面库中创建页面,但找不到任何有关如何创建页面的示例。我尝试了两种方法: 第一种方法是将页面库视为列表,并尝试添加列表项 static void createPage(Web w, ClientContext ctx) { List pages = w.Lists.GetByTitle("Pages"); //ListItem page = pages.GetItemById(0); ListItemCreation

我试图通过客户端对象模型在Sharepoint 2010页面库中创建页面,但找不到任何有关如何创建页面的示例。我尝试了两种方法:

第一种方法是将页面库视为列表,并尝试添加列表项

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();            
}
正如预期的那样,此操作失败,并显示错误消息:

To add an item to a document library, use SPFileCollection.Add()
我尝试的下一种方法是将其添加为文件。问题是FileCreationInformation对象需要一个字节数组,我不确定传递给它什么

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();    
}
上面的代码将在Pages库中添加一个项目,但打开该文件将显示一个空白页面,我无法编辑该页面。通过阅读各种主题,我怀疑可能只能通过服务器端代码添加页面。有什么想法吗

谢谢

问题是 FileCreationInformation对象为 需要一个字节数组,但我没有 确定要传递什么

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();    
}
您可以使用任何方法将页面内容转换为字符串(从文件读取、使用StringBuilder创建等),然后使用

System.Text.Encoding.ASCII.GetBytes()

首先,在SharePoint2010中,不支持通过客户端对象模型(CSOM)发布API。但是您可以考虑下面的方法来演示如何使用SharePoint 2010 CSOM.</P>创建发布页面。 如何使用SharePoint 2010 CSOM创建发布页面
我想我的问题用词不对。我希望客户端对象模型中有一种方法可以用于创建页面,类似于服务器端的方法,即
PublishingPage newPage=nppublishingweb.GetPublishingPages().Add(fileName,pageLayout)。然而,你的回答促使我从另一个角度看待它。我所做的是手动创建一个页面,然后下载它。然后,我可以告诉页面内容必须是什么,这样我就可以将它们编码为字节,并使用最初发布的代码以编程方式传递它们。谢谢
using (var ctx = new ClientContext(url))
{ 
     ctx.Credentials = new NetworkCredential("username", "password", "domain");
     CreatePublishingPage(ctx, "Pages", "Greetings.aspx", "Welcome to SharePoint!"); 
}