How to update a SharePoint online document library item's custom fields using graph API patch calls?

Doug Horton 0 Reputation points
2024-09-23T18:34:54.95+00:00

I have a PowerShell script that is pulling user photos from Active Directory and then posting them to a SharePoint Document Library. This script is working successfully to this point.

Next, I need to update the photo item's custom columns with Active Directory properties including email address, phone, title, etc. This is where I'm stuck.

I've seen various examples of PATCH posts, but I keep getting 400 bad request or 404 not found errors. Here is the URL I'm currently trying: https://graph.microsoft.com/v1.0/drives/$DriveId/items/$ItemId

I'm obtaining the $ItemId from the result of the API call that successfully uploads the photo. E.g. $Result = invoke-restmethod... then $ItemId = $Result.Id
I'm using the following format for body (after conversion to JSON -- line breaks added for readability):
{
"Position":"XXX",
"Phone":"XXX-XXX-XXXX",
"City":"XXXXXXXX",
"Email":"XXX@XXXX.com",
"State":"XX",
"Zip":"XXXXX",
"Address":"XXXXXXXXXXX"
}
I've also tried the following versions I saw in other posts with the same body:
graph.microsoft.com/v1.0/drives/$DriveId/items/$ItemId/listitem/fields
graph.microsoft.com/v1.0/drives/$DriveId/items/$ItemId/fields
graph.microsoft.com/v1.0/sites/$SiteId/lists/$ListId/items/$ItemId/fields
graph.microsoft.com/v1.0/sites/root/drives/$DriveId/items/$ItemId/listitem/fields

Where $ListId = $Result.parentreference.id

Any help would be appreciated!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,967 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,488 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hitesh Pachipulusu - MSFT 1,575 Reputation points Microsoft Vendor
    2024-09-24T10:29:45.31+00:00

    Hello Doug Horton,

    Thank you for reaching out to Microsoft Support!

    It looks like you’re on the right track with your PowerShell script! The issue you’re encountering with the PATCH requests might be due to the specific endpoint you’re using or the format of your request body. Here are a few steps and tips to help you troubleshoot and resolve the issue:

    1. Correct Endpoint:
      • For updating custom columns in a SharePoint Document Library, you should use the following endpoint:
             PATCH https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items/{item-id}/fields
        
      • Ensure that you replace {site-id}, {list-id}, and {item-id} with the actual IDs.
    2. Request Body Format:
      • The request body should be a JSON object with the fields you want to update. Make sure the field names match the internal names of the columns in your SharePoint list. Here’s an example:
             {
               "Position": "XXX",
               "City": "XXXXXXXX",
               "Email": "XXX@XXXX.com",
               "State": "XX",
               "Zip": "XXXXX",
               "Address": "XXXXXXXXXXX"
             }
        
    3. Common Issues:
      • 400 Bad Request: This usually indicates a problem with the request format, or the data being sent. Double-check the JSON structure and ensure all required fields are included.
      • 404 Not Found: This typically means the endpoint URL is incorrect or the item does not exist. Verify the IDs used in the URL.
    4. Permissions:
      • Ensure that the access token you’re using has the necessary permissions to update items in the SharePoint list. You might need permissions like Sites.ReadWrite.All.

    Please refer to below screenshot.

    image (26)

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.