我正在尝试通过他的电子邮件ID访问用户。但即使我能够使用该电子邮件ID登录Office 365门户网站,我也会收到用户未找到的异常。以下是代码段:
//Creating Context
Uri webFullUrl = new Uri(URL);
string username = UserName;
string password = Password;
SecureString secureString = new SecureString();
for (int i = 0; i < password.Length; i++)
{
char c = password[i];
secureString.AppendChar(c);
}
SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(username, secureString);
ClientContext clientContext = new ClientContext(webFullUrl)
{
Credentials = credentials
};
User user = clientContext.Web.SiteUsers.GetByEmail(email);
clientContext.ExecuteQuery();
当我执行最后一行时,我得到一个异常User not found
。任何人都知道如何从电子邮件中获取用户?我只有用户的电子邮件ID。
答案 0 :(得分:0)
我找到了解决方案,它是一个两步过程,虽然它的工作我不确定这是否正确。我的代码如下:
public User GetUserByEmail(ClientContext clientContext, string email)
{
try
{
ClientResult<Microsoft.SharePoint.Client.Utilities.PrincipalInfo> persons =
Microsoft.SharePoint.Client.Utilities.Utility.ResolvePrincipal(clientContext, clientContext.Web, email,
Microsoft.SharePoint.Client.Utilities.PrincipalType.User,Microsoft.SharePoint.Client.Utilities.PrincipalSource.All, null, true);
clientContext.ExecuteQuery();
Microsoft.SharePoint.Client.Utilities.PrincipalInfo person = persons.Value;
User user = clientContext.Web.SiteUsers.GetByLoginName(person.LoginName);
clientContext.ExecuteQuery();
return user;
}
catch
{
return null;
}
}
我从Here获取了代码段的帮助。