2

How to check using CSOM if a web has publishing infrastructure and publishing features enabled?

2

In the Server Object Model the method PublishingWeb.IsPublishingWeb is used to verify whether the SPWeb object is a PublishingWeb object:

if (PublishingWeb.IsPublishingWeb(web))
{
    //
}

Regarding Client Object Model (CSOM), it actually depends on what version of SharePoint is used.

SharePoint 2013

In SharePoint 2013 support for Publishing API was introduced.

Microsoft.SharePoint.Client.Publishing namespace:

Provides a large set of classes for developing custom plugins and apps for interacting publishing pages in SharePoint Server 2013

To determine whether Web is a PublishingWeb the PublishingWeb.GetPublishingWeb method could be used:

public static PublishingWeb GetPublishingWeb(
    ClientRuntimeContext context,
    Web web
)

Example:

var pWeb = PublishingWeb.GetPublishingWeb(context, context.Web);
context.Load(pWeb);
if (pWeb != null)
{
   //..
}

SharePoint 2010

Publishing API is not supported via CSOM in SharePoint 2010

To determine whether Web is a Publishing Web the following code could be used:

    //Publishing Web Client Object for  SharePoint 2010 (CSOM)
    public class PublishingWeb
    {

        public PublishingWeb(Web web)
        {
            this._web = web;
        }


        public static bool ReturnIsPublishingWebProperty(Web web)
        {
            EnsureWeb(web);
            return GetProperty(web, "__PublishingFeatureActivated", false);
        }     

        private static void EnsureWeb(Web web)
        {
            var ctx = web.Context;
            if(web.AllProperties.FieldValues.Count == 0)
            {
                ctx.Load(web, w => w.AllProperties);
                ctx.ExecuteQuery();
            }
        }


        private static T GetProperty<T>(Web web,string propName, T defaultPropValue)
        {
            if (!web.AllProperties.FieldValues.ContainsKey(propName))
                return defaultPropValue;
            return (T)Convert.ChangeType(web.AllProperties[propName], typeof(T));
        }

        private static void SetProperty<T>(Web web,string propName, T propValue)
        {
            web.AllProperties[propName] = propValue;
        }


        private Web _web;


    }

Gist

Example:

if (PublishingWeb.ReturnIsPublishingWebProperty(context.Web))
{
   //...
}

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.