8

I am using VSTS Pullrequest create API method to automate the PR creation, in the request i need to provide various IdentityRef id values for createdby/autocompleteby/reviewers properties. In my case all i have is user details (like full name, email address - user1@domain.com ), in this case how do i retrieve the IdentityRef Guid so that i can pass it to PR Create API.

https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/create?view=azure-devops-rest-5.0

Any help is appreciated.

Mahender
  • 5,554
  • 7
  • 38
  • 54
  • Use the graph API: https://learn.microsoft.com/en-us/rest/api/azure/devops/graph/users?view=azure-devops-rest-5.0 – Daniel Mann Dec 30 '18 at 00:53
  • Thanks Daniel for the reply. I tried above "Users - Get" graph API but it needs the userDescriptor which i think it the GUID or some sort which isn't clear in the docs. I tried the user List graph api to list all users in the org but it only lists 500 users. – Mahender Dec 30 '18 at 05:45
  • Hi @Mahende, did you success to override the `createdBy`? – Shayki Abramczyk Jul 23 '20 at 06:44

2 Answers2

4

1) Use Graph - List Users, but also check for a X-MS-ContinuationToken response header to determine if there is still additional paged data to be retrieved. If so, resend the request with the continuation token value until all values are returned:

  • GET https://vssps.dev.azure.com/{organization}/_apis/graph/users?api-version=5.0-preview.1, followed by
  • GET https://vssps.dev.azure.com/{organization}/_apis/graph/users?continuationToken={continuationToken}&api-version=5.0-preview.1

(I'm wondering if this is why you are only getting 500 users per your comment above. Unfortunately the documentation doesn't list the max page size for this API. If you are using the continuation token and all users are not being returned, that sounds like an API bug to me.)

2) Use Get User Entitlements. This provides top and skip parameters. The top parameter has a 10000 record limit per the documentation.

  • GET https://vsaex.dev.azure.com/{organization}/_apis/userentitlements?top=10000&api-version=5.0-preview.2
Eric Munn
  • 580
  • 2
  • 8
  • Thank you Eric for the reply, I tried the Graph-List users with continuation token indeed it is solving my problem. I haven't used the continuation token parameter in my earlier runs. – Mahender Jan 09 '19 at 21:50
2

An easier approach is to use Identities - Read Identities API, which allows us to query for the IdentityRef of a user by email, by IdentityDescriptors, by ids, by name and by SubjectDescriptors. This avoids the costly approach of having to fetch the user graph repeatedly to locate a single user and also helps in directly getting the IdentityRef and not just the user descriptor.

  • love it, unfortunately this is apparently very new api and not available on even recent on-premise servers. Will remember it for when we join the cloud version ;) (then again I just saw the other solutions have the same problems... not sure how this is supposed to be done on TFS 2018/2019) – SvenS Mar 05 '21 at 09:08
  • 1
    Correct me if I am wrong, but doesn't this API return "Identity" not "IdentityRef"? Is there a way to convert the Identity type to an IdentityRef? You can't pass the Identity object to the IdentityRefWithVote constructor. – Jay Feb 13 '23 at 16:55