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'm trying for like 5 days to achieve this event and i got to this but still not even close to what i want. I need to change permissions on file added in library like: user which added file to have full right, his manager same, his department colleagues read rights and rest of people no right. With this part of code i achived to BreakInheritance on item and remove "Advanced" menu from Shared with, but i still can edit file properties. Any ideeas please? Hope that i managed to describe well what i need, if not please ask.

public override void ItemAdded(SPItemEventProperties properties)
    {
        base.ItemAdded(properties);
        if (properties.ListTitle.Equals("Documents"))
        {
            using (SPSite site = new SPSite(properties.WebUrl))
            {
                site.AllowUnsafeUpdates = true;
                using (SPWeb web = site.OpenWeb())
                {
                    SPUser user = web.CurrentUser;
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        try
                        {
                            web.AllowUnsafeUpdates = true;
                            SPListItem li = properties.ListItem;
                            SPFile lf = web.GetFile(li.Url);

                            SPRoleType read = SPRoleType.Guest;
                            lf.Item.BreakRoleInheritance(true);
                            while (lf.Item.RoleAssignments.Count > 0)
                            {
                                lf.Item.RoleAssignments.Remove(0);
                            }
                            AssignPermissionsToItem(lf.Item, user, read);
                            web.AllowUnsafeUpdates = false;

                        }

                        catch (Exception ex)
                        {
                        }

                    });
                }
            }
        }
    }

public static void AssignPermissionsToItem(SPListItem item, SPPrincipal obj, SPRoleType roleType)
{
    if (!item.HasUniqueRoleAssignments)
    {
        item.BreakRoleInheritance(true);
    }

    SPRoleAssignment roleAssignment = new SPRoleAssignment(obj);
    SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
    roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

    item.RoleAssignments.Add(roleAssignment);
}
share|improve this question
    
It's better to put in your solution as an answer rather than editing the question; and then you can come back after 2 days and mark it as the answer. –  lgaud Aug 28 '13 at 17:52
    
I wanted to do that, but I'm new here and can't answer my question only after 10 hours. I'll it edit tomorrow. –  Alex Stefan Aug 28 '13 at 17:56

1 Answer 1

up vote 0 down vote accepted

Solved myself. So here is code for adding Read permissions on item added for current user. Hope someone will find this helpfull. If any question, don't mind to ask.

public override void ItemUpdated(SPItemEventProperties properties)
{
    base.ItemUpdated(properties);
    if (properties.ListTitle.Equals("Documents"))
    {
        using (SPSite site = new SPSite(properties.WebUrl))
        {
            site.AllowUnsafeUpdates = true;
            using (SPWeb web = site.OpenWeb())
            {
                SPUser user = web.CurrentUser;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    try
                    {
                        web.AllowUnsafeUpdates = true;
                        SPListItem li = properties.ListItem;
                        SPFile lf = web.GetFile(li.Url);

                        SPRoleType read = SPRoleType.Reader;
                        li.BreakRoleInheritance(false);
                        while (li.RoleAssignments.Count > 0)
                        {
                            li.RoleAssignments.Remove(0);
                        }
                        AssignPermissionsToItem(li, user, read);
                        web.AllowUnsafeUpdates = false;

                    }
                    catch (Exception ex){   }
                });
            }
        }
    }
}

public static void AssignPermissionsToItem(SPListItem item, SPPrincipal obj, SPRoleType roleType)
{
    SPRoleAssignment roleAssignment = new SPRoleAssignment(obj);
    SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
    roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

    item.RoleAssignments.Add(roleAssignment);
    item.Update();
}
share|improve this answer

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.