Solved

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

Posted on 2010-08-16
6
500 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Work Over Net is a new and very powerful collaboration product. With its new easy interface it is becoming very competitive to other similar products like webex and office interactive. WON 2010 have the standard business tools needed for multi-offic…
When installing SharePoint 2010 RTM I came across a strange error, I was getting timeouts during the installation. I searched the web and found the best solution to be found here (http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010genera…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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