Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 6 down vote accepted

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; }
            }

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

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.

share|improve this answer
    
System.IO.FileNotFoundException = Not creating the list before the event handler is added –  John Jun 23 '12 at 20:20

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

share|improve this answer
1  
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 '12 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 –  Khushi Oct 7 '13 at 15:37

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.

share|improve this answer
    
having such dumb logic in couple of features would further complicate deployment process. keep it in one. –  Geek Jun 24 '12 at 15:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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