以编程方式列出项目多个用户的权限(共享点) [英] List Item multiple users permissions programmatically (sharepoint)

查看:74
本文介绍了以编程方式列出项目多个用户的权限(共享点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've looked around and haven't found a solution yet. I have picked up code snippets here and there to find a solution.

我有一个名为文档协作"的文档库,其字段为分配给".这是一个人员/组"字段.这些人将能够处理特定的文档(列表项权限).现在,起初,他们将具有隐藏的权限(他们看不到),但是将其添加到文档中后,他们将看到它并能够对其进行贡献,并且还将收到电子邮件通知.我已经附上了完整的代码.

I have a Doc Library called "Document Collaboration" with the field "Assigned To". This is a People/Groups field. These people will be able to work on a specific document(list item permission). Now, at first, they will have hidden permissions(they cant see it), but when added to the doc, they will see it and be able to contribute it, also they will get an email notification. I have attached the full code below.

因此,当我进行VS10调试时,没有任何错误.但是它不会发送任何电子邮件或未设置权限.怎么了?

So, I don't get any errors, when I go through VS10 debug. But it doesn't send any email or doesn't set the permissions. What's wrong?

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

推荐答案

不,我犯了一个简单的错误,将其置于签入状态而不是更新状态,该函数在更新时多次运行也存在变通办法,只需在列表中添加一个名为"updateContributors"的列,并使用默认值True/Yes

nope, I made the simple mistake of putting it under checked in instead of updated, there's also a work around for the function being run multiple times when it updates, just make a column called "updateContributors" in your list and default value True/Yes

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

Here's the code/no time to explain, but pretty commented, good luck:

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



        }

    }

这篇关于以编程方式列出项目多个用户的权限(共享点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆