开发者

item wise permissions in sharepoint document library

开发者 https://www.devze.com 2023-04-12 09:15 出处:网络
I have created an email enabled document library in sharepoint 2007. Our client is using crystal reports and stores reports in this doc. Library. I have around 900 documents in that library now, and c

I have created an email enabled document library in sharepoint 2007. Our client is using crystal reports and stores reports in this doc. Library. I have around 900 documents in that library now, and client is asking me to provide item wise permissions. They are naming the files using employee ID. And they want these files to be visible to proper employee and his/her secr开发者_JAVA技巧etary.

Can anyone help me how to achieve this programatically? IS there any proper way of doing it?

Thanks,


You can create an event handler that will break file permission inheritance and change the permissions you require.

Another option is to move the file to a folder (or even separate document library) that is pre-configured with the permissions you want.

The first option is simpler to implement, but the security management can become... messy.

Here is some code to get you started on breaking and setting document permissions.

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; 
    }


Without writing code, you can create a folder for each employee and set folder permissions to be accessible by only that employee and managers.

For managers you can still make views which would display all list items without folders.

0

精彩评论

暂无评论...
验证码 换一张
取 消