How to get the details of user by passing department as a value in a sharepoint site

Hi all,

I am facing issue by getting the value by department wise,generally we will use id. need immediate response waiting for reply 

April 28th, 2015 3:08am

Hi Lakshmi,

From your description, my understanding is that you want to get user detail via department.

To accomplish your requirement, you could get all users  in current site, and then check if current user is a member of a department via department name.

You could refer to code below get all users in current site:

            // replace your site url and department name
            string serverUrl = "http://sp/sites/sp2013";
            string department = "test";

            using (SPSite site = new SPSite(serverUrl))
            {

               using (SPWeb web = site.OpenWeb())
                {

                    foreach (SPUser user in web.Users)
                    {
                        //replace your domain name
                        string targetUser = "domain\\" + user.Name.ToString();

                        getUserDetailByDepartment(serverUrl, targetUser, department);
                    }

                }
            }

Then you could accomplish your requirement via get user profile properties with CSOM, and check if the user is in your department, please refer to this code below:

        /// <summary>

        /// get user detaile via department name

        /// </summary>

        /// <param name="serverUrl">target sharepoint site</param>

        /// <param name="targetUser">target user</param>

        /// <param name="department">department name</param>

        private static void getUserDetailByDepartment(string serverUrl, string targetUser, string department)

        {

            // Connect to the client context.

            ClientContext clientContext = new ClientContext(serverUrl);


            // Get the PeopleManager object and then get the target user's properties.

            PeopleManager peopleManager = new PeopleManager(clientContext);

            PersonProperties personProperties = peopleManager.GetPropertiesFor(targetUser);


            clientContext.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties);

            clientContext.ExecuteQuery();


            //check if current user is the member of the department

            if (personProperties.UserProfileProperties["Department"] == "test")

            {

                foreach (var property in personProperties.UserProfileProperties)

                {

                    Console.WriteLine(string.Format("{0}: {1}", property.Key.ToString(), property.Value.ToString()));

                }

            }

        }

You could refer to this article about retrieving user profile properties with CSOM:

How to: Retrieve user profile properties by using the .NET client object model in SharePoint 2013

https://msdn.microsoft.com/en-us/library/office/jj163182.aspx

Best Regards,

Vincent Han

Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 3:43am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics