3

I am trying to create an event reciever on a document library that on creation it removes all permissions and gives the author contribute writes. All other users get nothing.

I have the following

element.xml

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="101">
      <Receiver>
        <Name>Asset_Store_EventsItemAdded</Name>
        <Type>ItemAdded</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>Asset_Store.SP.EventReceivers.Asset_Store_Events.Asset_Store_Events</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>

  </Receivers>
</Elements>

And then my code

public override void ItemAdded(SPItemEventProperties properties)
{
    base.ItemAdded(properties);


    var item = properties.ListItem;

    item.BreakRoleInheritance(true);
    // Remove All Permissions
    while (item.RoleAssignments.Count > 0)
    {
        item.RoleAssignments.Remove(0);
    }

    var web = properties.Web;

    var authorID = int.Parse(item.Properties["Author"].ToString());
    var user = web.Users[authorID];


    SPRoleDefinition spRole = web.RoleDefinitions["Contribute"];

    SPRoleAssignment roleAssignment = new SPRoleAssignment(user.LoginName, user.Email, user.Name, user.Notes);
    roleAssignment.RoleDefinitionBindings.Add(spRole);

    item.RoleAssignments.Add(roleAssignment);

    item.Update();

}

However when I add a document to the library nothing is happening... What could be going wrong?

Update

It does help if you include the event handler in your feature...

public override void ItemAdded(SPItemEventProperties properties)
{
    base.ItemAdded(properties);

    var item = properties.ListItem;

    item.BreakRoleInheritance(false);


    var web = properties.Web;

    var authorLoginName = (string)item.Properties["vti_author"];
    var user = web.Users[authorLoginName];


    SPRoleDefinition spRole = web.RoleDefinitions["Contribute"];

    SPRoleAssignment roleAssignment = new SPRoleAssignment(user.LoginName, user.Email, user.Name, user.Notes);
    roleAssignment.RoleDefinitionBindings.Add(spRole);

    item.RoleAssignments.Add(roleAssignment);
}
4

Have you considered using the SPList.ReadSecurity and SPList.WriteSecurity (see this post). This allows you to set contribute permissions to the particular item only for the current user and remove all role assignments for others. Also event receivers might not work if you are doing bulk upload.

If that does't work for you, in your event receiver, you can stat by checking if the event receiver is actually registered. You can check the properties SPList.EventReceivers, SPWeb.EventReceivers, SPSite.EventReceivers depending on the event receiver scope.

Also you might want to replace

item.BreakRoleInheritance(true);
    // Remove All Permissions
    while (item.RoleAssignments.Count > 0)
    {
        item.RoleAssignments.Remove(0);
    }

with item.BreakRoleInheritance(false);.

Also verify if you are actually getting SPUser object in user variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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