2

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.

Community
  • 1
  • 1
Mirzodaler
  • 455
  • 5
  • 10
  • It looks like the libraries you mention modify a local instance of SharePoint. But you can certainly accomplish what you want: if you control the server, write a web service that uses this library, then call it from the client. Maybe SharePoint has a service like that already. If none of that is possible, you can always do it with web requests / tcp. If there is a way to do something as a human user, then there is always a way to do it in code, unless there is some kind of captcha involved. – svinja Jan 24 '15 at 10:23

1 Answers1

4

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

enter image description here

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

enter image description here

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

Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193