Example #1
0
        /// <summary>
        /// Gets the logged in user details.
        /// </summary>
        /// <param name="context">The Client Context</param>
        /// <returns>User object with name, logon name and email address.</returns>
        public static Users GetLoggedInUserDetails(ClientContext context)
        {
            Users currentUserDetail = new Users();
            try
            {
                if (null != context)
                {
                    context.Load(context.Web.CurrentUser, userInfo => userInfo.Title, userInfo => userInfo.Email, userInfo => userInfo.LoginName);
                    context.ExecuteQuery();
                    currentUserDetail.Name = context.Web.CurrentUser.Title;
                    currentUserDetail.Email = context.Web.CurrentUser.Email;

                    //Check if email is available to get account name, if not use login name
                    if (!String.IsNullOrEmpty(currentUserDetail.Email))
                    {
                        currentUserDetail.LogOnName = currentUserDetail.Email;
                    }
                    else
                    {
                        currentUserDetail.LogOnName = context.Web.CurrentUser.LoginName;
                    }

                    //Retrieve user name from login
                    int splitPositionStart;
                    int splitPositionEnd = currentUserDetail.LogOnName.LastIndexOf(ConstantStrings.SymbolAt, StringComparison.OrdinalIgnoreCase);
                    if (splitPositionEnd == -1)  //The user is an on-premise account
                    {
                        splitPositionStart = currentUserDetail.LogOnName.LastIndexOf(ConstantStrings.BackwardSlash, StringComparison.OrdinalIgnoreCase) + 1;
                        splitPositionEnd = currentUserDetail.LogOnName.Length;
                    }
                    else
                    {
                        splitPositionStart = currentUserDetail.LogOnName.LastIndexOf(ConstantStrings.Pipe, StringComparison.OrdinalIgnoreCase) + 1;
                    }
                    currentUserDetail.LogOnName = currentUserDetail.LogOnName.Substring(splitPositionStart, splitPositionEnd - splitPositionStart);
                }
            }
            catch (Exception exception)
            {
                Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ConstantStrings.LogTableName);
            }
            return currentUserDetail;
        }
 public string GetUsers(RequestObject requestObject, Client client, SearchObject searchObject)
 {
     string returnValue = ConstantStrings.FALSE;
     if (null != requestObject && null != client && null != searchObject && ValidationHelperFunctions.CheckRequestValidatorToken())
     {
         string ProvisionMatterValidation = ValidationHelperFunctions.ProvisionMatterValidation(requestObject, client, null, null, null, int.Parse(ConstantStrings.ProvisionMatterCommonValidation, CultureInfo.InvariantCulture), null);
         if (string.IsNullOrWhiteSpace(ProvisionMatterValidation))
         {
             try
             {
                 searchObject.SearchTerm = (!string.IsNullOrWhiteSpace(searchObject.SearchTerm)) ? searchObject.SearchTerm : string.Empty;
                 using (ClientContext clientContext = ServiceUtility.GetClientContext(requestObject.SPAppToken, new Uri(client.Url), requestObject.RefreshToken))
                 {
                     IList<PeoplePickerUser> foundUsers = SharePointHelper.SearchUsers(clientContext, searchObject.SearchTerm);
                     IList<Users> users = new List<Users>();
                     if (null != foundUsers && 0 != foundUsers.Count)
                     {
                         users = ProvisionHelperFunctions.FilterUsers(users, foundUsers);
                     }
                     else
                     {
                         Users noResult = new Users()
                         {
                             Name = TextConstants.PeoplePickerNoResults,
                             LogOnName = string.Empty,
                             Email = string.Empty,
                             EntityType = string.Empty,
                             ProviderName = string.Empty,
                             EntityData = new EntityData()
                             {
                                 Department = string.Empty,
                                 Email = string.Empty,
                                 Title = string.Empty
                             }
                         };
                         users.Add(noResult);
                     }
                     returnValue = JsonConvert.SerializeObject(users);
                 }
             }
             catch (Exception exception)
             {
                 ///// SharePoint Specific Exception
                 returnValue = Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName);
             }
         }
         else
         {
             returnValue = ProvisionMatterValidation;
         }
     }
     else
     {
         returnValue = string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, string.Empty, TextConstants.MessageNoInputs);
     }
     return returnValue;
 }
 /// <summary>
 /// Iterates through the list of users and maps required properties.
 /// </summary>
 /// <param name="users">List of users</param>
 /// <param name="userSet">Users obtained from people picker service based on the search term</param>
 /// <returns>Returns resultant list of users for matter provision</returns>
 internal static IList<Users> FilterUsers(IList<Users> users, IList<PeoplePickerUser> userSet)
 {
     foreach (PeoplePickerUser item in userSet)
     {
         Users tempUser = new Users();
         tempUser.Name = Convert.ToString(item.DisplayText, CultureInfo.InvariantCulture);
         tempUser.LogOnName = Convert.ToString(item.Key, CultureInfo.InvariantCulture);
         tempUser.Email = string.Equals(item.EntityType, ConstantStrings.PeoplePickerEntityTypeUser, StringComparison.OrdinalIgnoreCase) ? Convert.ToString(item.Description, CultureInfo.InvariantCulture) : Convert.ToString(item.EntityData.Email, CultureInfo.InvariantCulture);
         tempUser.EntityType = Convert.ToString(item.EntityType, CultureInfo.InvariantCulture);
         tempUser.ProviderName = Convert.ToString(item.ProviderName, CultureInfo.InvariantCulture);
         tempUser.EntityData = new EntityData()
         {
             Department = string.IsNullOrWhiteSpace(item.EntityData.Department) ? string.Empty : item.EntityData.Department,
             Email = string.IsNullOrWhiteSpace(item.EntityData.Email) ? tempUser.Email : item.EntityData.Email,
             Title = string.IsNullOrWhiteSpace(item.EntityData.Title) ? string.Empty : item.EntityData.Title
         };
         users.Add(tempUser);
     }
     return users;
 }
 /// <summary>
 /// Iterates through the list of users and maps required properties.
 /// </summary>
 /// <param name="users">List of users</param>
 /// <param name="userSet">Users obtained from people picker service based on the search term</param>
 /// <returns>Returns resultant list of users for matter provision</returns>
 internal static IList<Users> FilterUsers(IList<Users> users, IList<PeoplePickerUser> userSet)
 {
     foreach (PeoplePickerUser item in userSet)
     {
         Users tempUser = new Users();
         tempUser.Name = Convert.ToString(item.DisplayText, CultureInfo.InvariantCulture);
         tempUser.LogOnName = Convert.ToString(item.Key, CultureInfo.InvariantCulture);
         tempUser.Email = string.Equals(item.EntityType, ConstantStrings.PeoplePickerEntityTypeUser, StringComparison.OrdinalIgnoreCase) ? Convert.ToString(item.Description, CultureInfo.InvariantCulture) : Convert.ToString(item.EntityData.Email, CultureInfo.InvariantCulture);
         tempUser.EntityType = Convert.ToString(item.EntityType, CultureInfo.InvariantCulture);
         users.Add(tempUser);
     }
     return users;
 }