Solved

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

Posted on 2010-08-16
6
487 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:edgarmora4033
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for your help.

Edja
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

I recently came across an issue with a MOSS 2007 deployment where access into some sub-sites were denied, even for the MOSS farm administrators. A bit of background to the setup of this MOSS farm; this was a three server setup, consisting of a fr…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now