SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

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;

    }

Read the complete post at http://blogs.msdn.com/pranab/archive/2007/07/04/how-to-set-item-level-permission-for-moss-wss-list-document-library-programmatically.aspx


Posted 07-03-2007 6:31 PM by Pranab Paul's Blog - SharePoint 2007 (MOSS/WSS 3.0) Development Tips
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.