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:
 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 ;-)

Comments

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.
https://www.assignmenthelperuk.co.uk/buy-assignment-online
 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. https://soundcloudtomp3.io
 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.

http://www.investmentwatchblog.com/nellie-mayshak-her-takeaways-from-her-battle-against-corruption-in-nigeria/
 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.

https://ideamensch.com/matt-choi/
 on 10/3/2018 4:32 AM

Zushikatochi

 on 11/27/2018 3:17 AM

Whatsapp

Are you serching whatsapp new status ? You are on the right status website. We give you a big collection of whatsapp new status
http://testsoc-invamam.1gb.ru/home.php?mod=space&uid=103900
 on 1/30/2019 11:21 AM

Great

 on 2/7/2019 8:02 AM

My point of view

Good website posted here. http://andersrask.sharepointspace.com/
 on 2/15/2019 9:56 AM

Add Comment

Title


Body *


Migrated Source URL


Commentator Name


Commentator Email


BotCheck *


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

Attachments