none
Need to add pages to sitemap programmatically. RRS feed

  • Question

  • We are using the Microsoft.SharePoint.Client SDK to dynamically generate pages from content in SharePoint Lists. These pages are created in the root of the sitemap but we would like to be able to add them as a node in the sitemap. We cannot find examples of this approach in MSDN or other developer networks and are wondering if this is possible at all. Here is an excerpt of our code:

    // Get Publishing Page Layouts
    List publishingLayouts = context.Site.RootWeb.Lists.GetByTitle("Galerie met basispagina's");
    allItems = publishingLayouts.GetItems(CamlQuery.CreateAllItemsQuery());
    context.Load(allItems, items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName == layoutName));
    context.ExecuteQuery();
    ListItem layout = allItems.Where(x => x.DisplayName == layoutName).FirstOrDefault();
    context.Load(layout);
    context.ExecuteQuery();

    // Create a publishing page
    PublishingPageInformation publishingPageInfo = new PublishingPageInformation();
    publishingPageInfo.Name = pageName + ".aspx";
    publishingPageInfo.PageLayoutListItem = layout;
    PublishingPage publishingPage = web.AddPublishingPage(publishingPageInfo);
    context.ExecuteQuery();

    Any guidance would be appreciated.

    Thanks in advance.

    Wednesday, April 22, 2015 6:08 PM

All replies

  • Hi,

    Please check whether the current web object is a root web, try to use the node web.

    Here is a method to create a publishing page for your reference:

    public void AddPublishingPage(ClientContext context, string pageName)
    {
        // Get current web
        Web webSite = context.Web;
        context.Load(webSite);
        PublishingWeb web = PublishingWeb.GetPublishingWeb(context, webSite);
        context.Load(web);
    
        if (web != null)
        {
            // Get Pages Library
            List pages = context.Site.RootWeb.Lists.GetByTitle("Pages");
            ListItemCollection existingPages = pages.GetItems(CamlQuery.CreateAllItemsQuery());
            context.Load(existingPages, items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName == pageName));
            context.ExecuteQuery();
    
            // Check if page already exists
            if (existingPages != null && existingPages.Count > 0)
            {
                // Page already exists
            }
            else
            {
                // Get Publishing Page Layouts
                List publishingLayouts = context.Site.RootWeb.Lists.GetByTitle("Master Page Gallery");
                ListItemCollection allItems = publishingLayouts.GetItems(CamlQuery.CreateAllItemsQuery());
                context.Load(allItems, items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName == "BlankWebPartPage"));
                context.ExecuteQuery();
                ListItem layout = allItems.Where(x => x.DisplayName == "BlankWebPartPage").FirstOrDefault();
                context.Load(layout);
    
                // Create a publishing page
                PublishingPageInformation publishingPageInfo = newPublishingPageInformation();
                publishingPageInfo.Name = pageName;
                publishingPageInfo.PageLayoutListItem = layout;
                PublishingPage publishingPage = web.AddPublishingPage(publishingPageInfo);
                publishingPage.ListItem.File.CheckIn(string.Empty, CheckinType.MajorCheckIn);
                publishingPage.ListItem.File.Publish(string.Empty);
                publishingPage.ListItem.File.Approve(string.Empty);
                context.Load(publishingPage);
                context.Load(publishingPage.ListItem.File, obj => obj.ServerRelativeUrl);
                context.ExecuteQuery();
            }
        }
    }
    

    More information:

    How to add a Publishing Page in SharePoint 2013 using Client Object Model

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

    If I misunderstanding your requirement, I suggest you provide more information for further research.

    Best Regards,

    Dennis Guo


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Thursday, April 23, 2015 9:51 AM
    Moderator
  • Hi Dennis,

    Thanks for your answer. The example you provided looks pretty much like the example I used to write my code. And it works fine. However, as I mentioned in my original question, the new pages will be located in the root and I would like to be able to position a page deeper into the sitemap, for example in mysite.com/news/my_page.

    You mentioned "Please check whether the current web object is a root web, try to use the node web." Can you provide an example of that? It sounds promising. Provide that I have a reference to an existing page in the sitemap, how do I get a reference to its sitemap-node and how do I add the new page as a child node to this existing node? I am new to this aspect of the Client Object Model, so any help in this would be appreciated.

    By the way, we are using the Public Website feature of SharePoint Online. I understand that there may be some limitations to managed navigation but I've come this far so I figured that the final step should be possible.

    Rik

    Thursday, April 23, 2015 10:24 AM