• 如何通过代码向 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事件里面总是出错,在小林的帮助下解决了问题

  • 相关阅读:
    [XPath] XPath 与 lxml (五)XPath 实例
    [XPath] XPath 与 lxml (四)XPath 运算符
    [XPath] XPath 与 lxml (三)XPath 坐标轴
    [XPath] XPath 与 lxml (二)XPath 语法
    拥抱.NET Core系列:MemoryCache 缓存过期
    拥抱.NET Core系列:MemoryCache 初识
    一个开源的强类型客户端(.NET 中的 Open Fegin)— Rabbit Go
    Configuration Extensions
    拥抱.NET Core系列:Logging (1)
    拥抱.NET Core系列:依赖注入(2)
  • 原文地址:https://www.cnblogs.com/Fengger/p/2535223.html
Copyright © 2020-2023  润新知