Getting Event Receivers to Fire in SharePoint Content Types

Recently I had a need to hook up an event receiver to a content type. Why not just a list or a list template you might ask?  Well the scenario is the recent move of our Intranet from 2007 whereby we wanted to move our old documents into new libraries but make use of managed Metadata. 

Now we had constructed a custom Managed Metadata Field from scratch and plugged it into our Managed Metadata Service Application and one aspect of doing this is in order for everything to work AND for your Managed Metadata terms to appear as Search Refinements, an event receiver is needed to be registered for each Library that has the Managed Metadata Field.

The problem was that we were using a codeplex tool to migrate the libraries and the tool would create the library. Sure we could add in our Managed Metadata field afterwards via a content type but the event receiver necessary for Search Refinements to work would need to be tied to the Template ID of the list which no doubt would be 101 the standard document library. Didn’t fancy that, as I didn’t want the event receiver firing when there were no Managed Metadata field in a library with template 101.  The answer was to hook up the necessary event receivers to the content type. Here’s how you do it.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="
http://schemas.microsoft.com/sharepoint/&quot;>
    <!– Parent ContentType: Document (0x0101) –>
    <ContentType ID="0x010100401BA56F946B4A2D991C699A44D6116A"
                 Name="Ridgian Document"
                 Group="Ridgian"
                 Description="Ridgian Base Document Content Type"
                 Inherits="TRUE"
                 Version="0">
        <FieldRefs>
            <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" Required="TRUE" />
            <FieldRef ID="{81291702-2603-431B-9EA8-EBDAAD457C1A}" Name="RidgianKeywords" DisplayName="Keywords" Required="TRUE" />
            <FieldRef ID="{A6FDD77B-AD2E-4E8D-9579-065488900F1C}" Name="RidgianKeywordsTaxHTField0" DisplayName="KeywordsTaxHTField0" Hidden="TRUE"/>
            <FieldRef ID="{f3b0adf9-c1a2-4b02-920d-943fba4b3611}" Name="TaxCatchAll" Hidden="TRUE" />
            <FieldRef ID="{8f6b6dd8-9357-4019-8172-966fcd502ed2}" Name="TaxCatchAllLabel" Hidden="TRUE" />
            <FieldRef ID="{BCDA41CE-70D2-490A-AFBF-3D7E576D211F}" Name="External" Description="Set to ‘Yes’ if document can be viewed Externally."/>
        </FieldRefs>
        <XmlDocuments>
            <XmlDocument NamespaceURI="
http://schemas.microsoft.com/sharepoint/events&quot;>
                <spe:Receivers xmlns:spe="
http://schemas.microsoft.com/sharepoint/events&quot;>
                    <Receiver>
                        <Receiver>
                            <Name>TaxonomyItemAddingEventReceiver</Name>
                            <Synchronization>Synchronous</Synchronization>
                            <Type>1</Type>
                            <Assembly>Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
                            <Class>Microsoft.SharePoint.Taxonomy.TaxonomyItemEventReceiver</Class>
                            <SequenceNumber>50</SequenceNumber>
                        </Receiver>
                        <Receiver>
                            <Name>TaxonomyItemUpdatingEventReceiver</Name>
                            <Synchronization>Synchronous</Synchronization>
                            <Type>2</Type>
                            <Assembly>Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
                            <Class>Microsoft.SharePoint.Taxonomy.TaxonomyItemEventReceiver</Class>
                            <SequenceNumber>50</SequenceNumber>
                        </Receiver>
                    </Receiver>
                </spe:Receivers>
            </XmlDocument>
            <XmlDocument NamespaceURI="
http://schemas.microsoft.com/sharepoint/v3/contenttype/forms&quot;>
                <FormTemplates xmlns="
http://schemas.microsoft.com/sharepoint/v3/contenttype/forms&quot;>
                    <Display>RidgianDocumentLibraryForm</Display>
                    <Edit>RidgianDocumentLibraryForm</Edit>
                    <New>RidgianDocumentLibraryForm</New>
                </FormTemplates>
            </XmlDocument>
        </XmlDocuments>
    </ContentType>

</Elements>

 

It’s through the XmlDocument elements within the CAML definition where the connection is made. Of course you can do this in code if you like. The key thing I found in order to get things working properly was to declare the type of the event receiver as a number, not as the description as you would normally do. So the ItemAdding event enumerated value is 1. The ItemUpdating is 2.  I discovered this when looking at the way event receivers are hooked up to DocumentSets when working with the Content Organiser.  If you want the full enumerated values for event receivers, here they are straight from Microsoft.SharePoint.dll courtesy of Redate .NET Reflector, seeing as MSDN don’t actually publish the values …

public enum SPEventReceiverType
{
    ContextEvent = 0x7ffe,
    EmailReceived = 0x4e20,
    FieldAdded = 0x2775,
    FieldAdding = 0x65,
    FieldDeleted = 0x2777,
    FieldDeleting = 0x67,
    FieldUpdated = 0x2776,
    FieldUpdating = 0x66,
    InvalidReceiver = -1,
    ItemAdded = 0x2711,
    ItemAdding = 1,
    ItemAttachmentAdded = 0x2717,
    ItemAttachmentAdding = 7,
    ItemAttachmentDeleted = 0x2718,
    ItemAttachmentDeleting = 8,
    ItemCheckedIn = 0x2714,
    ItemCheckedOut = 0x2715,
    ItemCheckingIn = 4,
    ItemCheckingOut = 5,
    ItemDeleted = 0x2713,
    ItemDeleting = 3,
    ItemFileConverted = 0x271a,
    ItemFileMoved = 0x2719,
    ItemFileMoving = 9,
    ItemUncheckedOut = 0x2716,
    ItemUncheckingOut = 6,
    ItemUpdated = 0x2712,
    ItemUpdating = 2,
    ListAdded = 0x2778,
    ListAdding = 0x68,
    ListDeleted = 0x2779,
    ListDeleting = 0x69,
    SiteDeleted = 0x27d9,
    SiteDeleting = 0xc9,
    WebAdding = 0xcc,
    WebDeleted = 0x27da,
    WebDeleting = 0xca,
    WebMoved = 0x27db,
    WebMoving = 0xcb,
    WebProvisioned = 0x27dc,
    WorkflowCompleted = 0x2907,
    WorkflowPostponed = 0x2906,
    WorkflowStarted = 0x2905,
    WorkflowStarting = 0x1f5
}

 

Hope that helps somebody

Cheers

Dave Mc

Advertisements

About davemcmahon81
Software Developer & Architect, User Group Leader, Speaker, Writer, Blogger, Occasional Guitarist, Man-made Global Warming Sceptic, Climate Change Believer, General Optimist but most of all proud Husband and Dad ...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: