sharepointsharepoint-listsplist

How to restrict editing in SP List only to own Items?


I'm looking for possibility to define following options for Sharepoint list. I know it can be done from interface, but how to make if from XML or code? Can I set this somewhere in List Definition or List Instance:


Solution

  • You are most likely looking for the RoleAssignments property that is available for most scopes.

    Suppose you could start with something like this

    private void DoStuff() 
    {
        SPList list = web.Lists["MyList"];
    
        // Create custom role
        SPRoleDefinitionCollection roles = web.RoleDefinitions;
        SPRoleDefinition roleDefinition = roles["Contribute"];
        roleDefinition.BasePermissions = SPBasePermissions.AddListItems |
            SPBasePermissions.BrowseDirectories |
            SPBasePermissions.EditListItems |
            SPBasePermissions.DeleteListItems |
            SPBasePermissions.AddDelPrivateWebParts;
        roleDefinition.Update();
    
        //Creates a new role assignment for a group
        SPGroup myGroup = web.SiteGroups["Team Site Members"];
        SPRoleAssignmentCollection roleAssignments = web.RoleAssignments;
    
        // SPRoleAssignment accepts a SPPrincipal which can be a SPUser or SPGroup
        SPRoleAssignment roleAssignment = new SPRoleAssignment(myGroup);
    
        //add a new role definition to the bound role definitions for the role assignment
        SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
        roleDefBindings.Add(roles["Contribute"]);
    
        //Add the new role assignment to the collection of role assignments for the site.
        roleAssignments.Add(roleAssignment);
    
        // Stop inheriting permissions
        list.BreakRoleInheritance(true);
        list.RoleAssignments.Add(roleAssignment);
        list.Update();
    }