以编程方式列出项目多个用户权限(sharepoint)

时间:2011-06-20 15:09:38

标签: c# sharepoint list permissions

我环顾四周,还没有找到解决方案。我已经在这里和那里找到了代码片段以找到解决方案。

我有一个名为“文档协作”的文档库,其中包含“已分配给”字段。这是一个人/组字段。这些人将能够处理特定文档(列表项权限)。现在,首先,他们将拥有隐藏的权限(他们无法看到它),但是当添加到文档时,他们将看到它并能够贡献它,他们也会收到电子邮件通知。我已附上以下完整代码。

所以,当我通过VS10调试时,我没有得到任何错误。但它不发送任何电子邮件或不设置权限。怎么了?

    using System;
    using System.IO;
    using System.Security.Permissions;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Security;
    using Microsoft.SharePoint.Utilities;
    using Microsoft.SharePoint.Workflow;

    namespace ARDT.Notifications
    {
        /// <summary>
        /// List Item Events
        /// </summary>
        public class Notifications : SPItemEventReceiver
        {
           /// <summary>
           /// An item was checked in
           /// </summary>
           public override void ItemCheckedIn(SPItemEventProperties properties)
           {
               SPSite site = new SPSite("http://sp2010dev/ardt");
               using (SPWeb web = site.OpenWeb())
               {
                   SPList list = web.Lists["Document Collaboration"];
                   SPListItem listItem = properties.ListItem;
                   SPUser userName = null;
                   String toAddress = null;

                   //EMail initializations
                   bool appendHtmlTag = false;
                   bool htmlEncode = false;
                   string subject = "Subject";
                   string message = "Message text";

                   //get usernames
                   string[] userNameArray = listItem.Fields["Assigned to"].ToString().Split(';');

                   for (int i = 0; i <= userNameArray.Length - 1; i++)
                   {
                       userName = web.AllUsers[userNameArray[i]];
                       toAddress = userName.Email;
                       SPSecurity.RunWithElevatedPrivileges(delegate()
                       {
                           //EMAIL USER
                           bool result = SPUtility.SendEmail(web, appendHtmlTag, htmlEncode, toAddress, subject, message);

                           //PERMISSIONS
                           //remove permissions first
                           web.AllowUnsafeUpdates = true;
                           listItem.BreakRoleInheritance(false);
                           SPRoleAssignmentCollection raCollection = listItem.RoleAssignments;
                           //remove exisiting permissions one by one
                           for (int a = raCollection.Count - 1; i > -0; i--)
                           {
                               raCollection.Remove(a);
                           }

                           //grant permissions for specific list item
                           SPRoleDefinition roleDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                           SPRoleAssignment roleAssignment = new SPRoleAssignment(userName);

                           roleAssignment.RoleDefinitionBindings.Add(roleDefintion);
                           listItem.RoleAssignments.Add(roleAssignment);
                           listItem.Update();
                       });
                   }
               }
               base.ItemCheckedIn(properties);
           }

        }
    }

2 个答案:

答案 0 :(得分:5)

nope,我犯了一个简单的错误,即将其置于已检入而不是更新,还有一个解决方案,当它更新时多次运行该函数,只需在列表中创建一个名为“updateContributors”的列并默认值真/是

这是代码/没时间解释,但很有评论,祝你好运:

    using System;
    using System.IO;
    using System.Security.Permissions;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Security;
    using Microsoft.SharePoint.Utilities;
    using Microsoft.SharePoint.Workflow;

    //Debugging includes
    using System.Diagnostics;


    namespace ARDT.Notifications
    {
        /// <summary>
        /// List Item Events
        /// </summary>
        public class Notifications : SPItemEventReceiver
        {
            /// <summary>
            /// An item was updated
            /// </summary>
            public override void ItemUpdated(SPItemEventProperties properties)
            {
                if (properties.ListItem["updateContributors"].ToString().Equals("True"))
                {
                    //work around so it goes through it only once instead of everytime the item is updated
                    properties.ListItem["updateContributors"] = "False";
                    SPSite site = new SPSite("http://sp2010dev/ardt/");
                    using (SPWeb web = site.OpenWeb())
                    {

                        SPList list = web.Lists["Document Collaboration"];
                        SPListItem listItem = properties.ListItem;
                        SPUser userName = null;
                        String toAddress = null;

                        //EMail initializations
                        bool appendHtmlTag = false;
                        bool htmlEncode = false;
                        string subject = "You have been assigned to a Document";
                        string message = "Test Message";

                        //get usernames
                        string tempFieldValue = listItem["Assigned To"].ToString();
                        string[] userNameArray = listItem["Assigned To"].ToString().Split(';');

                        //remove permissions first
                        web.AllowUnsafeUpdates = true;
                        listItem.BreakRoleInheritance(false);
                        SPRoleAssignmentCollection raCollection = listItem.RoleAssignments;
                        //remove exisiting permissions one by one
                        for (int a = raCollection.Count - 1; a >= 0; a--)
                        {
                            raCollection.Remove(a);
                        }

                        for (int i = 1; i < userNameArray.Length; i++)
                        {
                            tempFieldValue = userNameArray[i].Replace("#", "");
                            userName = web.AllUsers[tempFieldValue];
                            toAddress = userName.Email;
                            SPSecurity.RunWithElevatedPrivileges(delegate()
                            {
                                //EMAIL USER
                                bool result = SPUtility.SendEmail(web, appendHtmlTag, htmlEncode, toAddress, subject, message);

                                //PERMISSIONS                              
                                //grant permissions for specific list item
                                SPRoleDefinition roleDefintion = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                SPRoleAssignment roleAssignment = new SPRoleAssignment(userName);

                                roleAssignment.RoleDefinitionBindings.Add(roleDefintion);
                                listItem.RoleAssignments.Add(roleAssignment);
                                listItem.Update();
                            });
                            i++;
                        }

                    }
                    //base.ItemUpdated(properties);
                    //after final update has been done return true
                    properties.ListItem["updateContributors"] = "True";
                }
            }



        }

    }

答案 1 :(得分:1)

您的代码中存在的一个问题 - 您在当前执行上下文中创建SPWeb对象,然后尝试使用SPSecurity.RunWithElevatedPrivileges发送邮件。您永远不应该使用一个帐户打开网络,然后在另一个帐户中使用它。您必须在RunWithElevatedPrivilegesas it can be seen in this example中重新创建上下文。