Programatically identifing feature for activation
-
29-09-2020 - |
Domanda
I have a requirement where a custom action is only added to the root site of the site collection. So planning to add two features one Site
scoped and the other Web
scoped.
In the event receiver of a Site
scoped feature, I'm activating the Web
scope feature.
For this i can use
web.Features.Add(webFeatureId);
How can I find the web level features feature id programmatically ?
Soluzione
Well. It really depends on which feature you have to activate.
The id of a feature does not change during its life. This means that if you can find out the id you will be sure it stays the same, regardless of the specific SharePoint farm instance. That said, you can "hardcode" your ID in a support class, much like SharePoint does with fields id in the SPBuiltInFields class.
If for any reason you can't know the feature ID beforehand you will need some other way to identify the feature - for example the display name. With that info you can use the FeatureDefinitions property of the SPWeb object to browse the installed 'Web' level features, search the one you need and add it to the Features collection to activate it as required.