Tschareck
Tschareck

Reputation: 4239

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:

Upvotes: 2

Views: 1873

Answers (2)

Eric Herlitz
Eric Herlitz

Reputation: 26277

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();
}

Upvotes: 1

Tschareck
Tschareck

Reputation: 4239

Trikks, this is one solution, but you had me thinking.
Turns out, the thing I needed is WriteSecurity. I added feature receiver, and in FeatureActivated I set SPList.WriteSecurity = 2 on this list.

I found this MSDN docu, http://msdn.microsoft.com/en-us/library/dd588628(v=office.11).aspx
I suppose it's possible to set this in code, but where?

I added this part to schema.xml in ListDefinition, as in documentation, but this doesn't work. After deployment and creating new list, I go to List Settings -> Advanced Settings and check 'Create and Edit access'. Still, checked is the first option, not second.

Upvotes: 2

Related Questions