首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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

要将SharePoint多用户字段字符串转换为SPUsers数组,可以使用以下步骤:

  1. 首先,需要确保已经安装并引用了SharePoint Server 2013的Microsoft.SharePoint.Client和Microsoft.SharePoint.Client.Runtime命名空间。
  2. 然后,使用以下代码将多用户字段字符串转换为SPUsers数组:
代码语言:csharp
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;

namespace SharePointMultiUserFieldToStringArray
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://your-sharepoint-site-url";
            string userName = "your-username";
            string password = "your-password";
            string multiUserFieldValue = "your-multi-user-field-value";

            ClientContext clientContext = new ClientContext(siteUrl);
            SecureString securePassword = new SecureString();
            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }
            clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);

            List<int> userIds = GetUserIdsFromMultiUserField(clientContext, multiUserFieldValue);
            List<SPUser> spUsers = GetSPUsersFromUserIds(clientContext, userIds);

            Console.WriteLine("SPUsers:");
            foreach (SPUser spUser in spUsers)
            {
                Console.WriteLine("Title: " + spUser.Title);
                Console.WriteLine("Id: " + spUser.Id);
                Console.WriteLine("LoginName: " + spUser.LoginName);
                Console.WriteLine("Email: " + spUser.Email);
            }
        }

        static List<int> GetUserIdsFromMultiUserField(ClientContext clientContext, string multiUserFieldValue)
        {
            List<int> userIds = new List<int>();
            string[] splitValues = multiUserFieldValue.Split(';');
            foreach (string splitValue in splitValues)
            {
                int userId;
                if (int.TryParse(splitValue, out userId))
                {
                    userIds.Add(userId);
                }
            }
            return userIds;
        }

        static List<SPUser> GetSPUsersFromUserIds(ClientContext clientContext, List<int> userIds)
        {
            List<SPUser> spUsers = new List<SPUser>();
            foreach (int userId in userIds)
            {
                User spUser = clientContext.Web.GetUserById(userId);
                clientContext.Load(spUser);
                clientContext.ExecuteQuery();
                spUsers.Add(spUser);
            }
            return spUsers;
        }
    }
}
  1. 最后,将代码中的siteUrl、userName、password和multiUserFieldValue替换为实际的SharePoint站点URL、用户名、密码和多用户字段字符串。

这样,就可以将多用户字段字符串转换为SPUsers数组并输出每个用户的Title、Id、LoginName和Email。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券