2

I have a set of event receivers for a SharePoint 2010 content type and Library. Previously, these would install and automatically be activated when I ran a script to install the wsp solution. I dug up a different way of doing this, however, and now they will install appropriately when I run the solution in debug mode in Visual Studio, but they will not install when I run the aforementioned power shell script. I checked the site and made sure the feature was activated, but after doing that I still did not see the event receivers using SPM.

I was following the guide from this page

Event Receiver Elements.xml:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="101">
    <Receiver>
      <Name>EventReceiverItemCheckedIn</Name>
      <Type>ItemCheckedIn</Type>
      <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
      <Class>SharePointFeatures.EventReceiver.EventReceiver</Class>
      <SequenceNumber>1000</SequenceNumber>
    </Receiver>
    <Receiver>
      <Name>EventReceiverItemCheckingIn</Name>
      <Type>ItemCheckingIn</Type>
      <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
      <Class>SharePointFeatures.EventReceiver.EventReceiver</Class>
      <SequenceNumber>1001</SequenceNumber>
    </Receiver>
    <Receiver>
      <Name>EventReceiverItemUpdated</Name>
      <Type>ItemUpdated</Type>
      <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
      <Class>SharePointFeatures.EventReceiver.EventReceiver</Class>
      <SequenceNumber>1002</SequenceNumber>
    </Receiver>
    <Receiver>
      <Name>EventReceiverItemUpdating</Name>
      <Type>ItemUpdating</Type>
      <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
      <Class>harePointFeatures.EventReceiver.EventReceiver</Class>
      <SequenceNumber>1003</SequenceNumber>
    </Receiver>
    <Receiver>
      <Name>EventReceiverItemDeleting</Name>
      <Type>ItemDeleting</Type>
      <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
      <Class>SharePointFeatures.EventReceiver.EventReceiver</Class>
      <SequenceNumber>1004</SequenceNumber>
    </Receiver>
  </Receivers>
</Elements>

Feature template.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/">
  <Properties>
    <Property Key="GloballyAvailable" Value="true"/>
    <Property Key="AssemblyQualifiedName" Value="$SharePoint.Type.800d2e40-4c00-4517-9ff4-d11da11b6826.AssemblyQualifiedName$"/>
    <Property Key="AssemblyFullName" Value="$SharePoint.Project.AssemblyFullName$"/>
  </Properties>
</Feature>

Feature Receiver code:

[Guid("fbe26e13-dba2-4fe6-b947-9f2c34accd39")]
public class FeaturesEventReceiver : SPFeatureReceiver
{
    private string EventReceiverClassName;
    private string AssemblyFullName;
    /// <summary>
    /// Feature activated logic
    /// </summary>
    /// <param name="properties"></param>
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {

        try
        {
            SPWeb web = properties.Feature.Parent as SPWeb;

            //di-associate EventReceiver from Print only content type
            SetAssemblyParams(properties.Feature.Properties);
            DisableEventReceiver(web);


            //associate EventReceiver to content type as defined in configuration <activefields sharepointcontenttypename="Print Only"> 
                FmiMetadataMappingHandler mappingHandler = WebConfigurationManager.OpenWebConfiguration("/", web.Site.WebApplication.Name).GetSection(Utils.GetConfigSectionNameForWeb(web)) as FmiMetadataMappingHandler;
                SPContentType ct = web.ContentTypes[mappingHandler.ActiveFields.SharepointContentTypeName];

                if (ct != null)
                {
                    SPEventReceiverDefinition def = ct.EventReceivers.Add();
                    def.Name = "EventReceiverItemCheckingIn";
                    def.Type = SPEventReceiverType.ItemCheckingIn;
                    def.SequenceNumber = 1000;
                    def.Assembly = AssemblyFullName;
                    def.Class = EventReceiverClassName;
                    def.Update();

                    // bind event receiver to ItemCheckedIn Event                                      
                    def = ct.EventReceivers.Add();
                    def.Name = "EventReceiverItemCheckedIn";
                    def.Type = SPEventReceiverType.ItemCheckedIn;
                    def.SequenceNumber = 1001;
                    def.Assembly = AssemblyFullName;
                    def.Class = EventReceiverClassName;
                    def.Update();

                    // bind event receiver to ItemUpdated Event                                      
                    def = ct.EventReceivers.Add();
                    def.Name = "EventReceiverItemUpdated";
                    def.Type = SPEventReceiverType.ItemUpdated;
                    def.SequenceNumber = 1002;
                    def.Assembly = AssemblyFullName;
                    def.Class = EventReceiverClassName;
                    def.Update();

                    // bind event receiver to ItemUpdating Event                                      
                    def = ct.EventReceivers.Add();
                    def.Name = "EventReceiverItemUpdating";
                    def.Type = SPEventReceiverType.ItemUpdating;
                    def.SequenceNumber = 1003;
                    def.Assembly = AssemblyFullName;
                    def.Class = EventReceiverClassName;
                    def.Update();

                    // bind event receiver to ItemDeleting Event                                      
                    def = ct.EventReceivers.Add();
                    def.Name = "CWTEventReceiverItemDeleting";
                    def.Type = SPEventReceiverType.ItemDeleting;
                    def.SequenceNumber = 1004;
                    def.Assembly = AssemblyFullName;
                    def.Class = EventReceiverClassName;
                    def.Update();
                    //update content type with event receiver
                    ct.Update(true);

                    Utils.LogMessage("CWT Feature Event Receivers added successfully.");
                }

        }
        catch (Exception ex)
        {
            Utils.LogError(ex.Message, ex.StackTrace);
        }

    }

    private void SetAssemblyParams(SPFeaturePropertyCollection properties)
    {
        EventReceiverClassName = properties["AssemblyQualifiedName"].Value;
        AssemblyFullName = properties["AssemblyFullName"].Value;
    }// end SetAssemblyParams

    /// <summary>
    /// feature deactivating
    /// </summary>
    /// <param name="properties"></param>
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        SetAssemblyParams(properties.Feature.Properties);
        DisableEventReceiver(properties.Feature.Parent as SPWeb);
    }

    /// <summary>
    ///  di-associate event receiver from content type
    /// </summary>
    /// <param name="web"></param>
    private void DisableEventReceiver(SPWeb web)
    {
        try
        {
            FmiMetadataMappingHandler mappingHandler = WebConfigurationManager.OpenWebConfiguration("/", web.Site.WebApplication.Name).GetSection(Utils.GetConfigSectionNameForWeb(web)) as FmiMetadataMappingHandler;
            SPContentType ct = web.ContentTypes[mappingHandler.ActiveFields.SharepointContentTypeName];
            string errMsg = string.Empty;
            if (ct == null)
            {
                errMsg = string.Format("{0} has not been created in web {1} ", mappingHandler.ActiveFields.SharepointContentTypeName, web.Title);
                Utils.LogError(errMsg);
                throw new Exception(errMsg);

            }
            if (ct != null)
            {
                SPEventReceiverDefinition[] fmiEvents = new SPEventReceiverDefinition[ct.EventReceivers.Count];
                ct.EventReceivers.CopyTo(fmiEvents, 0);

                for (int i = 0; i < fmiEvents.Length; i++)
                {
                    if (fmiEvents[i].Class.Equals(EventReceiverClassName, StringComparison.CurrentCultureIgnoreCase))
                    {
                        fmiEvents[i].Delete();
                        Utils.LogError("cwt receiver removed");
                    }
                }


                ct.Update(true);
              //  web.Update();

            }
        }
        catch (Exception ex)
        {
            Utils.LogError(ex.Message);
        }
    }

* UPDATE *

So, now it won't install at all, even in debug mode. I looked in the logs and the feature is generating an error during the "FeatureActivated" part of the installation. I commented out all of the code in FeatureActivated and everything except a try/catch in FeatureDeactivating and it still throws this error.

Error message: Type names passed to Assembly.GetType() must not specify an assembly.

4
  • Your 'new' deployment script doesn't happen to involve using 'Update-SPSolution' does it?
    – Dave Wise
    Dec 17, 2012 at 22:23
  • no, it uninstalls and then reinstalls the solution: Uninstall-SPSolution -> Remove-SPSolution -> Add-SPSolution -> Install-SPSolution, then IISReset, etc.
    – Matt
    Dec 17, 2012 at 22:43
  • You script activates the features as well?
    – Dave Wise
    Dec 18, 2012 at 0:04
  • it apparently always did before. I didn't change anything with regards to the script. I did notice the feature wasn't activated so I did activate it later but that still didn't add the event receiver
    – Matt
    Dec 18, 2012 at 1:32

1 Answer 1

0

I am still not sure what the problem was, but I made it go away. Maybe this is the real solution I was looking for, I do not know. At any rate, I went through the motions commenting out all of my code except for the actual event handler methods themselves. I then tried running the solution in debug mode. No dice. At that point I basically knew it couldn't have been anything I was really doing (it could have but I "knew" it). So, on a hunch, I rebuilt the solution and packaged it, THEN ran it in debug mode. Voila. I then systematically re-added the various code pieces starting with the feature receiver code and everything has been deploying since. I do, however, continue to package first to prevent my original issue from happening again. I am not sure that's absolutely necessary but it's easier than running into that same problem again.

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.