.Net操作Sharepoint常用方法(7) 根据邮箱或域账号获取用户信息

private static UserModel GetUserByEmail(string email)
{
    UserModel userModel = null;

    try
    {
      OfficeDevPnP.Core.AuthenticationManager authManager = new OfficeDevPnP.Core.AuthenticationManager();
      using (var clientContext = authManager.GetNetworkCredentialAuthenticatedContext(ConfigurationHelper.GetSPSiteURL(), ConfigurationHelper.GetSPUserAccount(), ConfigurationHelper.GetSPUserPassword(), ConfigurationHelper.GetDomainName()))
      {
      
Guid SPClassStudentListId = new Guid(ConfigurationHelper.GetSPClassStudentListId());
      List list = clientContext.Web.Lists.GetById(SPClassStudentListId);

      User user = clientContext.Web.SiteUsers.GetByEmail(email.ToLower());
      clientContext.Load(clientContext.Web);
      clientContext.Load(clientContext.Web.SiteUsers);
      clientContext.Load(user);
      clientContext.ExecuteQuery();

      //实例对象
      userModel = new UserModel();
      userModel.UserName = user.Title;
      userModel.Email = user.Email;
      userModel.Id = user.Id;

      try { //获取部门及职位信息 PeopleManager peopleManager = new PeopleManager(clientContext); string targetUser = "[domain name]\\" + email.ToLower().Replace("@xxx.com", ""); string[] profilePropertyNames = new string[] { "Department", "Title" }; UserProfilePropertiesForUser profilePropertiesForUser = new UserProfilePropertiesForUser(clientContext, targetUser, profilePropertyNames); IEnumerable<string> profilePropertyValues = peopleManager.GetUserProfilePropertiesFor(profilePropertiesForUser); // Load the request and run it on the server. clientContext.Load(profilePropertiesForUser); clientContext.ExecuteQuery(); // Iterate through the property values. string[] userPropertyArr = profilePropertyValues.ToArray(); userModel.DepartmentName = userPropertyArr[0].ToString(); userModel.Title = userPropertyArr[1].ToString(); } catch(Exception ex) { }
    }
}
  
catch (Exception ex)
  {
    userModel = null;
  }
  return userModel;
}

 

posted @ 2020-12-28 16:32  无敌师爷IT技术Blog  阅读(321)  评论(0)    收藏  举报