简体   繁体   English

无法通过电子邮件从Sharepoint获取用户

[英]Unable to get user by email from Sharepoint

I am trying to access the user by his email id. 我正在尝试通过他的电子邮件ID访问该用户。 But I am getting user not found exception, even when I am able to login to office 365 portal using that email id. 但是,即使我能够使用该电子邮件ID登录到Office 365门户,也无法找到用户例外。 Here is the code snippet: 这是代码片段:

//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();

When I execute the last line, I get an exception User not found . 执行最后一行时,出现异常User not found Anyone has any idea about how to get user from email? 有人对如何从电子邮件中获取用户有任何想法吗? I have user's email id only. 我只有用户的电子邮件ID。

I found the solution, its a two step process, though its working I am not sure if this is correct way. 我找到了解决方案,它是一个两步过程,尽管我无法确定这是否正确。 My code looks like: 我的代码如下:

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;
   }

}

I took the help from snippet given Here . 我从Here给出的摘录中得到了帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM