Get, Expand Properties in Metadata from SharePoint REST API
Posted On April 17, 2019
You might have encountered troubles in getting data out of a SharePoint person field via SharePoint REST API. This usually happens when you want to access more of a user’s properties like the title, name, email, and other data as seen on the table below. Well, this article aims to help you on how to successfully fetch properties from SharePoint person field.
Getting the list data in SharePoint via REST API with only selecting the person field will only give you the user or the person’s user ID which isn’t too helpful. The good thing is that SharePoint REST API has the option to expand lookup metadata properties. The secret is to use the OData $expand query option to get all the available properties inside a person type metadata.
Let’s try to apply its concept in a classic SharePoint Online page.
Create a page and name the page anything you want to name it. Make sure you’re creating a web part page, so we can add a web part inside the page. Here’s how it should look like.
Edit the page and insert 2 Script Editor web part.
We will need the help of jQuery so that we can get the response in SharePoint API. Paste the jQuery CDN script reference inside the script editor and click Insert. Name the script editor as CDN References and set it as hidden inside the web part options. This is helpful if we want to know what content the web part has even if we don’t edit it.
We will also need to insert some html table snippets inside the second web part. Edit the web part and label it (for example, Main Content). Insert the html code snippet inside the Main Content web part. I’ve added some CSS to our table just to make it a little bit fancy.
If you correctly followed the steps above, you’ll see the photo below in the page.
Now, we need to have a source of our data to query. Create a list and call it, for example, Participants. Add a person type metadata. Name it Participant and fill at least 5 person entries in the list.
The Rest Endpoint for getting a list item is /_api/web/Lists/GetByTitle(‘Participants’)/Items?$select=*.
Try to run the code below in the page console.
Notice that the participants object doesn’t have any person name or other property inside other than its ID (ParticipantId).
We need to get the other properties of the person metadata. In that case, we will need to use the $expand query option so that we can expand its object. Try to run the code below in the page console.
We can now access the other available properties inside a person type metadata.
To complete our table inside our web part, we need to append the persons, display name, and its property. Insert the code below in the Main Content web part.
When you reload your page after you have inserted the code, you’ll notice your table being populated by your script inside the Main Content web part. Somewhere in your page should look like this (the number of items depends on how many entries you have in your Participants list).
The link of the whole script inside the Main Content web part can be viewed here. You can also copy this link https://www.dataoutsource.com/blog/wp-content/uploads/2019/01/ExpandPersonMetadata.txt and paste it in your browser.
There you go. We hope you find this article helpful. Don’t forget to share this with your friends or anyone you know who might need this.