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 created a Feature named Feature1. Feature1 is activated in a SharePoint site. I create a Web Part that contains the following code.

SPSite site = new SPSite("http://intranet/site1");

SPWeb web = site.OpenWeb();

SPFeatureDefinition feature = SPFarm.Local.FeatureDefinitions("Feature1");

I need to modify the Web Part to activate Feature1 in Site1 only.

Should I use? site.Features.Add(feature.Id) or web.Features.Add(feature.Id)

Whats the difference between the two instructions?

share|improve this question

4 Answers 4

up vote 2 down vote accepted

In the object model, an SPSite is a Site Collection, whereas an SPWeb is a site. Yes, it is a little confusing.

So if you have a feature that needs to be activated at the Site level (not Site Collection level), then you want to use the SPWeb, which your code calls web.

share|improve this answer

The difference is the scope of the Feature, which depends on the content of the feature. Here's a starter for reading material about this:

http://weblogs.asp.net/soever/archive/2007/05/03/sharepoint-features-elements-scope-and-other-info.aspx

share|improve this answer
if (feature.Scope == SPFeatureScope.Web)
 web.Features.Add(feature.Id)
else if (feature.Scope == SPFeatureScope.Site)
 site.Features.Add(feature.Id)
share|improve this answer

Luis based on your question

I need to modify the Web Part to activate Feature1 in Site1 only

You should use
web.Features.Add(feature.Id);

share|improve this answer

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.