Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-08-16
6
Medium Priority
?
520 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
  • 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Pimping Sharepoint 2007 without Server-Side Code Part 1 One of my biggest frustrations with Sharepoint 2007 in the corporate world is that while good-intentioned managers lock down the more interesting capabilities of Sharepoint programming in…
The vision: A MegaMenu for a SharePoint portal home page The mission: Make it easy to maintain. Allow rich content and sub headers as well as standard links. Factor in frequent changes without involving developers or a lengthy Dev/Test/Prod rel…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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