Creating simple pages for SharePoint-2013 remotely from WPF

百般思念 提交于 2019-12-24 03:55:07

问题


I have no experience working with share point. I have a simple C# WPF application that should connect to SharePoint server and programatically create some pages based on layouts or update existing ones. The sharepoint server is not installed on my machine. I am using SharePoint client dlls locally from

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

The only steps that is done and working is connecting with credentials, getting the list of folders and pages. I have some difficulties on creating and reading the content of the pages. So, what is the best way of doing it and is it possible to do it remotely?

I was trying to add server side libraries and come up with similar problem in a below question.

This question is saying that

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

This link have good example of how to do it but i can't get necessary classes.


回答1:


Since you are developing a client WPF application you could consider the following client-side APIs:

  • Managed Client Object Model (CSOM)
  • RESTful Web Services
  • SharePoint SOAP Web Services

Since you mentioned in question that you've already installed SharePoint Server 2013 Client Components SDK, below is demonstrated how utilize CSOM in WPF application.

How to manage a Publishing pages using SharePoint 2013 CSOM API

SharePoint 2013 introduced support for publishing pages in SharePoint 2013 CSOM, the following class demonstrates how to create and read publishing pages:

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 app

Prerequisites: make sure all the required SharePoint CSOM assemblies are referenced in WPF project as demonstrates on picture below

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>

Load pages

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

Create a publishing page

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

where

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

Result

Update

How to create a publishing page and specify page properties:

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

Usage

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

References

  • Choose the right API set in SharePoint 2013
  • NET client API reference for SharePoint 2013


来源:https://stackoverflow.com/questions/28122532/creating-simple-pages-for-sharepoint-2013-remotely-from-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!