0

I have added FeatureEventReceiver to the solution and here is my code,

 public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {

            using (SPSite site = new SPSite("http://itcdev33"))
            {
                using (SPWeb web = site.RootWeb)
                {

                    web.AllowUnsafeUpdates = true;
                    site.AllowUnsafeUpdates = true;
                    Guid myid = new Guid("32529c03-833d-4c5e-a15e-0bcb35bdedad");
                    if (site.Features[myid] == null)
                    {
                        site.Features.Add(myid, true, SPFeatureDefinitionScope.Site);
                    }
                    web.Update();
                    site.AllowUnsafeUpdates = false;
                    web.AllowUnsafeUpdates = false;
                }
            }
        });

    }
5
  • no sharepoint 2010 Feb 25, 2014 at 13:10
  • Is this a custom feature or is it some inbuilt feature? Feb 25, 2014 at 13:24
  • custom solution, custom visual webpart and in that I added featureeventreceiver and in that I have written code in FeatureInstalled Feb 25, 2014 at 13:34
  • What feature id is this "32529c03-833d-4c5e-a15e-0bcb35bdedad" Feb 25, 2014 at 13:35
  • Ya that's my guid of feature that I have viewed in feature.xml Feb 25, 2014 at 13:39

2 Answers 2

1

already answered here:

Activate a feature in feature installed

issue and why:

The FeatureInstalled method is called when the WSP is loaded into the Farm and the Feature files are copied into the 12 hive.

This is done in Central Administration or via the command line (Powershell or STSADM). Because of this, there is no website Context to speak of, no site collection or no SPWeb.

solution:

Then your solution is to have the Feature scoped to Web-Application, your Feature.Parent will be an SPWebApplication object, and you will be able to access the Sites property of that SPWebApplication, which is a collection of SPSite objects. You'll then be able to activate your Site Collection Scoped feature at that level.

2
  • Sorry sir I didn't understood what you said, I am a bit confused. If we make the feature's scope to web-application how would the same feature remain at site collection level? Feb 26, 2014 at 6:10
  • I mean I am trying to activate same feature of the sharepoint project while the solution is deployed. And I have added a visual webpart to that solution which I have to add on a web part page so I want that the feature to be activated while the solution is deployed so that I can just add the web part to the web part page. Feb 26, 2014 at 6:25
0

I don't think it is possible to activate the same feature in the Feature installed event. The feature installation is not yet complete and trying to activate it will cause the error you are seeing.

UPDATE

static void Main(string[] args)
{

    using (SPSite site = new SPSite("http://itcdev33"))
    {
        SPWeb web = site.RootWeb;
        web.AllowUnsafeUpdates = true;
        Guid featureGuid = new Guid("32529c03-833d-4c5e-a15e-0bcb35bdedad");
        web.Features.Add(featureGuid);
        web.AllowUnsafeUpdates = false;
    }           
}

You can also activate the feature using PowerShell:

Enable-SPFeature "32529c03-833d-4c5e-a15e-0bcb35bdedad" -Url "http://itcdev33"
11
  • Ok Thanks sir, can you tell me how do I achieve activating a site collection feature programmatically. Since the feature is visible in Site collection features and not yet activated though the solution is deployed. Feb 25, 2014 at 13:58
  • site.Features.Add is the correct method of doing it programmatically. However, as said feature installed event is not the right place to call that code. Feb 25, 2014 at 15:04
  • Then where should I call the code in page load? Feb 26, 2014 at 5:26
  • What do you mean by page load? Can you provide more information about what you are trying to achieve. May be there is a different way of doing it. Feb 26, 2014 at 5:58
  • I have created an empty sharepoint project, in that I have added visual webpart and I have added featureeventreceiver, to the project. And trying to activate the feature when the solution is deployed, I mean programmatically. So is that I have to add an Event receiver to that webpart? Feb 26, 2014 at 6:05

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.