0

We currently use the following format to retrieve a list of data from SharePoint Online and it works fine, but how can I do a similar query that returns a different field "as" the Id field?

https://server/_api/web/lists/GetByTitle('Pages')/Items?$select=Id,Title&$filter=<filter criteria 1>

I would like to do the equivalent of this if possible?

https://server/_api/web/lists/GetByTitle('Pages')/Items?$select=SomeOtherId as Id,Title&$filter=<filter criteria 2>

So that the output still says Id but the value is what is contained in the SomeOtherId field. The database equivalent syntax would be

select SomeOtherId as Id,Title ...

Or even how to merge two fields as the output field, such as FirstName + Surname as Name?

Is this possible?

3
  • 1
    This is not possible using REST APIs. Where are you using the results of api call? You can transform the results in success function. Jul 5, 2022 at 16:47
  • @GaneshSanap I currently have to add a [JsonProperty] decorator in the code for each mapping. This works, but I thought SP would have the ability to provide this functionality without me having to loop through all the results.
    – DeclanMcD
    Jul 6, 2022 at 8:45
  • No, SP REST API does not provide the functionality to use as in $select query. check my answer given below. Jul 8, 2022 at 4:53

1 Answer 1

1

This is not possible using SharePoint REST APIs. You cannot use as in $select query in REST APIs.

After you get results from REST API call, you have to transform results array in success function to add new property in results array. You can do it like given below:

Results.forEach(function (item) {
  item.SomeOtherId = item.Id;
});

OR

Results.map(item => ({ ...item, SomeOtherId: item.Id }))

Reference: Add property to an array of objects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.