Checking if a site is a publishing site programmatically

There are time when you need to check if a site is a “Publishing Site”. Use the following code snippet to check if a site is a publishing site or not

SPSite site = SPContext.Current.Site;

if (PublishingSite.IsPublishingSite(site))
{
    //it is a publishing site
}
else
{
    throw new System.ArgumentException("The SPSite is not a Publishing Site", "site");
}

Similarly, if you want to check for the Publishing Web the use the following snippet

SPWeb web = SPContext.Current.Web;

PublishingWeb publishingWeb = null;
if (PublishingWeb.IsPublishingWeb(web))
{
    publishingWeb = PublishingWeb.GetPublishingWeb(web);
}
else
{
    throw new System.ArgumentException("The SPWeb is not a PublishingWeb", "web");
}

Till then happy coding!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.