1

In our SharePoint 2010 environment, we want to enforce the users to choose the option "show user names in the survey list" (survey list) however there may be a time when user may choose "No" to this option instead of "Yes".

so we decided to utilize the ListAdded event receiver.

the below code is in my mind.

public class DoNotCreateSurvey : SPListEventReceiver
{ 
   public override void ListAdded(SPListEventProperties properties)
   {
       base.ListAdded(properties);

       SPList list = properties.List;

       // revert the list settings whenever a end-users chooses anonymous surveys
       if (list != null && !list.ShowUser && list.BaseType.ToString().Equals("Survey", StringComparison.CurrentCultureIgnoreCase)){
           this.EventFiringEnabled = false;
           list.ShowUser = true;
           list.Update();

           // put notification code here
           this.EventFiringEnabled = true;
       }
   }
}

I know, it can be deployed against a subsite however the same feature should be available/associated with all the web-applications(all the site-collections & all the sub-sites which are all already exists and the future once) are in the farm.

let me know if it can be used like this:

    <Feature Id="C8F0E2DA-846D-43c0-84B1-56463178E91A"  
      Title="SurveyListEnforceShowOption"  
      Description="Should enforce the option to show the user name in the survey list"
      Scope="Farm"  Hidden="true"  
      xmlns="http://schemas.microsoft.com/sharepoint/"
      ReceiverAssembly="assemblyInfo"
      ReceiverClass="classInfo">
    </Feature>

I do not think a feature stapling is required here however feel free to suggest on this..

Update

As suggested, the above feature can't be used.

Please let me know if the below can be used.

Feature.xml

    <Feature 
      Scope="Site" 
      Title="Survey list update" 
      Id="C8F0E2DA-846D-43c0-84B1-56463178E91A" 
      xmlns="http://schemas.microsoft.com/sharepoint/">
      <ElementManifests>
        <ElementManifest Location="Elements.xml"/>
      </ElementManifests>
    </Feature>

Element.xml

    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Receivers 
        **ListTemplateId="102">**
        <Receiver>
          <Name>SurveryShowUser</Name>
          <Type>ListAdding</Type>
          <SequenceNumber>10000</SequenceNumber>
          <Assembly>AssemblyInfo</Assembly>
          <Class>ClassInfo</Class>
        </Receiver>
      </Receivers>
    </Elements>

Feature Stapping

Feature.xml

    <Feature Id="4AF9999A-0517-4224-9ED3-D2F9F87D92E2"
            Title="demo.Survey.FeatureStapling"
            Description="Description."
            Version="1.0.0.0"
            Scope="WebApplication"
            Hidden="FALSE"
            xmlns="http://schemas.microsoft.com/sharepoint/">
      <ElementManifests>
        <ElementManifest Location="stapling.xml" />
      </ElementManifests>
    </Feature>

Stapling.xml

    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
        <FeatureSiteTemplateAssociation Id="C8F0E2DA-846D-43c0-84B1-56463178E91A" TemplateName="STS#0" />
        <FeatureSiteTemplateAssociation Id="C8F0E2DA-846D-43c0-84B1-56463178E91A" TemplateName="STS#1" />
        <FeatureSiteTemplateAssociation Id="C8F0E2DA-846D-43c0-84B1-56463178E91A" TemplateName="STS#2" />
        <FeatureSiteTemplateAssociation Id="C8F0E2DA-846D-43c0-84B1-56463178E91A" TemplateName="........." />
        <FeatureSiteTemplateAssociation Id="C8F0E2DA-846D-43c0-84B1-56463178E91A" TemplateName="........" />
        <FeatureSiteTemplateAssociation Id="C8F0E2DA-846D-43c0-84B1-56463178E91A" TemplateName="MYSITEPER" />
    </Elements>

Installation:

  1. Install-SPFeature -path "surveyShowUser"
  2. Install-SPFeature -path "surveyShowUserStappling"
  3. Enable-SPFeature -identity "surveyShowUserStappling" -URL http://

Is it ok? or am I missing something here?

kindly help..

2 Answers 2

4

By design ListAdded can be hosted by SPWeb or SPSite only as documented here. So Feature Stapling cannot be avoided if you want to enforce that across the farm.

UPDATE: As per MSDN Documentation, If the Receivers tag has no ListTemplateId or ListUrl attribute, the event receiver is registered at the same scope as the Feature. Which means you should not use ListTemplateId and ListUrl.

On Feature Stapling, as another answer suggests, you should use GLOBAL template to attach to all installed Site Templates.

Another thought: I am not sure you are covered by just capturing the ListAddedEvent. Those having enough access can also change this setting later on.

2
  • can you please suggest me on feature stapling stuff.. Commented Oct 6, 2011 at 19:28
  • Yes, this option is also blocked in the list general settings. Commented Oct 13, 2011 at 5:56
2
+25

To elaborate some more about the stapling solution.

As Microsof says for SharePoint 2007 (but the same is valid for the 2010 version) "Feature stapling is a concept that allows you to attach (or staple) a SharePoint Feature to a SharePoint site definition without modifying the original site definition files in any way". In that sense, stapling is a valid solution when you need to apply some new logic to an existing template (OOTB or a custom template - editing templates that are in use is not supported and can lead to problems).

In your case you could try to staple your custom feature to all the available site templates so that anytime a new site will be created your feature will be activated by default. This page on MSDN provides more info about the topic. You should use something like:

<FeatureSiteTemplateAssociation 
Id = "your custom feature guid" 
TemplateName = "GLOBAL"> 

Basically you must define a stapler feature that will attach your secondary "staplee" feature to a specific site definition (or multiple site definitions if you need to, see the following comments for more details). In the stapler feature you will define the id of the feature to staple and the name of the template the feature should be stapled to. As a result, any new site created using the template will automatically end with your feature active.

NOTE: you should use the template name GLOBAL to staple your feature to all the available templates. Some documentation sources uses the name "GLOBAL#0" but from what I saw it won't work that way.

Also notice that this solution won't work for any already-created site (someone please tell me if I am wrong on this point).

(Edited to add some more explanation about Feature Stapling)

2
  • 1
    if you staple to global, remember that Blank site is not included, hence you must staple to STS#1 also Commented Oct 10, 2011 at 14:18
  • I knew that I had forgot something. Ander is right, any template that sets the AllowGlobalFeatureAssociations property to false will be excluded from a GLOBAL stapling. The blank site template is one example of such templates - be carefull if your environment includes third-party templates that could use that property (better be safe than sorry). Commented Oct 10, 2011 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.