如何最好地将 SharePoint 多用户字段字符串转换为 SPUsers 数组?

发布于 2024-07-11 19:27:33 字数 522 浏览 9 评论 0原文

我正在为 SharePoint 列表编写 ItemAdding 处理程序,并且该列表包含多用户字段。 由于 SPItem 此时实际上不可用,因此我认为我只能使用从 SPItemEventDataCollection 返回的字符串。 当 user1、user2 和 user3 存在时,该字符串看起来像这样:

1;#MYDOMAIN\user1;#4;#MYDOMAIN\user2;#10;#MYDOMAIN\user3

我想将其转换为一个数组SPUser 对象,以便将其传递到另一个现有方法中。 是否有任何 SharePoint 内置方法来处理这些字符串,或者我是否只能解析该字符串?

另外,假设我需要处理这个字符串,看起来这里的整数标记总是对应于后面的域\用户名。 是否存在这种情况不成立且整数或域\用户名丢失或不正确的情况? 仅使用数字并使用 SPWeb 的 SiteUsers.GetByID(id) 方法安全吗? 在一些测试中,我无法让它失败,但如果数字和字符串数据完全冗余,那么它们都会被包含在内,这似乎很奇怪。

谢谢!

I'm writing an ItemAdding handler for a SharePoint list, and the list contains multi-user fields. Since the SPItem is not actually available at this point, I think I'm relegated to using the string that comes back from the SPItemEventDataCollection. That string will look something like this when user1, user2, and user3 are present:

1;#MYDOMAIN\user1;#4;#MYDOMAIN\user2;#10;#MYDOMAIN\user3

I'd like to convert this into an array of SPUser objects in order to pass it into another existing method. Is there any SharePoint built-in way to handle these strings, or am I relegated to parsing this string?

Also, assuming I need to deal with this string, it looks like the integer tokens here always correspond to the domain\username that follows. Are there any cases where this will not be true and either the integer or the domain\username are missing or otherwise incorrect? Would it be safe to just use the numbers and use SPWeb's SiteUsers.GetByID(id) method? In a handful of tests I can't get that to fail, but it seems weird that both the numeric and string data would be included if they're totally redundant.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念三年u 2024-07-18 19:27:33

SPFieldUserValueCollection 正是您所寻找的。 它的构造函数接受 SPWeb 和用户字符串。 集合中的 SPFieldUserValue 项提供了一个 User 属性,该属性返回 Web 的相应 SPUser 对象。

private void ProcessUsers(SPListItem item, string fieldName)
{
  string fieldValue = item[fieldName] as string;
  SPFieldUserValueCollection users = new SPFieldUserValueCollection(item.Web, fieldValue);

  foreach(SPFieldUserValue uv in users)
  {
    SPUser user = uv.User;
    // Process user
  }
}

SPFieldUser 类型从标准 SPFieldLookup 继承其行为,该类型还存储 ID 和 Title 并具有相应的 SPFieldLookupValueCollection。 ID 支持(弱)引用完整性,而缓存的值允许快速查询。

SPFieldUserValueCollection does exactly what you're looking for. Its constructor accepts an SPWeb and string of users. The SPFieldUserValue items in the collection provide a User property that returns the corresponding SPUser objects for the web.

private void ProcessUsers(SPListItem item, string fieldName)
{
  string fieldValue = item[fieldName] as string;
  SPFieldUserValueCollection users = new SPFieldUserValueCollection(item.Web, fieldValue);

  foreach(SPFieldUserValue uv in users)
  {
    SPUser user = uv.User;
    // Process user
  }
}

The SPFieldUser type inherits its behavior from the standard SPFieldLookup, which also stores both ID and Title and has a corresponding SPFieldLookupValueCollection. The ID supports (weak) referential integrity while the cached Value allows for quick queries.

吹梦到西洲 2024-07-18 19:27:33

这适用于我从用户字段获取 SPSuser 它应该与从事件数据中提取它相同。:

private SPUser getGroup(SPListItem item, string fieldName)
        {
            string fieldValue = item[fieldName] as string;
            if (string.IsNullOrEmpty(fieldValue)) return null;
            int id = int.Parse(fieldValue.Split(';')[0]);
            SPUser user = item.Web.AllUsers.GetByID(id);
            return user;
        }

整数指的是本地 SPWebs 用户数据库中的用户 id。 但是,这是不同步的,因此自保存列表项以来该用户可能已从用户数据库中删除。

This works for me getting a SPSuser from a user field It should be the same thing for extracting it from event data.:

private SPUser getGroup(SPListItem item, string fieldName)
        {
            string fieldValue = item[fieldName] as string;
            if (string.IsNullOrEmpty(fieldValue)) return null;
            int id = int.Parse(fieldValue.Split(';')[0]);
            SPUser user = item.Web.AllUsers.GetByID(id);
            return user;
        }

The integer refers to the id of the user in the local SPWebs's user database. This is, however not synced, so the user may have been deleted from the user database since the list item was saved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文