Is it possible to insert metadata when uploading a file with API graph?

Robin GANTNER 0 Reputation points
2024-09-10T08:12:05.0233333+00:00

Hi there,

I'm trying to extract a lot of documents from an external source on a Sharepoint site. But I need to be able to add some metadata for each document, such as the creation date for example.

First, I create the columns I want to fill :

  • POST /sites/{site-id}/lists/{list-id}/columns

Unfortunately, the only way I've found is to run three requests per document.

I have to upload my document, then get the Id (which is not the id returned by the upload...) , to finally be able to set the column field :

Or, the second request, to get the ID, which by the way requires the header prefer: HonorNonIndexedQueriesWarningMayFailRandomly, will fail on large lists. So it will return an empty array and I won't be able to fill my columns anymore.

So here are my questions:

  • Is it possible to insert metadata when uploading a file with API graph?
  • What is the purpose of the id returned by the first PUT to upload the files? Is it possible to use it to set my columns? Apparently not...
  • Is it possible to get the id that permit me to set the columns other than by this query string?

Thank you very much for helping me :)

Microsoft 365 and Office | SharePoint | Development
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ling Zhou_MSFT 23,670 Reputation points Microsoft External Staff
    2024-09-11T02:51:22.8033333+00:00

    Hi @Robin GANTNER,

    Thank you for posting in this community.

    Q1: Is it possible to insert metadata when uploading a file with API graph?

    Unfortunately, we can't add metadata at the same time as uploading the file. We need to make two requests, one to upload the file and one to update the file's metadata.

    Q2: What is the purpose of the id returned by the first PUT to upload the files? Is it possible to use it to set my columns?

    According to the document put request, part of the information about the driveItem object is returned. where id refers to "The unique identifier of the item within the Drive.". It is different from listId.

    User's image

    User's image

    Q3: Is it possible to get the id that permit me to set the columns other than by this query string?

    We can use siteId, listId, itemId to get the file you uploaded and update the metadata of the file.

    Reference: Update listItem.

    We can also make the above requests in bulk:

    Here’s a simplified example of how you might structure a batch request to update metadata for multiple files:

    POST https://graph.microsoft.com/v1.0/$batch
    Content-Type: application/json
    {
      "requests": [
        {
          "id": "1",
          "method": "PATCH",
          "url": "
          "headers": {
            "Content-Type": "application/json"
          },
          "body": {
            "name": "new-file-name-1.docx",
            "description": "Updated description for file 1"
          }
        },
        {
          "id": "2",
          "method": "PATCH",
          "url": "
          "headers": {
            "Content-Type": "application/json"
          },
          "body": {
            "name": "new-file-name-2.docx",
            "description": "Updated description for file 2"
          }
        }
        // Add more requests as needed
      ]
    }
    
    

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

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

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