1

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.

2 Answers 2

0

The answer to your problem is CSOM. The Client side object model allows for remote calls using either managed code or JavaScript. This means that you don't need the server side libraries.

This post explains the procedure for creating publishing pages through CSOM.

http://blogs.msdn.com/b/prasannabalajim/archive/2013/05/20/how-to-add-a-publishing-page-in-sharepoint-2013-using-client-object-model.aspx

Another example:Is it possible to programmatically add page items to Enterprise Wiki using CSOM or REST?

0

Since you are developing a client WPF application you could consider the following 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

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

References

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.