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

发布于 2024-11-11 14:47:36 字数 1125 浏览 2 评论 0原文

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

第一种是将Pages库视为列表并尝试添加列表项。

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

上面的代码将在页面库中添加一个项目,但打开文件会显示一个我无法编辑的空白页面。通过阅读各种主题,我怀疑可能只能通过服务器端代码添加页面。有什么想法吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我家小可爱 2024-11-18 14:47:36

问题是
FileCreationInformation 对象是
期待一个字节数组,但我不是
确定要传递给它什么。

您可以使用任何想要将页面内容转换为字符串的方法(从文件中读取它,使用 StringBuilder 创建它等),然后使用将字符串转换为字节数组

System.Text.Encoding.ASCII.GetBytes()

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

凉月流沐 2024-11-18 14:47:36

首先,SharePoint 2010 中不支持通过客户端对象模型 (CSOM) 发布 API。但您可以考虑使用以下方法来演示如何使用 SharePoint 2010 CSOM 创建发布页面。

如何使用 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();
}

用法

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

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!"); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文