从 WPF 远程为 SharePoint-2013 创建简单页面

Creating simple pages for SharePoint-2013 remotely from WPF

我没有使用共享点的经验。我有一个简单的 C# WPF 应用程序,它应该连接到 SharePoint 服务器并以编程方式根据布局创建一些页面或更新现有页面。我的机器上没有安装共享点服务器。我正在本地使用来自

的 SharePoint 客户端 dll

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\ISAPI

唯一完成并有效的步骤是连接凭据,获取文件夹和页面列表。我在创建和阅读页面内容时遇到了一些困难。那么,最好的方法是什么?是否可以远程进行?

我试图添加服务器端库并在下面的问题中遇到了类似的问题。

This question 是说

If you are using SharePoint dll's it will only work on a machine with SharePoint installed.

这个 link 有很好的例子说明如何做到这一点,但我没有必要 类。

由于您正在开发 客户端 WPF 应用程序,您可以考虑以下 client-side APIs:

既然您在问题中提到您已经安装了 SharePoint Server 2013 Client Components SDK,下面将演示如何在 WPF 应用程序中使用 CSOM。

如何使用 SharePoint 2013 CSOM 管理发布页面API

SharePoint 2013引入support for publishing pages in SharePoint 2013 CSOM,下面class演示如何创建和读取发布页面:

class PagesManager
{

    public static ListItemCollection LoadPages(ClientContext ctx)
    {
        var pagesList = ctx.Web.Lists.GetByTitle("Pages");
        var pageItems = pagesList.GetItems(CamlQuery.CreateAllItemsQuery());
        ctx.Load(pageItems);
        ctx.ExecuteQuery();
        return pageItems;
    }



    public static void CreatePublishingPage(ClientContext ctx, string pageName,string pageLayoutName)
    {
        var pubWeb = PublishingWeb.GetPublishingWeb(ctx, ctx.Web);
        var pageInfo = new PublishingPageInformation();
        pageInfo.Name = pageName;
        pageInfo.PageLayoutListItem = GetPageLayout(ctx,pageLayoutName);
        var publishingPage = pubWeb.AddPublishingPage(pageInfo);
        ctx.ExecuteQuery();
    }


    public static ListItem GetPageLayout(ClientContext ctx,string name)
    {
        var list = ctx.Site.GetCatalog((int)ListTemplateType.MasterPageCatalog);
        var qry = new CamlQuery();
        qry.ViewXml = string.Format("<View><Query><Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>{0}</Value></Eq></Where></Query></View>", name);
        var result = list.GetItems(qry);
        ctx.Load(result);
        ctx.ExecuteQuery();
        var item = result.FirstOrDefault();
        return item;
    }
}

WPF 应用

先决条件:确保在 WPF 项目中引用了所有必需的 SharePoint CSOM 程序集,如下图所示

XAML

<Window x:Class="SPManager.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Pages" Height="350" Width="525" Name="PagesWindow">
    <StackPanel>
        <DataGrid  Name="gridPages" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridHyperlinkColumn Header="Page"  Binding="{Binding Path=PageLink}" ContentBinding="{Binding Path=PageName}"/>
                <DataGridTextColumn Header="Creation Date" Binding="{Binding CreationDate}"/>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Create Page" HorizontalAlignment="Right" VerticalAlignment="Bottom" Click="PageCreate_Click"/>
    </StackPanel>
</Window>

加载页面

 private void LoadPages()
 {
     using (var ctx = GetClientContext())
     {
        var items = PagesManager.LoadPages(ctx).Select(i => new
        {
            CreationDate = (DateTime)i["Created"],
            PageName = i["FileLeafRef"],
            PageLink = i["FileRef"].ToString()
        });
        gridPages.ItemsSource = items;
    }
}

创建发布页面

private void PageCreate_Click(object sender, RoutedEventArgs e)
{
    using (var ctx = GetClientContext())
    {
        PagesManager.CreatePublishingPage(ctx, "Hello from WPF.aspx", "BlankWebPartPage.aspx");
    }
}

哪里

private static ClientContext GetClientContext()
{
    var webUri = new Uri(ConfigurationManager.AppSettings["WebUrl"]);
    //var userName = ConfigurationManager.AppSettings["UserName"];
    //var password = ConfigurationManager.AppSettings["Password"];
     return new ClientContext(webUri);
}

结果

更新

如何创建发布页面并指定页面属性:

public static void CreatePublishingPage(ClientContext ctx, string pageName,string pageLayoutName,IDictionary<string,object> properties)
{
    var pubWeb = PublishingWeb.GetPublishingWeb(ctx, ctx.Web);
    var pageInfo = new PublishingPageInformation();
    pageInfo.Name = pageName;
    pageInfo.PageLayoutListItem = GetPageLayout(ctx,pageLayoutName);
    var publishingPage = pubWeb.AddPublishingPage(pageInfo);
    var pageItem = publishingPage.ListItem;
    foreach (var p in properties)
    {
        pageItem[p.Key] = p.Value; 
    }
    pageItem.Update();
    ctx.ExecuteQuery();
}

用法

var pageItemProperties = new Dictionary<string, object>();
pageItemProperties["PublishingPageContent"] = "<h1>Hello from WPF!</h1>";
pageItemProperties["Title"] = "Hello from WPF!";
PagesManager.CreatePublishingPage(ctx, "Hello from WPF.aspx", "BlankWebPartPage.aspx", pageItemProperties);

参考资料