Retrieve all SharePoint users from web service

In my last post, I discussed using static global variables so the variable maintains it's value even when a browser is closed. I will be using that here, referring to that global variable as a separate class (all code is provided there).

Don't forget that you will need to add a web service since the only way I know how to get all users is through this web service.

If you're using Visual Studio 2005, right click on References and click add Web Reference

If you're using Visual Studio 2008, right click on References, and click add Service Reference, then click Advanced, then click Add Web Reference.

The web service url is http://your_intranet_name/_vti_bin/UserProfileService.asmx

I usually set the Web reference name as "UserProfileService", though I'm not 100% sure if I should be doing that since it could get confusing.

First thing, set your variables:

 #region Variables
UserProfileService.UserProfileService UserProfileServiceWS = new UserProfileService.UserProfileService();
private const string UserProfileServiceURL = "/_vti_bin/UserProfileService.asmx";
#endregion

Now I check the static global variable to see if it has been set or if it has expired.

             if (GlobalVariable.ExpirationTime != null)
            {
                if (DateTime.Now.CompareTo(GlobalVariable.ExpirationTime) != -1) // if current time is after expiration time
                {
                    GlobalVariable.AllUsers = null; // reset user list so it is then re-populated
                }
            } 
That code above will set the AllUsers variable to null because the variable has expired. Then the function below (which includes the code above) will check if AllUsers is null (which is true if the variable is not populated) and then populate the variable accordingly.

         public string RetrieveUsers()
        {
            string userList = "";
            if (GlobalVariable.ExpirationTime != null)
            {
                if (DateTime.Now.CompareTo(GlobalVariable.ExpirationTime) != -1) // if current time is after expiration time
                {
                    GlobalVariable.AllUsers = null; // reset user list so it is then re-populated
                }
            }
            if (GlobalVariable.AllUsers == null)
            {
                // set the expiration time to be the next day
                GlobalVariable.ExpirationTime = DateTime.Today.AddDays(1);
                try
                {
                    //SPSite site = new SPSite(SiteURL);
                    SPWeb web = SPContext.Current.Web;
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite elevatedSite = new SPSite(web.Site.ID))
                        {
                            ServerContext context = ServerContext.GetContext(elevatedSite);
                            UserProfileManager profileManager = new UserProfileManager(context);
                            PeopleList pl = new PeopleList();
                            DateTime Today = DateTime.Now.Date;
                            UserProfileServiceWS.Credentials = CredentialCache.DefaultCredentials;
                            UserProfileServiceWS.Url = web.Url + UserProfileServiceURL;
                            foreach (UserProfile profile in profileManager) // cycle through all profiles
                            {
                                 userList += "|" + profile["AccountName"].ToString();
                            }
                            GlobalVariable.AllUsers = userList;
                        }
                    });
                }
                catch (Exception ex)
                {
                    throw ex; // or handle the exception any way you like.
                }
            }
            else
            {
                userList = GlobalVariable.AllUsers.ToString();
            }
            return userList;
        }
 

So you see, the code is fairly simple... if the global variable named AllUsers is null, then go and retrieve all users and place it in a single comma delimited string. If it is not null, then just return the userList since it already contains all the users in it.

Once you have the userList, you can turn this string into an array and handle it just like any other array.

string[] result = userList.Split(new Char[] { ',' });
foreach (string user in result)
{
    // you have the username, now you can get any of their properties (described here)
}

 

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.