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 (31)

  1. Vysakh says:

    thank u very much…

    gd article…

  2. anelka says:

    so where do u insert it ????????????

  3. anelka says:

    where do you insert it ? can anyone explain it simply ? i’m new to designer

  4. saritha says:

    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

  5. Geetha says:

    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.

  6. shafaqat says:

    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

  7. Ali says:

    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

  8. pete w says:

    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

  9. Jeena says:

    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

  10. PJMueller says:

    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

  11. Shady says:

    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

Skip to main content