1

I am unable to attach an event receiver to my custom content type. If I change the inherits property of the content type to false , then the content type is not visible after deployment. I change the inherits property of the content type to true then the <XMLDocuments> tag is getting ignored. Please help me solve this issue.

The Elements.xml file is :

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Parent ContentType: Item (0x01) -->
  <ContentType ID="0x01008812D4663E574A9DB3147C3277B026A4"
               Name="DeclarativeCT2"
               Group="Custom Content Types"
               Description="My Content Type"
               Inherits="FALSE"
               Version="0">
    <FieldRefs>
      <FieldRef ID="{FF878E1C-9C79-485A-B252-193C0EBB4F2D}" 
                Name="CreatedFromXML" DisplayName="Created From XML" 
                ReadOnly="FALSE" 
                Required="TRUE" />
    </FieldRefs>
    <XmlDocuments>
      <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/events">
        <Receivers xmlns:spe="http://schemas.microsoft.com/sharepoint/events">
          <Receiver>
            <Name>EventReceiverForCTItemUpdating</Name>
            <Type>ItemUpdating</Type>
            <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
            <Class>$SharePoint.Type.c2237798-87e9-483b-bb17-1b3d4715fed2.FullName$</Class>
            <SequenceNumber>1</SequenceNumber>
          </Receiver>
        </Receivers>
      </XmlDocument>
    </XmlDocuments>
  </ContentType>
  <Field ID="{FF878E1C-9C79-485A-B252-193C0EBB4F2D}" 
         Name="CreatedFromXML" 
         DisplayName="Created From XML" 
         Type="Text" 
         Required="FALSE" Group="Custom Columns" />
</Elements>

The C# code for event receiver is :

using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Microsoft.SharePoint.Taxonomy;

namespace DeclarativeCT1
{
    [Guid("C2237798-87E9-483B-BB17-1B3D4715FED2")]
    public class EventReceiverForCT : SPItemEventReceiver
    {

        public override void ItemUpdating(SPItemEventProperties properties)
        {
            this.EventFiringEnabled = true;
            base.ItemAdded(properties);
            string customFieldValue = properties.ListItem[new Guid("FF878E1C-9C79-485A-B252-193C0EBB4F2D")].ToString();
            if (customFieldValue.Equals("Fish", StringComparison.OrdinalIgnoreCase))
            {
                properties.Status = SPEventReceiverStatus.CancelWithError;
                properties.ErrorMessage = "No Fish in text please";
            }
        }
    }
}

Below is screenshot of my project :

enter image description here

Below is the script i used to check for event receivers enter image description here

  • How do you know for sure the ER is not attached? Maybe an error is raised in the code of your ER, and you think it does not run, whil it actually starts and fails immediately (like properties.ListItem[new Guid("FF878E1C-9C79-485A-B252... being null. – Evariste Sep 11 '16 at 17:44
  • @Evariste I have added screenshot of the script I wrote to check the event receivers. The count is zero. – Karthik Sep 11 '16 at 20:07
  • @Evariste you are actually correct. The event receiver is working. I was just checking the output of the script I wrote. It is misleading. Can anyone explain why the script is misleading ? – Karthik Sep 11 '16 at 20:49
  • Are you sure that you have bound correct <Class> path in declaration? – Kalpesh Vaghela Sep 12 '16 at 3:57
0

The ER may be attached, and an error is raised in the code of your ER; you think it does not run, while it actually starts and fails immediately (like at properties.ListItem[new Guid("FF878E1C-... being null).

Your $ct.EventReceivers.Count may not report the ER accordingly since it's registered declaratively (via a feature artefact/XML file).

In such scenarios, I highly prefer an imperative approach, i.e. attaching the ER from C# code in the feature receiver. That way you do control exactly what happens (easier feature upgrade + easier debugging + more control over options + easier to be understood for a developer...).

  • "Your $ct.EventReceivers.Count may not report the ER accordingly since it's registered declaratively (via a feature artefact/XML file)." I think the Count parameter should return the count of the registered event recievers, regardless of the type of registration (programmatically or declaratively). – pholpar Oct 12 '18 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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