How to Search "Name" Keys in Sharepoint API Response and Return Corresponding "ID" Key's Value?

This topic was automatically generated from Slack. You can find the original thread here.

I ran an API call to Sharepoint to get a list of Drives on my Sharepoint Site. From that response, I received an arrary of values where there is a “id” key and a “name” key. How can I search the “name” keys to check for a value and then return the sibling “id” key’s value?

To search through an array of objects returned from your SharePoint API call and find a specific “name” value, then return the corresponding “id” value, you can use the JavaScript find method. This method allows you to specify a condition to search for and returns the first element in the array that satisfies this condition. Here’s how you can do it within a Pipedream component: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { sharepoint: { type: "app", app: "sharepoint", }, }, async run({ steps, $ }) { // Example API call to get the list of Drives const response = await axios($, { url: `https://graph.microsoft.com/v1.0/sites/{site-id}/drives`, headers: { Authorization: `Bearer ${this.sharepoint.$auth.oauth_access_token}`, }, }); // Assuming response.data.value contains the array of Drives const drives = response.data.value; // Replace 'yourTargetName' with the name you're searching for const targetName = 'yourTargetName'; const targetDrive = drives.find(drive => drive.name === targetName); if (targetDrive) { // If a drive with the target name is found, return its id return targetDrive.id; } else { // If no drive with the target name is found, return a message or handle as needed return 'Drive not found'; } }, }); Make sure to replace {site-id} with your actual SharePoint site ID and 'yourTargetName' with the name of the drive you’re searching for. This code snippet demonstrates how to make an API call to SharePoint to retrieve a list of drives, search for a drive by name, and return the corresponding ID.

I ran that code and replace the Site ID and the target. I got the following error.

Cannot read properties of undefined (reading ‘value’)