zoukankan      html  css  js  c++  java
  • 如何通过代码向 User 类型的字段里面添加 多个用户

    需求:要通过一个User字段,记录修改列表的人员信息,通过事件处理器

    程序实现:

    在添加事件里面添加代码:记录创建人的信息

             /// <summary>
            /// 已添加项.
            /// </summary>
            public override void ItemAdded(SPItemEventProperties properties)
            {
                SPListItem item = properties.ListItem;
                SPFieldUserValueCollection fuvc = new SPFieldUserValueCollection();
                SPUser user = properties.Web.EnsureUser(properties.UserLoginName);
                fuvc.Add(new SPFieldUserValue(properties.Web, user.ID, user.Name));
                item["UserType"] = fuvc;
                item.Update();
            }

    在修改事件里添加代码:记录修改人的信息,这里要添加一个判断,如果字段里面已经存在该用户,则不用再次添加用户信息

             /// <summary>
            /// 已更新项
            /// </summary>
            public override void ItemUpdated(SPItemEventProperties properties)
            {
                bool isTure = true;
                SPListItem listItem = properties.ListItem;
                SPUser user = properties.Web.EnsureUser(properties.UserLoginName);
                SPFieldUserValueCollection fieldUserValueCollection = listItem["UserType"] as SPFieldUserValueCollection;
                foreach (SPFieldUserValue userValue in fieldUserValueCollection)
                {
                    if (userValue != null)
                    {
                        if (userValue.LookupId == user.ID) //判断用户是否存在
                        {
                            isTure = false;
                            break;
                        }
                    }
                }

                if (isTure)
                {
                    fieldUserValueCollection.Add(new SPFieldUserValue(properties.Web, user.ID, user.LoginName));
                    listItem["UserType"] = fieldUserValueCollection;
                    listItem.Update();
                }
            }

    备注:第一次写程序的时候,把代码放到Adding事件里面总是出错,在小林的帮助下解决了问题

  • 相关阅读:
    微信开发SDK使用教程--手机微信群聊新增通知服务端
    微信开发SDK使用教程--手机微信联系人信息上传服务端
    微信开发SDK使用教程--手机端接收发送朋友圈任务指令后数据回传服务端
    微信二次开发SDK使用教程--手机端向服务端发送回复朋友圈评论任务反馈服务端
    js 自执行匿名函数(转载)
    js继承中,原型属性的继承探究
    搭建邮件服务器和使用压测工具的总结
    window.onload和window.document.readystate的探究
    读艾伦的jQuery的无new构建,疑惑分析——jquery源码学习一
    WPF页面 全球化和本地化
  • 原文地址:https://www.cnblogs.com/Fengger/p/2535223.html
Copyright © 2011-2022 走看看