1

I am making a visual web part in SP 2010 using visual studio 2010.

I want it so that when I add and deploy my wsp file, it should by default keep the site collection feature activated for the web part.

Is there a way to do this? Maybe add some special xml to the feature file?

Also I noticed that the first time I deployed it, it was not active, then I activated the feature. Then from that point, if I ever try to retract and remove, then add and deploy, the feature is activated by default. Can anyone explain whats going on here? I would expect the feature to be inactivated by default...

5
  • Are you taking about deployment in a development environment using Visual Studio or in a non-dev environment using Powershell and/or Central Admin? Aug 29, 2013 at 21:32
  • Non dev environment. And I will be installing the wsp file through sp powershell and central admin.
    – omega
    Aug 29, 2013 at 22:47
  • The way Visual Studio does deployment can make this confusing. By default, when you deploy in Visual Studio it automatically activates Features. When you deploy using Powershell or Central Admin it does not automatically activate Features. Aug 30, 2013 at 1:01
  • As far as I know, there is no setting in the Feature manifest (XML) to have SharePoint automatically activate the Feature when the solution package (WSP) is deployed. Aug 30, 2013 at 1:02
  • I have already iterated through the site collection features and founded features that are activated and those that are not activated. And also have tried to activate the site collection feature by using site.Features.Add(guid,bool value,SPFeatureDefinitionScope.Site) but still not getting activated that feature please anybody help me to tell. Feb 25, 2014 at 11:51

4 Answers 4

2

The approach can all be done in PowerShell, First, you add the solution to the solution gallery if it's not already added using this command: "

Add-SPSolution -LiteralPath "path to the wsp file"

Then you install (deploy) the solution, note that PowerShell uses different terms when it deals with WSP files and features. To install (Deploy) the solution so the features will be available to activate, use this command:

Install-SPSolution –Identity “name of the wsp file with the .wsp extension” –WebApplication "url for the intended web application" 

You may need to use the -GACDeployment if the dll needs to be deployed to the GAC.

Next you will activate the feature on the desired site:

Enable-SPFeature –identity Feature GUID -URL "url of the site collection"

Hope this helps you.

6
  • The OP is looking for the feature to be enabled automatically when he installs the feature. Powershell is a great tool but it's hardly automatic. Aug 30, 2013 at 0:30
  • 1
    It's the right way to deploy and automate the deployment of WSP files. At the end, you are going to use powershell to deploy your WSP to the QA and the Production environment, so it's better to even use it in your development to automate the deployment of your work. It is worth it to spend one hour more to build a PowerShell function that will deploy your wsp and activate the features by default, which you would use as a base for other projects and for deployment in other environments.
    – user19105
    Aug 30, 2013 at 0:37
  • ...or, if you are not administering the farm you are developing solutions for, you would use a feature receiver instead of hoping that your admin is well-versed enough in Powershell to be able to carry out those commands. Aug 30, 2013 at 0:48
  • +1 :) If you mean Sandbox solution, then yes, you would need to write code when the feature is installed.
    – user19105
    Aug 30, 2013 at 1:09
  • 1
    You can specify feature's GUID or Name, both will work.
    – user19105
    Aug 30, 2013 at 2:38
2

For Site and Web scoped feature, only way is to use the powershell to activate the feature after it is deployed.

(But for Farm and Web Application scoped features, Go to the manifest of your feature and set "ActivateOnDefault" to true, this will cause the feature to get activated automatically whenever the solution is deployed using powershell or central admin etc. This avoids the need for any code to activate a feature.)

1
  • The above comment says this only works when u deploy from visual studio. I will be deploying from powershell and CA.
    – omega
    Aug 30, 2013 at 11:59
1

Have a look at the properties on the feature within VS -- there are a couple of options there that will control how the features behave when the solution is deployed via VS ( only ).. these settings have no affect on the solution once it is out of VS...

Activate On Default - Indicates whether the feature is activated during installation. ...this is what you need to set to True.

Always force Install - Indicates whether the feature is installed by force, even though it is already installed. Auto Activate in Central Admin - Indicates that the feature is activated for administrators in CA...

1
  • I will be installing through powershell and central admin. Is there a way to sorta embed this setting in the wsp file so when I install the wsp file it will just have the site collection feature enabled by default?
    – omega
    Aug 29, 2013 at 22:49
1

If you don't separately disable the feature, then yes, when you uninstall and then reinstall a solution it will be previously enabled. This is by design; I for one have done this with features over several websites and it would be a huge PITA to re-enable the feature on all of them.

To your question, the best way I can think of to do this is by putting together a feature receiver. Right-click the feature you're installing the solution on and select "Add Event Receiver". That will create some code which is mostly commented out. Just find the code that coincides with the right spot in the lifecycle (in your case, you'll want FeatureActivated with an -ed at the end, not FeatureActivating), and then either call the specific sites you want to disable the feature at or iterate through all the sites in the web application. Then you'd just want to find the feature in the site's feature store by the feature GUID and run the disable() method on it.

I'd provide you with code but unfortunately I don't have SP installed on this device. Otherwise, make sure you dispose of the SPSite objects you call!

2
  • Actually I want to activate the feature by default. With that, are your instructions the same? Is there another event receiver I should be looking at now?
    – omega
    Aug 29, 2013 at 23:10
  • Yeah, exactly the same thing. You'd use the Feature Receiver to activate the features on the sites in question (or iterate through all of them). Aug 29, 2013 at 23:34

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.