How to check the site is a valid hub site and content type in the hub in SharePoint 2010


In this article we will be seeing how to check whether the site is a valid hub site and how to check whether the content type in the hub site is published or not.

Steps Involved:

Using console application:

  • Open visual studio 2010.
  • Create a new console application.
  • Add the following references.

    o Microsoft.SharePoint.dll
    o Microsoft.SharePoint.Taxonomy.dll
     
  • Add the following namespaces.

    o Using Microsoft.SharePoint;
    o Using Microsoft.Sharepoint.Taxonomy.ContentTypeSync;

C# Code snippet:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy.ContentTypeSync;

namespace EMM
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://servername:20/"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPContentType contentType = web.ContentTypes["_bContentType"];
                    if (ContentTypePublisher.IsContentTypeSharingEnabled(site))
                    {
                        Console.WriteLine(site.Url.ToString() + " is a valid hub site");
                        ContentTypePublisher ctPublisher = new ContentTypePublisher(site);
                        if (ctPublisher.IsPublished(contentType))
                        {
                            Console.WriteLine(contentType.Name + " is published content type");
                        }
                        else
                        {
                            Console.WriteLine(contentType.Name + " is unpublished content type");
                        }
                    }
                    else
                    {
                        Console.WriteLine(site.Url.ToString() + " is not a valid hub site");
                    }
                    Console.ReadLine();
                }
            }
        }
    }
}

Powershell script:

$hubSite=Get-SPSite "http://servername:10/"
$hubWeb=$hubSite.RootWeb
if([Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher]::IsContentTypeSharingEnabled($hubSite))
{
write-host $hubSite.Url " is a valid hub site"
$contentType=$hubWeb.ContentTypes["_bContentType"]
$ctPublisher=New-Object Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher($hubSite)
if($ctPublisher.IsPublished($contentType))
{
write-host $contentType.Name "is published content type"
}
else
{
write-host $contentType.Name "is unpublished content type"
}
}
else
{
write-host $hubSite.Url " is not a valid hub site"
}
  

Up Next
    Ebook Download
    View all
    Learn
    View all