Skip Ribbon Commands
Skip to main content

Anders Rask on SharePoint


Anders Rask on SharePoint > Posts > Web Templates + Content Type Hub = beware!
March 06
Web Templates + Content Type Hub = beware!
Recently I have run into problems when provisioning site collections that uses Web Templates, and I want to do something with the content types during provisioning. The problem only arises on fast servers such as production environments, not on my slower (not slow, mind you ;-) virtualized development environments. This is the kind of behaviour that brings back memorys about race condition issues you would see in SharePoint 2007 when doing site definitions and feature stapling.
The problem is that the content types are published by the Content Type Hub, and they simply are not available in the thread I am given in the feature receiver.
Normally it is enough to make sure that the TaxonomyFieldAdded feature is called before the custom feature. TaxonomyFieldAdded isn't perhaps the best naming for that feature, since it does alot of other stuff: Declaratively it provisions custom actions for term store management and gives access to Content Type hub publishing and error logs in site settings, and adds Enterprise Keyword link from List Settings. In the feature receiver, one of the things it does is call the same code as the Content Type Hub publishing timer job (you can see timer job and feature receiver share the ProcessSite method in screenshot below). ProcessSite saves data from Content Type Hub as a CAB file in the local temp directory of the user running the code. The cab file is then imported into the new site collection. Export and import is done using the PRIME API (also used for content publishing).
If you just want to get a new site collection with content types published from the hub, this works fine. The problem arises when you have another feature that during site collection provisioning wants to access the content types in feature receiver code. On my development machines and our local virtualized environments this works fine, but on two occasions on fast production sites it fails!
This is weird, as ONET.XML defined features are synchronous (I confirmed this by setting Taxonomy ULS logging to Verbose and checking that my feature receiver wrote an entry in the ULS log just after the last entry by ProcessSite ("ContentTypeSynchronize finally for proxy {0} for site {1}.").
I have seen other attempts to fix this, but really it should be enough to ensure that the TaxonomyFieldAdded feature is added before any feature that uses content types from SPWeb.AvaliableContentTypes but apparently the thread given through the properties of the event receiver sometimes (read: for fast sites) havent picked up on the added content types. I havent digged into the PRIME API to check, but I guess importing/exporting CAB files just sometimes isn't fast enough...
Ok, so how to fix this? After spending some time in Reflector and ULSViewer, I thought of a very neat and simple solution: I just spawn a new thread by instantiating a new SPSite based on the ID of the SPSite given in the feature receiver properties. So far my testing reveals that this fixes the issue:
public class MyEventReceiver : SPFeatureReceiver 
    public override void FeatureActivated(SPFeatureReceiverProperties properties) 
        if (properties.Feature.Parent is SPSite) 
            SPSite featureSite = properties.Feature.Parent as SPSite; 
            Guid siteId = featureSite.ID; 
            // NOTE: get a new site/web context 
            // this solves race conditions / threading issue with TaxonomyFieldAdded feature: 
            // if we access SPWeb.AvailableContentTypes collection for CTs published through CT hub 
            // the content types are not there even though TaxonomyFieldAdded feature receiver has 
            // manually kicked off CT hub synchronization synchronously (not timer job and in ONET.XML features) 
            using (SPSite site = new SPSite(siteId)) 
                using (SPWeb web = site.RootWeb) 
                    // do stuff 
Feel free to comment on similar issues, or technical insights i may have missed ;-)


SP 2013 in web template

Bear with me as I am rather new to this but, I am working in 2013 trying to setup a web template that depends upon the availability of content hub columns.  If I understand correctly, this goes into the event receiver for a site scope feature enable by/within the solution for the web template?  Once run - it will ensure my content types are available for features that run after this site scoped feature?
 on 6/19/2014 6:30 PM

nice one

I am getting here about something happened dastardly deed and making the better popeye would be perfect. The wondered brought muscles to appear and great technical recliner and impressive rescue us some.
 on 7/3/2018 5:16 AM

The problem arises

The problem arises when you have another feature that during site collection provisioning wants to access the content types in feature receiver code.
 on 9/19/2018 1:36 AM

After decades of establishing plans about the suitable reforms

After decades of establishing plans about the suitable reforms strategies for such a country like Liberia, Nellie Mayshak, the president of the Canaf Consulting Associates,finally made her way on regaining the balance of the country.
 on 10/3/2018 4:31 AM

While the strategy, positioning in the market and coming up with

While the strategy, positioning in the market and coming up with a long-term plan is very important, we must focus on creating satisfied customers, recommends Certus Trading Reviews.
 on 10/3/2018 4:32 AM


 on 11/27/2018 3:17 AM


Are you serching whatsapp new status ? You are on the right status website. We give you a big collection of whatsapp new status
 on 1/30/2019 11:21 AM


 on 2/7/2019 8:02 AM

My point of view

Good website posted here.
 on 2/15/2019 9:56 AM

thank for share

I will have to follow you, the information you bring is very real, reflecting correctly and objectively, it is very useful for society to grow together.
 on 6/28/2019 3:02 AM
1 - 10Next

Add Comment


Body *

Migrated Source URL

Commentator Name

Commentator Email

BotCheck *

Are you human? What is the sum of fifty-two minus ten?