Home > Blogroll, development, event handlers, MOSS, MS SharePoint Sever, sharepoint, sps, Uncategorized > Setting Item Level Permissions Programmatically

Setting Item Level Permissions Programmatically


Here is how you can set item level permissions for a document library/list in sharepoint.


public string ItemLevelPermissions(string SiteAddress)

{

        string ReturnResult = "";

        try

        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {               
               using (SPSite WebApp = new SPSite(SiteAddressHere))
               {

                   using (SPWeb Site = WebApp.OpenWeb())
                   {

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

                      SPListItem item = list.Items[0];

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

                      SPRoleAssignment RoleAssignment = new SPRoleAssignment("<DomainNameHere>\\<UserNameHere>", "EmailAddressHere", "GiveNameHere", "GiveNotesHere");

                      RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);

                      if(!item.HasUniqueRoleAssignments)

                      {

                         item.BreakRoleInheritance(true);               

                      }

                     item.RoleAssignments.Add(RoleAssignment);

                     item.Update();

                  }
               }
           });
        }
        catch (Exception ex)

        {

            ReturnResult += "Sorry the permissions could not be set for the item due to the following exception: " + ex.Message;

        }

        return ReturnResult;
}

Hope this helps–

Advertisements
  1. December 10, 2009 at 1:53 pm

    Can you give me an idea of how this would be deployed? How would it be activated and used?

  2. December 11, 2009 at 2:13 pm

    Hi David,

    There could be multiple scenarios. One of them could be to put this into an event handler which would set permissions on any new item that is just added (i.e. on ItemAdded event). For writing, deploying and activating such an event handler, look here:

    https://rehmangul.wordpress.com/2009/11/17/writing-event-handlers-and-deploying-using-features/

    Just replace email code with the code above and you should be good to go!

    Cheers,

  3. Datta
    August 19, 2010 at 12:24 pm

    Hi

    I used you code to give permission to item. Now my requirment is that how I can rollback the permission.

  4. Ben
    February 10, 2012 at 4:15 am

    I’m not clear on what GiveNameHere and GiveNotes should be.
    I have an existing event receiver and I want to give the author who just created the item rights and deny everyone else who by default has access to the folder

  1. December 6, 2009 at 10:46 pm
  2. January 4, 2010 at 9:25 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: