11

I have a feature. On the FeatureActivated event I am creating a list if it is not there. I also have an EventReceiver that in the elements.xml on the Receivers node has the attribute ListUrl. This is set to the URL that I create in the FeatureActivated event.

When I deploy my code through Visual Studio I am getting:

Error occurred in deployment step 'Activate Features': 0x80070002

I am presuming that it has something to do with the order the code is run. The Event Receiver is being "installed" before the list is being created.

Is there a way I can get round this?

4 Answers 4

7

As EStruyf said, you can change the deployment configuration and debug the code for your Feature Event Receivers. But, you can also attach the event receivers after making sure List is created, so put this code after your list is created.

public class Feature1EventReceiver : SPFeatureReceiver
    {

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
      {
        SPList lst = site.Lists["ListName"];   
        string asmName = Assembly.GetExecutingAssembly().FullName;  //get the Full Assembly Name 
        string itemReceiverName = "Project1.EventReceivers"; // Event Receivers class (EventReceivers.cs) which contains definitions for events for e.g. ItemAdded...
        lst.EventReceivers.Add(SPEventReceiverType.ItemAdded, asmName, itemReceiverName);   

    }
    //delete the event receivers on Feature de-activation
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb oWeb = (SPWeb)properties.Feature.Parent;

            SPList oList = oWeb.Lists[_listName];
            for (int i = oList.EventReceivers.Count - 1; i >=0 ; i--)
            {
                if (oList.EventReceivers[i].Type.Equals(SPEventReceiverType.ItemAdded))
                {
                    try
                    {
                        oList.EventReceivers[i].Delete();
                    }
                    catch (Exception e)
                    {
                        // Write to logs
                    }
                }
            }
        }

}

Your EventReceivers.cs will look like

class EventReceivers : SPItemEventReceiver
    {

        public override void ItemAdded(SPItemEventProperties properties)
        {
            if (properties.Context != null)
            {
                try
                {

                  //do something here
                }
                catch { return; }
            }

        }
}
8
  • Will give it a go
    – John
    Jun 23, 2012 at 20:21
  • why don't you do it in one Feature? Jun 23, 2012 at 20:49
  • I get the System.IO.FileNotFoundException error. Not tried your method yet need to work out AssemblyofEventHandler & YourPublicKeyToken
    – John
    Jun 23, 2012 at 20:54
  • Ask questions, if you have any doubts Jun 23, 2012 at 21:06
  • Where would I find the information for asmName? Do you still need the elements.xml for the reciever?
    – John
    Jun 23, 2012 at 21:11
2

A quick way is to set the value of the active deployment configuration from Default to No Activation (in the properties of the project). That way you need to manually activate your features and you can directly see what goes wrong.

1
  • System.IO.FileNotFoundException = Not creating the list before the event handler is added
    – John
    Jun 23, 2012 at 20:20
1

Check this out: http://sharepointfundamental.wordpress.com/2012/05/17/sharepoint-2010-error-occurred-in-deployment-step-activate-features-0x80070002/

http://social.technet.microsoft.com/Forums/en-AU/sharepoint2010programming/thread/54f95e2b-782e-4205-85c6-dc0074bf8563

2
  • 2
    While this may theoretically answer the question, we prefer inclusion of the essential parts of the answer here, and to provide the link for reference. See answer for general guidelines.
    – SPDoctor
    Jun 24, 2012 at 16:18
  • sharepointfundamental.wordpress.com/2012/05/17/… This solves my problem. In my case the SiteURL was not set correct. The list doesn't exist in the SiteURL I set. Thanks Zakir HC for the reference link. Regards, Khushi
    – user19888
    Oct 7, 2013 at 15:37
0

I have split the code into two features. One creates the list the other adds the event reciever. I have put a feature depencey that the event reciever featute needs the setup feature enabled.

Not ideal but works. Would like to try and do it all in one feature.

1
  • having such dumb logic in couple of features would further complicate deployment process. keep it in one.
    – Geek
    Jun 24, 2012 at 15:20

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.