Freigeben über


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

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)

Comments

  • Anonymous
    August 02, 2007
    Well, where do you insert this code?

  • Anonymous
    August 03, 2007
    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.

  • Anonymous
    August 07, 2007
    How could you use this code to restrict permission to a folder or document level of a document library?

  • Anonymous
    August 13, 2007
    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.

  • Anonymous
    August 31, 2007
    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

  • Anonymous
    September 05, 2007
    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.

  • Anonymous
    October 02, 2007
    How can i set the permission for a group of people.

  • Anonymous
    October 03, 2007
    Hi , How to Set permissions for individual versions of a Document Sowmya

  • Anonymous
    October 09, 2007
    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)

  • Anonymous
    October 30, 2007
    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.

  • Anonymous
    February 14, 2008
    Requirement: I have a list and have made settings wherein the user can edit only the items created by

  • Anonymous
    February 14, 2008
    Requirement: I have a list and have made settings wherein the user can edit only the items created by

  • Anonymous
    February 21, 2008
    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

  • Anonymous
    March 14, 2008
    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

  • Anonymous
    March 20, 2008
    Someone above was wondering how to establish the above example with a group. Simply replace: SPRoleAssignment RoleAssignment = new SPRoleAssignment("<domain>&lt;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.

  • Anonymous
    March 27, 2008
    Hi Great Thanks a lot Can i use this in an eventhandler itemadded ? Good Day

  • Anonymous
    May 09, 2008
    I was wondering if you knew a way to set permissions to a customized page.  I created a custom page and attached a master page using SPD.  My entire site is public facing, and several areas are accessible.  I have lists that are locked down by permission based and was wondering if I could apply that permission idea to an individual page.  Do you know if this is possible?  Any ideas would be great!

  • Anonymous
    June 03, 2008
    Thanks for the article. I want to set item level permission, but without using SP Object Model. Is there any way to do the same thing without using SP Object model, by Using WSS Web Service ?? Thanks, Nilesh Thakkar nilesh_mscit@hotmail.com

  • Anonymous
    June 20, 2008
    i am new to MOSS.I want to assign permission for the selected item (which user selects ).Please let me know how to access it through your code .

  • Anonymous
    November 05, 2008
    The comment has been removed

  • Anonymous
    January 21, 2009
    hi there according to this site : https://blogs.pointbridge.com/Blogs/morse_matt/Pages/Post.aspx?_ID=8#EntryTabs but with no luck :( can you show me how to do that in moss ? what im trying to do is im creating a doc that is attached with a workflow , once the item is created i just want me and the approvers to see that document ! , how can that be done ? any help please :( in other words i want list security permissions to be on a DOC ( library ) , how can that be done ? thanks

  • Anonymous
    February 05, 2009
    How do you add a group that is in AD, but not explicitely set up as a SharePoint group?  You can do this if you go into SharePoint, however i need to do it programmatically.  thx

  • Anonymous
    February 23, 2009
    HI I have created specific Views in the particular SP site and now I need to ensure that my client can access only specific views that are meant for their eyes. I do not want them to See or access the other views from the View drop-down. Is there a way to restrict acces to views? I tried by creating a new library and then copying the .aspx view file but it did not work. Appreciate your help. Thanks much Jeena

  • Anonymous
    April 06, 2009
    Sorry to be nitpicky, but you should be using the using(...){} blocks in there to ensure those unmanaged objects are properly disposed. Thanks for the code example though I get the point

  • Anonymous
    July 03, 2009
    Hi thanks in advance, i have a document library in which arround 1 lac documents are there, in 1 lac document arround on 250 document i want to apply permission, all document has fields filled, the 250 documents on which i want to apply permissions  have got a field which is unique, now i want to assign permission on bulk basis, in a single click or through program, can you please help how to achieve this. Thanks

  • Anonymous
    July 06, 2009
    hi how can we add filed level permissions for users and groups. My list has some fields, i want to show some fields to UserA and some other Fields to UserB, is it possible to do so, thanx

  • Anonymous
    August 05, 2009
    can u pls tell me how to provide access for the user to a site programatically?? i tried with the following code, permissions are assigned and i can view the name in the permissions page of site settings, but the user cannot able to access the site. SPSite site = new SPSite("mysite"); // Opening the website : - (Actually we are making site live for getting the interface. SPWeb web = site.OpenWeb(); // Allowing the update without applying security constraints. web.AllowUnsafeUpdates = true; web.SiteUsers.Add("domain\user", "useremail", "useremail", "notes");            web.Update(); SPPrincipal user = null; user = web.AllUsers["domain\user"]; // Getting user for assigning the the role.   SPUser spuser = null; // Getting user properties from sharepoint. spuser = web.AllUsers["domain\user"]; spuser.Update(); // Getting role definitions information. SPRoleDefinition roleDefinition = null; roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Reader); //web.RoleDefinitions["Full Control"]; // Creating collections of roles SPRoleDefinitionBindingCollection roleDefinitionBindings = new SPRoleDefinitionBindingCollection(); roleDefinitionBindings.Add(roleDefinition); // Assigning the role to user. SPRoleAssignment roleAssignment = new SPRoleAssignment(user);   roleAssignment.ImportRoleDefinitionBindings(roleDefinitionBindings);  web.RoleAssignments.Add(roleAssignment); web.Update(); web.EnsureUser("domain\user"); kindly help ASAP.

  • Anonymous
    December 04, 2009
    Can anyone help me how to get list of groups user present in so that i can give permissions to all of his groups including him.Thanks

  • Anonymous
    January 20, 2010
    where do you insert it ? can anyone explain it simply ? i'm new to designer

  • Anonymous
    January 20, 2010
    so where do u insert it ????????????

  • Anonymous
    November 15, 2010
    thank u very much... gd article...