Solved

how can I set a spfeature to a specific web on sharepoint

Posted on 2010-08-16
6
504 Views
Last Modified: 2012-05-10
Using MOSS (SP 2007), I need to add a feature not just to be scoped at web level but also to be activated in a given web ( ... by using feature.xml?)

Alternately, Is there any way to do not allow a feature to be activated based on a library existance? e. g. if a given library exists in a web, allows the feature to be activated, otherwise deactivate feature on SPFeatureActivated event.

Any suggestions? Other ways of getting the expected result, which is do not allow to activate a feature in every indivually web but only in those that a given condition is met?

Thanks in advance,


Edja
0
Comment
Question by:edgarmora4033
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 21

Expert Comment

by:66866
ID: 33455007
When you are creating the Feature.xml file, you can specify a "ReceiverAssembly" property that points to a DLL and a "ReceiverClass" that points to the class where you can specify custom code on what to do when the feature is activated and how to install to a specific library/site.
http://msdn.microsoft.com/en-us/library/bb862634(office.12).aspx
0
 

Author Comment

by:edgarmora4033
ID: 33469163
66866:

Is this the only way of doing it? That means, inside the FeatureActvated event?

As far as I know, this solution does not prevent a feature from being activated in a web. Unless, there is a way to cancel the feature activation from inside the code, which, by way I have not found it yet.

I am copy-pasting a skeleton of what I would look like inside code:
 
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
   SPWeb web = SPContext.Current.Web;
   SPList list = web.GetList("/Lists/MyList");

   if (list != null)
   {
      // do your stuff
      // allow feature to activate inside web
   }
   else
   {
      // DO NOT ALLOW FEATURE TO BE ACTIVATED
      //    OR SELF DEACTIVATE FEATURE
   }
}

I have not found any way to achieve the else section. Does this approach make sense? Would be possible to cancel the activation of a feature or self-deactivate it using code at runing time?

Any other ideas?

Thank you in advance,


Edja
0
 
LVL 21

Expert Comment

by:66866
ID: 33501457
Yes, you will have to use a "Try..Catch" method since searching for a list that does not exist will throw an exception. Try this and let me know how it works.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
   SPWeb web = SPContext.Current.Web;
Try
{
   SPList list = web.GetList("/Lists/MyList");
   // Activate feature
}
Catch //the list does not exist
{
 //do nothing
}
 
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:edgarmora4033
ID: 33636747
66866:

Thank you for  your comments. Does it mean that the feature is activated with no functionality in case the given list is not present, right? It does not prevent from activating the feature itself.

Is there other way as for example cancelling activatin of a feature?

Thank you,


Edja
0
 
LVL 21

Accepted Solution

by:
66866 earned 500 total points
ID: 33637409
If you don't specify a list, then the feature will apply to all items. You cannot deactivate a feature within feature activatation. that's how the feature architecture has been setup.
since feature activation is usally done by the Site administrator, there is very little scope of causing error when this occurs. If a feature activation is dependant on a specific library/list then the developer would either create this list/library as part of the feature activation.
0
 

Author Closing Comment

by:edgarmora4033
ID: 33800244
Thank you for your help.

Edja
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you create your solutions on SharePoint sooner or later you will come upon a request to set  permissions of the item depending on some of the item's meta-data - the author, people assigned as approvers, divisions, categories etc. The most natu…
SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done? Scenario For a recent pr…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question