2

I have build an event receiver that perform some job when a new item is created.

The event should be fired on a specific list type which is defined in the same solution.

However, the event receiver also fires for list type, with the same TemplateID of other solution.

Here is my receiver subscription :

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="10000"
             ListTemplateOwner="fde2b1cc-51e1-4654-b754-addcb571ce12">
    <Receiver>
      <Name>MyEventReceiverItemAdded</Name>
      <Type>ItemAdded</Type>
      <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
      <Class>MyProjecT.MyReceiverClass</Class>
      <SequenceNumber>10000</SequenceNumber>
    </Receiver>
  </Receivers>
</Elements>

I specified in the ListTemplateOwner attribute the ID of the feature who created the list (via a <ListInstance> node).

But the event fires for another list, defined in another solution, with the same ListTemplateID. I can change the template ID, but I can't guarantee that someday, someone else create a new list template with the same ID (10000 is the default value with VS).

What is the correct way of subscribing an event receiver to a specific kind of list?

2 Answers 2

4

For what it's worth, if you're designing a custom list in Visual Studio, I NEVER accept the default of 10000 for the template type for this reason amongst others. (Another reason: I use the special list template number in custom ribbon development to make certain ribbon buttons appear in relationship to certain custom lists)

Here's the additional code I find myself having to write, because, I've also come across event receivers trying to fire site-wide in this bewildering manner you described:

Const MY_CUSTOMLIST_BASETEMPLATE As String = "37000"
''' <summary>
''' An item is being updated.
''' </summary>
Public Overrides Sub ItemUpdating(ByVal properties As SPItemEventProperties)
    If properties.List.BaseTemplate.ToString <> MY_CUSTOMLIST_BASETEMPLATE Then Return

    MyBase.ItemUpdating(properties)
    InitProfileLookup(properties, False)
End Sub

I look forward to other's comments to learn if there's a better way than to have to check for the list template type in the event receiver code. (EDIT: see my comment to this answer for why this is happening)


Thought I'd tack on my example ListTemplate Elements.xml snippet, for completeness.

Notice the Type=37000, a custom number I chose that will forever identify, so to speak,all lists spawned from this list definition (if you've already generated ListInstance element files, you'll need to find the 10000 number and fix it there, too)

    <?xml version="1.0" encoding="utf-8" ?>
        <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
            <ListTemplate
                Name="ManifestLibrary"
                Type="37000"
                BaseType="1"
                OnQuickLaunch="TRUE"
                SecurityBits="11"
                Sequence="110"
                DisplayName="Manifest Library"
                Description="Library designed for storing manifests scanned via copier"
                Image="/_layouts/images/itdl.png"
        </Elements>

Furthermore, any number ten-thousand and above are available for developer use, wheras Micrsoft reserves the right to use template numbers under ten thousand.

1
  • After talking to a Microsoft MVP / SharePoint consultant (Doug Ware) I realized there's another wrinkle to this problem. Despite the declarative file that would make you think the EventReciever is tightly scoped to a specific list, you need to be sure it's not being deployed in a feature that is "Site" scoped. Be sure your event receivers are "Web" scoped or they will fire when they shouldn't!!
    – bkwdesign
    Nov 20, 2012 at 16:59
2

I solved my problem by registering the event receiver to the content type, instead of the list.

bkwdesign solution may work, but I can't rely on everyone changing the templateID.

So here is my content type definition :

  <ContentType ID="0x01006ffb64509c2341d2bff13b98859f792b" Name="myct" Group="group" Description="desc" Version="0">
    <Folder TargetName="myct" />
    <FieldRefs>
        <!-- removed for readability -->
    </FieldRefs>
    <XmlDocuments>

      <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events">
        <spe:Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events">
          <Receiver>
            <Name>MyEventReceiverItemAdded</Name>
            <Type>ItemAdded</Type>
            <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
            <Class>MyNamespace.MyReceiver</Class>
            <SequenceNumber>10000</SequenceNumber>
          </Receiver>
        </spe:Receivers>
      </XmlDocument>
    </XmlDocuments>
  </ContentType>

Of course, I add to duplicate this content type definition within the schema.xml file of my custom list definition.

5
  • However, if you change YOUR template ID, then the event receiver will work for just your template. I'm not understanding why there is a need to care what everyone else is doing when you have complete control of your list?
    – bkwdesign
    Jun 22, 2012 at 16:14
  • Refer to the last snippet I added in my original answer. Since the list template is defined as 37000 for the type (a number I just made up) it will be used for every list ever instatiated off of this list template. Of course, your content type solution will work just as well. Your solution is more fluid in one sense, since existing lists of other types can make use of your event receiver if they edit their existing lists to add-in your new content type at some point in the future.
    – bkwdesign
    Jun 22, 2012 at 16:17
  • @bkwdesign: you are right, but in my case, it's a bit late. I will however follow your advise for the next lists I'll create.
    – Steve B
    Jun 22, 2012 at 16:20
  • But even if you changed the templateid, you have no guarantee that nobody will reuse the same id, isn't it ? The probability is low, but not null
    – Steve B
    Jun 22, 2012 at 16:23
  • Sure, it's technically possible, but, in my organization of 200 employees (and only 5 in I.T. - and only 1 who is the sharepoint guru - me) it is never going to happen. Now, when I used to work at Ernst & Young.. w/110,000 employeees.. maybe.. but that's what documentation and team communication is for..
    – bkwdesign
    Jun 22, 2012 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.