Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);
}
share|improve this question

1 Answer 1

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.

share|improve this answer
    
Do you have any more info regarding bulk upload –  John Jun 22 '12 at 20:43
    
See blogs.msdn.com/b/brianwilson/archive/2007/03/05/… . I have written a similar code some time back, and back then event receiver failed to run when the administrator did a bulk upload (i.e. open in folder explorer and drag and drop files). So you might have to handle this scenario. I also found this nice article which might help you msdn.microsoft.com/en-us/library/aa979520.aspx. –  sssreddy Jun 22 '12 at 22:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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