0
votes

I created some site columns / a content type and a list definition as part of a feature. I want to attach an eventreceiver to the content type. I added code to attach the event receiver to the content type. Using spmanager i can see that the event receiver is attached to the content type however when i create lists from the content type the event reciever is missing. Any ideas. My code is below

  public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                string asmName = System.Reflection.Assembly.GetExecutingAssembly().FullName;
                string itemReceiverName = "xxxxxx.Intranet.SP.xxxxx.PermissionsUpdaterEventReceiver";

                ////surely a better way to get all lists than this
                ////re - do
                using (SPSite thisSite = (SPSite)properties.Feature.Parent) {
                    using (SPWeb web = thisSite.RootWeb) {
                        SPContentType RambollNewsContentType = web.ContentTypes["RambollNewsContentType"];
                        RambollNewsContentType.EventReceivers.Add(SPEventReceiverType.ItemAdded, asmName, itemReceiverName);
                        RambollNewsContentType.Update(true);
                    }
                }    
            }
2

2 Answers

0
votes

I am using this successfully. I left in my logging and the logging method.

    /// <summary>
    /// This method is executed on feature activation.
    /// It attaches the event receiver to the content type.
    /// </summary>
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        LogMessage("FeatureActivated - Start");
        SPSite site = (SPSite)properties.Feature.Parent;
        LogMessage("FeatureActivated - 1");
        SPWeb web = site.RootWeb;
        LogMessage("FeatureActivated - 2");
        //ListContentTypes(web);
        SPContentType ctype = web.ContentTypes["Wells Project Task List"];
        LogMessage("FeatureActivated - 3");
        LogMessage("ctype name: " + ctype.Name.ToString());
        if (ctype != null)
        {
            LogMessage("FeatureActivated - I have a content type. Web url: " + web.Url);
            SPEventReceiverDefinition er = ctype.EventReceivers.Add();
            er.Class = "Wells.SharePoint.ProjectManagementEventReceiver";
            er.Assembly = "ProjectManagementEventReceiver, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a1cb21c41d80b7ae";
            er.Type = SPEventReceiverType.ItemAdded;
            er.Name = "ItemAdded";
            er.SequenceNumber = 10001;
            er.Update();
            ctype.Update(false);
            LogMessage("FeatureActivated - After ctype.update");
            web.Dispose();
        }
        else
            LogMessage("CT not found: " + web.Url);




        LogMessage("FeatureActivated - End");
    }

    static void ListContentTypes(SPWeb web)
    {
        foreach (SPContentType ct in web.ContentTypes)
        {
            LogMessage("CT: " + ct.Name.ToString());
        }
    }


    /// <summary>
    /// This method is executed on feature deactivation.
    /// It removes the event receiver from the content type
    /// </summary>
    /// <param name="properties"></param>
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        LogMessage("Feature DeActivated  - Start");
        SPSite site = (SPSite)properties.Feature.Parent;
        SPWeb web = site.OpenWeb();
        SPContentType contentType = web.ContentTypes["NAME OF CONTENT TYPE"];
        if (contentType != null)
        {
            LogMessage("im have a content type. Web url: " + web.Url);
            int i;
            //Use the above integer to loop through the event recievers on the first list and delete the above assembly
            for (i = 0; i < contentType.EventReceivers.Count; i++)
            {
                LogMessage("ER Count: " + contentType.EventReceivers.Count);
                if (contentType.EventReceivers[i].Assembly.Contains("ProjectManagementEventReceiver"))
                {
                    contentType.EventReceivers[i].Delete();

                    LogMessage("Deleting event receiver from CT");


                }
            }
            contentType.Update();
        }
        else
            LogMessage("CT not found: " + web.Url);

        LogMessage("Feature DeActivated  - End");

    }

static void LogMessage(string msg)
    {
        StreamWriter wrtr = null;
        try
        {
            wrtr = new StreamWriter("C:\\Logs\\FeatureActivatedDeactivated.txt", true);
            wrtr.WriteLine(msg + "--[" + System.DateTime.Now.ToString() + "]" + Environment.NewLine);
            wrtr.WriteLine(Environment.NewLine + "==================================");
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            if (wrtr != null)
            {
                wrtr.Close();
                wrtr.Dispose();
            }
        }
    }
0
votes

I am not sure is this related to your question or not but

but could you try to add the event receiver to the content type before you add the content type to the list.

I think the event receiver has to be added before because when adding a content type to a list the content type is not added directly to the list, rather a copy of it is added to the list. So when you add your content type to the list, there is no event receiver yet.

Correct me if i understand your question wrong?

Thanks