要将SharePoint多用户字段字符串转换为SPUsers数组,可以使用以下步骤:
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;
}
}
}
这样,就可以将多用户字段字符串转换为SPUsers数组并输出每个用户的Title、Id、LoginName和Email。
领取专属 10元无门槛券
手把手带您无忧上云