0
votes

I have created a custom feature for sharepoint 2007 using visual studio 2010. When I activate the feature it of course fires on all document libraries in the site collection. can someone give me an example of how to make the feature fire on a specific document library/list instance.

1

1 Answers

0
votes

First you'll have to add an EventReceiver to your feature and then in your Feature's xml add a ReceiverClass, like this:

<Feature  Id="f68efad8-ea0a-42a2-9994-db3b74aa67f8"
      Title="My features title"
      Description="Blah blah blah"
      Version="12.0.0.0"
      Hidden="FALSE"
      Scope="Web"
      DefaultResourceFile="core"
      ReceiverAssembly="MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c4f34f956cd0552b"
      ReceiverClass="MyProject.FeatureCode.EventHandler" <!-- This is where you set the EventReceiver -->
      xmlns="http://schemas.microsoft.com/sharepoint/">

EventHandler being the EventReceiver when you're feature is activated.

My example
First of, my eventreceiver:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        var assembly = typeof(PermissionHandler).Assembly.ToString();
        var classList = typeof(PermissionHandler).FullName;

        var web = SPContext.Current.Web;
        web.AllowUnsafeUpdates = true;
        try
        {
            var list = web.Lists["MyList"];
            list.EventReceivers.Add(SPEventReceiverType.ItemAdded, assembly, classList);
            list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assembly, classList);
        }
        catch (Exception ex)
        {
            EventLogger.LogError("Sample feature failed to run.", this, ex);
        }

    }

In the above example I want to add some permissions to the elements in MyList. As you can see I make 2 variables which is the typeof(PermissionHandler), which is a public class I've created to do the job.
I have added 5 items to the list before activating this feature, so I want the already existing items to also get the permissions I'm setting for the new items.

This is how I do it:

private void updateItemPermissions(SPItemEventProperties properties)
    {
        DisableEventFiring();
        SPListItem listItem = properties.ListItem;
        SPSecurity.RunWithElevatedPrivileges(() =>
        {
             SPSite site = new SPSite(listItem.ParentList.ParentWeb.Site.ID);
             SPWeb web = site.OpenWeb(listItem.ParentList.ParentWeb.ID);
             SPList list = web.Lists[listItem.ParentList.ID];
             SPListItem item = list.Items.GetItemById(properties.ListItem.ID);

             item.BreakRoleInheritance(true);
             if (item.RoleAssignments.Count > 0)
             {
                 for (var i = item.RoleAssignments.Count - 1; i >= 0; i--)
                      item.RoleAssignments.Remove(i);
             }

             var group = item.Web.Site.RootWeb.Groups["Visitors"];
             AddPermissions(item, web, SPRoleType.Reader, group);

        });

        EnableEventFiring();
    }

    private static void AddPermissions(SPListItem curItem, SPWeb web, SPRoleType roleType, SPPrincipal principal)
    {
        SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(roleType);
        SPRoleAssignment roleAssignment = new SPRoleAssignment(principal);
        roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
        curItem.RoleAssignments.Add(roleAssignment);
        curItem.Update();
    }



I hope this helped you :)