Getting VSTS user ID by display name using REST

I have been exploring the VSTS REST API lately. While there are quite a few services that require the user ID as parameter, it isn’t very obvious how you can obtain that user ID for a given user in the first place.

For my requirement I needed to get the user ID for a given user by their display name. The only way I have found to achieve this (via REST) is to use the Graph service, which is still in preview. The steps are described below.

First, we need to call the List all users service (https://[account].vssps.visualstudio.com/_apis/graph/users). This will return a list of all users. Each user record has the following structure:

{
	"subjectKind": "user",
	"domain": "Windows Live ID",
	"principalName": "[value removed]",
	"mailAddress": "[value removed]",
	"origin": "msa",
	"originId": "[value removed]",
	"displayName": "[value removed]",
	"_links": {
		"self": { "href": "https://[account].vssps.visualstudio.com/_apis/Graph/Users/msa...[value removed]" },
		"memberships": { "href": "https://[account].vssps.visualstudio.com/_apis/Graph/Memberships/msa...[value removed]" },
		"membershipState": { "href": "https://[account].vssps.visualstudio.com/_apis/Graph/MembershipStates/msa...[value removed]" },
		"storageKey": { "href": "https://[account].vssps.visualstudio.com/_apis/Graph/StorageKeys/msa...[value removed]" },
		"avatar": { "href": "https://[account].visualstudio.com/_apis/GraphProfile/MemberAvatars/msa...[value removed]" }
	},
	"url": "https://[account].vssps.visualstudio.com/_apis/Graph/Users/msa...[value removed]",
	"descriptor": "msa...[value removed]"
}

Using the displayName property we can locate the user record that we are after. We then need to invoke the service identified by the _links.storageKey property to retrieve the ID for this user. The URL of this service looks like this (value has been changed):

https://[account].vssps.visualstudio.com/_apis/Graph/StorageKeys/msa.XWI1NTUyXYZtMTzyZi09OTZhXYZmMDktXYkwZjA9NzQxZTK9

The above service returns a structure as follow:

{
	"value": "[GUID]",
	"_links": {
		"self": { "href": "https://[account].vssps.visualstudio.com/_apis/Graph/StorageKeys/msa...[value removed]" },
		"descriptor": { "href": "https://[account].vssps.visualstudio.com/_apis/Graph/Descriptors/[GUID]" 
		}
	}
}

The ID of the user can be found in the value property.

So there you have it…

Unfortunately currently it requires two calls to get the ID of a user given their display name. This might change in the future though as Microsoft’s documentation is indicating that the ability to search for user by UPN or display name is coming soon to the Graph service.

Advertisements

About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in TFS, VSTS. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s