如何:自定义内容类型联合
上次修改时间: 2010年4月21日
适用范围: SharePoint Server 2010
内容类型联合有助于确保列表的一致性,使列表使用相同的内容类型(从而使用相同的列)。在 Microsoft Office SharePoint Server 2007 中,将在网站集级别设置内容类型。通过在 Microsoft SharePoint Server 2010 中使用内容类型联合,可利用 Managed Metadata Service 应用程序在服务器场内或服务器场外共享内容类型。
可使用对象模型来自定义内容类型联合,以创建一个新内容类型,发布该内容类型,然后取消发布该内容类型。以下代码示例介绍了 ContentTypePublisher 类。在设置一个 Managed Metadata Service 应用程序及其代理,并将某个网站指定为服务应用程序中的集线器后,使用此类可在集线器网站上发布或取消发布内容类型。使用此类还可验证该集线器网站上的内容类型是否已发布以及是否已在集线器上启用元数据集线器功能。
在很多情况下,集线器将充当一个发布者,而 Web 应用程序或网站集将订阅该集线器发布的内容。在发布内容类型后,订户可通过计时器作业推入内容类型。用户导航到"管理中心"页,可在此页中选择一个 Web 应用程序作为集线器。单个 Web 应用程序也可以订阅选定的集线器作为其共享应用程序数据(包括内容类型)的提供程序。
示例
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
{
/// Demostrates how to use the ContentTypePublisher class by creating a
/// content type and then publishing it and unpublishing it.
/// Before this method is called, a Managed Metadata service application
/// should be created and a hub should be designated on the service application.
/// <param name="hubSite">
/// The site that is 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 the 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 to see whether 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 to see whether 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 whether 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");
}
}
}
}