Sharepoint 2013-仅允许文档创建者编辑文档的权限

时间:2018-06-27 14:53:13

标签: sharepoint-2013

是否有一种方法可以控制SharePoint的权限,以便将文档上载到文档库的用户是唯一可以编辑或删除该文件的用户?我希望其他用户能够打开该文件,但将其作为只读文件而不是检出文件(我不在乎他们是否将其保存到计算机并更改本地副本)。

我想这是一个允许所有人添加和删除文件的权限组的摘要,但是只有添加的文件的创建者才能签出并进行编辑。

谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以为此库创建事件接收器,当文件上传后,中断权限继承并为作者和其他人(可能是一个组)分配唯一权限。

这是technet的示例代码。

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Collections.Specialized;

namespace SurveyPermissionsMgr
{
    class ThreeMonthSurveyPermissionsMgr : SPItemEventReceiver
    {
        public override void ItemAdded(SPItemEventProperties properties)
        {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(properties.WebUrl))
                    {
                        site.AllowUnsafeUpdates = true;

                        using (SPWeb web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;

                            SPListItem myItem = web.Lists[properties.ListId].GetItemById(properties.ListItem.ID); //  GET LIST OBJECT FROM SPWEB, NOT EVENT PROPERTIES

                            myItem.BreakRoleInheritance(false);

                            this.DisableEventFiring();
                            myItem.Update();

                            SPMember member = web.SiteGroups["My Group"];
                            SPPrincipal principal = (SPPrincipal)member;
                            SPRoleDefinition roledefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                            SPRoleAssignment myRoleAssignment = new SPRoleAssignment(principal);
                            myRoleAssignment.RoleDefinitionBindings.Add(roledefinition);
                            myItem.RoleAssignments.Add(myRoleAssignment);

                            string managerstring = myItem["Manager Account Id"].ToString();
                            SPUser manager = web.EnsureUser(managerstring);

                            myItem["Manager"] = manager;

                            SPPrincipal managerprincipal = (SPPrincipal)manager;
                            SPRoleDefinition managerroledefinition = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                            SPRoleAssignment ManagerRoleAssignment = new SPRoleAssignment(managerprincipal);
                            ManagerRoleAssignment.RoleDefinitionBindings.Add(managerroledefinition);
                            myItem.RoleAssignments.Add(ManagerRoleAssignment);

                            string associatestring = myItem["Author"].ToString();
                            int userid = Convert.ToInt32(associatestring.Substring(0, associatestring.IndexOf(";#")));
                            SPUser associate = web.AllUsers.GetByID(userid);
                            SPPrincipal associateprincipal = (SPPrincipal)associate;
                            SPRoleDefinition associateroledefinition = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                            SPRoleAssignment AssociateRoleAssignment = new SPRoleAssignment(associateprincipal);
                            AssociateRoleAssignment.RoleDefinitionBindings.Add(associateroledefinition);
                            myItem.RoleAssignments.Add(AssociateRoleAssignment);

                            myItem.Update();
                            this.EnableEventFiring();
                            web.AllowUnsafeUpdates = false;
                            site.AllowUnsafeUpdates = false;

                            //Handle messaging
                            StringDictionary dict = new StringDictionary();
                            dict.Add("to", manager.Email);
                            dict.Add("from", "us@you.com");
                            dict.Add("subject", "bla bla bla");
                            string msgbody = "bla bla bla";
                            SPUtility.SendEmail(web, dict, msgbody);
                        }
                    }
                });
            }
        }
    }
}