Welcome to MSDN Blogs Sign in | Join | Help

Pranab Paul's Blog - Development Tips on SharePoint, Office and Web

------------------------------------------Web Parts, Workflow, InfoPath Form Services, Features, Site Definition, Event Receivers, Excel Services, Business Data Catalog (BDC), Search

News

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)

Posted: Wednesday, July 04, 2007 12:31 AM by pranab

Comments

taki said:

Well, where do you insert this code?

# August 2, 2007 11:09 AM

pranab said:

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.

# August 3, 2007 11:22 AM

david said:

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

# August 7, 2007 4:29 PM

Garry Jay said:

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.

# August 14, 2007 3:45 AM

Krishna said:

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

# September 1, 2007 7:36 AM

Jaya Borra said:

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.

# September 6, 2007 12:15 AM

Vivek Soni said:

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

# October 2, 2007 9:45 AM

Sowmya said:

Hi ,

How to Set permissions for individual versions of a Document

Sowmya

# October 3, 2007 8:27 AM

Venkat said:

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)

# October 10, 2007 12:31 AM

CorumMc said:

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.

# October 30, 2007 10:34 AM

Pranab Paul's Blog - SharePoint 2007 (MOSS/WSS 3.0) Development Tips said:

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

# February 14, 2008 1:47 PM

Noticias externas said:

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

# February 14, 2008 2:05 PM

deepakbadki@gmail.com said:

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

# February 22, 2008 1:33 AM

Raja said:

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

# March 14, 2008 10:56 AM

Crucial said:

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.

# March 20, 2008 5:17 PM

Liham said:

Hi Great Thanks a lot

Can i use this in an eventhandler itemadded ?

Good Day

# March 27, 2008 9:51 AM

beekermd03 said:

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!

# May 9, 2008 1:26 PM

Nilesh Thakkar said:

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

# June 3, 2008 10:10 AM

swati khera said:

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 .

# June 20, 2008 2:46 PM

SrinivasReddy said:

How to set the Item level permissions using out of the box webservice in moss 2007?

# November 6, 2008 12:47 AM

Shady said:

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

# January 22, 2009 3:42 AM

PJMueller said:

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

# February 5, 2009 12:18 PM

Jeena said:

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

# February 24, 2009 1:29 AM

pete w said:

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

# April 6, 2009 11:12 PM

Ali said:

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

# July 4, 2009 3:50 AM

shafaqat said:

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

# July 7, 2009 7:03 AM

Geetha said:

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.

# August 6, 2009 1:26 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker