How to set Item Level Permission for SharePoint 2007 (MOSS/WSS) List/Document Library Programmatically

How to set Item Level Permission for SharePoint 2007 (MOSS/WSS) List/Document Library Programmatically

Rate This
  • Comments 31

Here is a piece of code (a function) to set Item Level Permission. You can use it as a Web Method in a custom Web Service. This method can be used from Applications outside of SharePoint, provided the user using this application has sufficient privilege to update lists/libraries etc.

 

    public string ItemPermission(string SitePath)

    {

        string ReturnVal = "";

        try

        {

            SPSite WebApp = new SPSite(SitePath);

            SPWeb Site = WebApp.OpenWeb();

            SPList list = Site.Lists["TestDocLib"];

            SPListItem item = list.Items[0];

            SPRoleDefinition RoleDefinition = Site.RoleDefinitions.GetByType(SPRoleType.Contributor);

            SPRoleAssignment RoleAssignment = new SPRoleAssignment("<domain>\\<user>", "email", "name", "notes");

            RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);

            if(!item.HasUniqueRoleAssignments)

            {

                item.BreakRoleInheritance(true);               

            }

            item.RoleAssignments.Add(RoleAssignment);

            item.Update();

        }

        catch (Exception ex)

        {

            ReturnVal += "Permission not set, reason: " + ex.Message;

        }

        return ReturnVal;

    }

See also: How to set Item Level Permission for SharePoint 2007 (MOSS/WSS) List/Document Library Programmatically (Part 2)

Leave a Comment
  • Please add 5 and 3 and type the answer here:
  • Post
  • Well, where do you insert this code?

  • Hi Taki,

    This code can be used by the administrator or users with sufficient privilege to change item level permission for the other users. This code can be implemented in a custom web part, in an application (Layouts) page or in a custom web services.

  • How could you use this code to restrict permission to a folder or document level of a document library?

  • Thanks for the useful code. But in some cases it's much better to take some tools that can make just the same thing in several clicks. In this situation I would use something like <a href="http://dl.scriptlogic.com/landing/beta/Security-Explorer-for-SharePoint.aspx">security explorer for SharePoint</a>. I found this tool not too long ago but I'm amazed at how it makes common permission operations easier. For example, using tree-like view of SharePoint farm and all sites you can manage SharePoint security permissions, permission levels and SharePooint groups in the most easiest and visual way.

  • Hi Pranab,

    This code works superbly when i want to assign permission for a individual user.

    How can i modify & use this code to set ItemLevel Permissin for a share point group (instead of a person as u did in above code)

    Your suggestion will help a lot.

    Thanks and Regards

    Krishna

  • Hi,

    thanks for the useful code. Can you please tell me how can I use the code for setting page level permission instead of item level? Its very urgent for me, I am trying to find the solution from past 2 days. If you know any way can you please guide me in right direction?

    Thanks in advance

    Jaya Borra.

  • How can i set the permission for a group of people.

  • Hi ,

    How to Set permissions for individual versions of a Document

    Sowmya

  • Hi

    We have a client requirement. The client created a default template where in he will specify the each user role for document library under project site. So when the client creates a new project site, the permissions for each of document library should inherit from this custom template rather than MOSS default permission settings. Is it possible to programatically over ride the MOSS default permission setting and give rights using custom template?

    Please let us know the details (including table names, sample code if possible)

  • Thanks for the code. Is there a way to set permissions to a view? I have a registration form with several views filtered by a dropdown list of locations. I need to restrict various groups read access to this list to only see the view sorted by their location.

  • Requirement: I have a list and have made settings wherein the user can edit only the items created by

  • Requirement: I have a list and have made settings wherein the user can edit only the items created by

  • Thanks for the code snippet.

    This code uses SP Object Model. What if I dont have access to SP Server and I cannot deploy the custom web service. Is there any way to implement the same functionality using WSS Web Service?

    Thanks,

    Deepak

  • Hi,

    I converted your code into console app to test

    once I ran the code (no errors while executing) try to see like browse user permissions and other pages system says “file not found!!”

    do you know how to fix , Appreciate your help

    My Env. Moss 2007, Single farm

    Thank you

    Regards,

    Raja

  • Someone above was wondering how to establish the above example with a group.

    Simply replace:

    SPRoleAssignment RoleAssignment = new SPRoleAssignment("<domain>\\<user>", "email", "name", "notes");

    RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);

    With the following:

    SPGroup spGroup = WebApp.Groups["NameOfGroup"];

    SPRoleAssignment RoleAssignment = new SPRoleAssignment(spGroup);

    RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);

    item.RoleAssignments.Add(roleAssignment );

    Everything else should be the same.

Page 1 of 3 (31 items) 123