1
    var pWeb = PublishingWeb.GetPublishingWeb(context, context.Web);
        context.Load(pWeb);
        if (pWeb != null)
        {
           <This condition is true for Team and Publishing Site Both>
        }

This code is not working, it's going in if condition, if we provide Team site context as well..

        using (var clientContext = new ClientContext(webUrl))
        {


            var pWeb = PublishingWeb.GetPublishingWeb(clientContext, clientContext.Web);

            clientContext.Load(pWeb);

            if (pWeb != null)
            {
                Console.WriteLine("Web : " + webUrl + " is a publishing web");

            }
            else
            {
                Console.WriteLine("Web : " + webUrl + "is not a publishing web");
            }
        }

I have already included - Microsoft.SharePoint.Client.Publishing .. REFERENCE AND NAMESPACE in my project

I am using SharePoint 2013. Kindly suggest what is the issue here? why it's going in if condition for team site.

3

How to determine whether site is a Publishing site using CSOM:

public static bool IsPublishingWeb(Web web)
{
     var ctx = web.Context;
     var propName = "__PublishingFeatureActivated";
     //Ensure web properties are loaded
     if (!web.IsObjectPropertyInstantiated("AllProperties"))
     {
         ctx.Load(web, w => w.AllProperties);
         ctx.ExecuteQuery();
     }
     //Verify whether publishing feature is activated 
     if (web.AllProperties.FieldValues.ContainsKey(propName))
     {
         bool propVal;
         Boolean.TryParse((string)web.AllProperties[propName], out propVal);
         return propVal;
     }
     return false;
}

Compatible with SharePoint 2010/2013

Usage

   using (var ctx = GetSPOContext(webUri,userName,password))
   {
        var web = ctx.Web;
        if(IsPublishingWeb(web))
        {
           //...
        }
   }
  • +1, Definitely a cleaner approach than mine. – Nadeem Yousuf Feb 27 '15 at 12:09
2

You are missing ClientContext.ExecuteQuery. After clientContext.Load(pWeb); use clientContext.ExecuteQuery();

UPDATE

It appears that PublishingWeb.GetPublishingWeb creates a PublishingWeb object whether the site is a publishing site or not. As you may be aware the PublishingWeb has a Pages library, the workaround can be to check whether a library with this name exists or not in the Web. Example:

using (var clientContext = new ClientContext("http://aissp2013/sites/T1"))
{
    PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(clientContext, clientContext.Web);
    ListCollection listCollection = pWeb.Web.Lists;
    clientContext.Load(listCollection, lists => lists.Include(list => list.Title).Where(list => list.Title == "Pages"));
    clientContext.ExecuteQuery();
    if (listCollection.Count > 0)
    {
        Console.WriteLine("Web is publishing");
    }
    else
    {
        Console.WriteLine("Web is not publishing");
    }
}

However, this is not a foolproof method as user can create Document Library named Pages in a site.

UPDATE 2

A better way of finding whether a web is publishing or not is to check whether Publishing Feature is activated on it or not. Here is the code:

using (var clientContext = new ClientContext("http://aissp2013/sites/T1"))
{
    bool isPublishingWeb = false;
    Site site = clientContext.Site;
    FeatureCollection features = site.Features;
    clientContext.Load(features);
    clientContext.ExecuteQuery();
    Guid publishingFeatureID = new Guid("f6924d36-2fa8-4f0b-b16d-06b7250180fa");
    foreach (Feature feature in features)
    {
        if (feature.DefinitionId == publishingFeatureID)
        {
            isPublishingWeb = true;                       
            break;
        }
    }
    if (isPublishingWeb)
    {
        Console.WriteLine("Web is publishing!");
    }
    else
    {
        Console.WriteLine("Web is not publishing!");
    }
    Console.Read();
}
  • Hello Nadeem, Thanks for the quick response. I have added suggested piece of code as well, but no luck :( using (var clientContext = new ClientContext(webUrl)) { var pWeb = PublishingWeb.GetPublishingWeb(clientContext, clientContext.Web); clientContext.Load(pWeb); clientContext.ExecuteQuery(); if (pWeb != null) { Console.WriteLine("Publishing Web"); } else { Console.WriteLine("Non-Publishing Web"); } } – Virendra Kumar Feb 27 '15 at 9:56
  • Is the site a team site. Has the Publishing feature been activated in this site? – Nadeem Yousuf Feb 27 '15 at 9:58
  • No publishing feature is not ON. It's a team site only – Virendra Kumar Feb 27 '15 at 10:00
  • You need to turn on SharePoint Publishing feature in order to use PublishingWeb.GetPublishingWeb – Nadeem Yousuf Feb 27 '15 at 10:02
  • 1
    Thanks @Nadeem. I am also using similar kind of workaround. As we know this solution will fail if we have library with "Pages" name. That's why I asked question in forum. Web web = clientContext.Web; ListCollection listCollection = web.Lists; clientContext.Load(listCollection); clientContext.ExecuteQuery(); bool pagesListAvailability = false; foreach (List oList in listCollection) { if (oList.Title.Equals("Pages")) { pagesListAvailability = true; } } if(pagesListAvailability) {//PublishingWeb}else{//Non-PublishingWeb} – Virendra Kumar Feb 27 '15 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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