从 WPF 远程为 SharePoint-2013 创建简单页面 [英] Creating simple pages for SharePoint-2013 remotely from WPF

查看:48
本文介绍了从 WPF 远程为 SharePoint-2013 创建简单页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

的 SharePoint 客户端 dll<块引用>

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

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

我试图添加服务器端库并在下面的问题中提出了类似的问题.

  • NET 客户端 API 参考SharePoint 2013
  • 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.

    解决方案

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

    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

    这篇关于从 WPF 远程为 SharePoint-2013 创建简单页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆