繁体   English   中英

如何使用 sharepoint 2010 的 API 创建页面?

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

我刚刚开始使用 Sharepoint Foundation 2010 并且我正在尝试从 c# 编写 function 以将页面添加到站点。

我有一些代码可以创建一个新站点,但我似乎找不到任何有关使用客户端 object model 将页面添加到现有站点的文档。

这可能是一个简单的问题,但如果有人可以帮助我,我将不胜感激。

谢谢。

更新

这是我到目前为止所拥有的:

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

但我得到一个服务器异常“列表'页面'在带有 URL 的站点上不存在”

这就是我最终添加页面的方法。 基本上我只需要找到合适的列表标题。 这些只是站点上文档库的名称。

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

本准则适用于我。 它使用内容测试创建一个页面(“NewPage.aspx”)。

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

如果您正在谈论创建新网站页面,我建议您查看本教程:

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

花点时间确保您确实想通过代码添加它。 作为最近开始使用 SharePoint 进行开发的人,我可以告诉您,在使用 Object Model 时,学习曲线相当陡峭。 此外,通过 UI 很容易完成的任务使用代码可能会非常困难。

祝你好运!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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