ContentTypePublisher Class

Publishes or unpublishes a content type on the hub site.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher

Namespace:  Microsoft.SharePoint.Taxonomy.ContentTypeSync
Assembly:  Microsoft.SharePoint.Taxonomy (in Microsoft.SharePoint.Taxonomy.dll)

Syntax

'Declaration
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
Public NotInheritable Class ContentTypePublisher
'Usage
Dim instance As ContentTypePublisher
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public sealed class ContentTypePublisher

Remarks

After a managed metadata service application and its proxy are provisioned and a site has been designated as the hub on the service application, use this class to publish or unpublish the content types on the hub site.

Also, use this class to verify whether a content type on this hub site is published, and to verify whether the MetadataHub feature is enabled on the hub.

Examples

using System;
using System.IO;
using System.Globalization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint.Taxonomy.ContentTypeSync;

namespace Microsoft.SDK.SharePointServer.Samples
{
    public static class ContentTypeSyndicationSamples
    {
        /// <summary>
        /// Demostrates how to use ContentTypePublisher class by creating a new
        /// content type and then publish it and unpublish it.
        /// Before this method is called, a Managed Metadata service application
        /// should be created and a hub has been designated on the service application.
        /// </summary>
        /// <param name="hubSite">
        /// The site that has been designated as the hub
        /// </param>
        public static void UseContentTypePublisher(SPSite hubSite)
        {
            // Get the root web of the site
            SPWeb rootWeb = hubSite.RootWeb;

            // Create a new content type based on Document content type
            SPContentType docCt = rootWeb.ContentTypes["Document"];
            Random random = new Random();
            string newCTName = "Test content type " + random.Next().ToString();
            SPContentType newCt = new SPContentType(docCt,
                rootWeb.ContentTypes,
                newCTName);

            rootWeb.ContentTypes.Add(newCt);

            Console.WriteLine(newCTName + " has been created.");

            // Check if the site is a valid hub site
            if (ContentTypePublisher.IsContentTypeSharingEnabled(hubSite))
            {
                ContentTypePublisher publisher = new ContentTypePublisher(hubSite);

                Console.WriteLine("Publishing the content type. ");
                publisher.Publish(newCt);

                // Check if this content type has been published
                if (publisher.IsPublished(newCt))
                {
                    Console.WriteLine(newCTName + " is a published content type.");
                }

                Console.WriteLine("Unpublishing the content type. ");
                publisher.Unpublish(newCt);

                // Check if this content type is still published
                if (!publisher.IsPublished(newCt))
                {
                    Console.WriteLine(newCTName + " is not a published content type.");
                }
            }
            else
            {
                // The provided site is not a valid hub site
                Console.WriteLine("This site is not a valid hub site");
            }

        }
    }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

ContentTypePublisher Members

Microsoft.SharePoint.Taxonomy.ContentTypeSync Namespace