Below is a simple code snippet that you can wrap in a site scoped feature to publish and subscribe to content types from a content type hub.
As a precaution, please be careful to use this code snippet in production unless you have thoroughly tested it.
This is very useful when you are provisioning a site using a web template. This code, wrapped in a site scoped feature will push the published content types from the hub to the new site. You don’t have to wait and guess when the content types will be available in the newly provisioned site.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb spWeb = properties.Feature.Parent as SPWeb;
SPSite spSite = spWeb.Site;
string strCTHubUrl = properties.Feature.Properties["CTHubUrl"].Value;
using (SPSite siteCtHub = new SPSite(strCTHubUrl))
using (SPWeb webCtHub = siteCtHub.RootWeb)
if (ContentTypePublisher.IsContentTypeSharingEnabled(siteCtHub))
ContentTypePublisher ctPublisher = new ContentTypePublisher(siteCtHub);
IEnumerable<SPContentType> spContentTypes =
webCtHub.AvailableContentTypes
.Cast<SPContentType>().Where(ct => ct.Group == "MyGroup");
foreach (SPContentType spContentType in spContentTypes)
ctPublisher.Publish(spContentType);
Thread.Sleep(100);
}
SPWebApplication spWebApp = spSite.WebApplication;
SPService saMMS = spWebApp.Farm.Services.Cast<SPService>()
.FirstOrDefault(s => s.TypeName == "Managed Metadata Web Service");
SPJobDefinition ctHubTimerJob = null;
//Run the Content Type Hub Job
//This timer job is associated with the MMS service application
ctHubTimerJob = saMMS.JobDefinitions.Cast<SPJobDefinition>()
.FirstOrDefault(jd => jd.Title == "Content Type Hub");
if (ctHubTimerJob == null)
saMMS = spWebApp.Farm.Services.Cast<SPService>()
.FirstOrDefault(s => s.TypeName ==
"Microsoft SharePoint Foundation Timer");
.FirstOrDefault(jd => jd.Title ==
"Content Type Hub");
DateTime startTime = DateTime.Now.ToUniversalTime();
ctHubTimerJob.RunNow();
while ((from SPJobHistory j in saMMS.JobHistoryEntries
where j.JobDefinitionId ==
ctHubTimerJob.Id && j.StartTime >
startTime select j).Any() == false)
//Run the Content Type Subscriber Job
//This timer job is associated with the subscriber's web application
SPJobDefinition ctHubSubTimerJob = spWebApp.JobDefinitions.Cast<SPJobDefinition>()
.FirstOrDefault(jd => jd.Title == "Content Type Subscriber");
startTime = new DateTime();
startTime = DateTime.Now.ToUniversalTime();
ctHubSubTimerJob.RunNow();
while ((from SPJobHistory j in spWebApp.JobHistoryEntries
ctHubSubTimerJob.Id && j.StartTime >
The code performs the following:
This code can now be used in your web template onet.xml and activated before all of your lists, libraries are created using the referenced subscribed content types.
As a precaution, please be careful to use this code snippet in production unless you have thoroughly tested it. Also don’t forget to handle the exceptions. This code snippet does not handle any exceptions that may arise due to the timer job or any other issues.
Thanks...Helped me a lot!!!