0

Just curious to know this. Suppose we have a site collection with 3 sites (1 root site + 2 sub-sites). We create a feature in VS to create a new list.

Foll. is the code:-

foreach (SPWeb web1 in site.AllWebs)
                {
                    //create new list
                }

Case 1:- Set the scope to Site.

Case 2: Set scope to Web.

In both above cases, a new list is created in each sub-site. Then, what is the purpose of feature scope?

Is it only to set a place from where this feature can be activated from? (Ex: Web :: Manage Site Feature; Site :: Manage Site Collection Features)?

1

This really depends on what you want to achieve. Suppose you have fixed number of sub sites in your site collection and you know that you won't be adding any new subsite in future. And in each subsite you need a list. So in this case setting the scope to site collection will help as you will activate the feature only once and write code which will iterate through all sub sites and create a list. However, if the number of sub sites is not fixed, setting the scope to site collection will not help as the feature will create lists only in sub sites present in site collection at the time of activation. So a web scoped feature will make more sense so that whenever a new sub site is created, you will activate a feature which will create a list in that sub site.

Another example: Suppose you have a site collection inside which are sub sites for different departments like Sales, Marketing etc. And now you create a functionality which is needed by Sales team only (or may be some other team wants it in future). Here again web scoped feature will make more sense.

6
  • In your para1, suppose I activate feature once the new site is created then wont it throw error because the list with same name already exists in other sites? – variable Mar 20 '14 at 7:41
  • No it won't throw error as the list with same name will be created inside that particular site. Note that we can have same named list in different subsites but not in same site. – Nadeem Yousuf-AIS Mar 20 '14 at 7:47
  • But list with same name already exists in other site..The feature may have been already activated in one web and activating it in another web will try to create the lists again. – variable Mar 20 '14 at 7:59
  • Say you have web1 and you activate a feature and it will create a list named "ABC" in it. After some time you create web2 and activate feature in it. This time it will create a list named "ABC" in web2 not web1. Hence no error. – Nadeem Yousuf-AIS Mar 20 '14 at 8:09
  • But my code is:- foreach (SPWeb web1 in site.AllWebs) { //create new list } So, it is creating list in each web of site collection... – variable Mar 20 '14 at 8:41
0

In your case the scope does not matter functionality wise, but truly it does matter logically and permission wise for your user.

If web scoped, a user with full control of a subsite only will be able to activate this feature. This does not feel right. The only logical thing is to have it scoped to Site collection, so that only the Site collection administrator sees it and can activate it!

0

You should do it in a site scoped feature to avoid conflicts. The feature will be available in only one place (site collection features).

With web scoped feature it will be available in every web. The feature may have been already activated in one web and activating it in another web will try to create the lists again. Also in this case a feature activated in a subsite modifies the root site which is not common and may be cofusing which subsite has created the list in the root web.

Also SPFeatureEventProperties has differnet properties in differently scoped features, though that's not critical in your case.

4
  • OK, but suppose wsp is deployed to the 2nd website. Then, still will it be visible under manage site features for all other websites also? – variable Mar 20 '14 at 7:23
  • OH, WSP can be deployed to only GAC or web application. Sorry. I think my above comment is invalid? – variable Mar 20 '14 at 7:23
  • Yes, farm solution can be deployed to one or more web application while sandboxed solution are deployed on site collection level. In any case a web scoped feature will be available in every web in a web application/site collection. – Naim Murati Mar 20 '14 at 8:12
  • Ok so I can also access other web applications from this SP server OM code. Then what is the meaning of feature scope. I thought a site level scoped feature wont get access to web app scope. I am confused – variable Mar 20 '14 at 18:17

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.