Solved

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

Posted on 2010-08-16
6
499 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

For SharePoint sites, particularly public-facing ones, there are times when adding JavaScript, Meta Tags, CSS Styles or other content to the page <head> section is more practical than modifying master pages.  For instance, you could add the jQuery l…
Microsoft SharePoint Foundation 2010 and Microsoft SharePoint Server 2010 do not offer the option to configure the location of the SharePoint diagnostic trace log files during installation.  This can, however, be configured through Central Administr…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

829 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