Debmalya Ghosh
Debmalya Ghosh

Reputation: 89

How to create SharePoint list item with Microsoft Graph API?

I am trying to create list items with Microsoft Graph API to a SharePoint endpoint. My URL is as follows:

https://graph.microsoft.com/v1.0/sites/{id}.sharepoint.com:/sites/{name of the site}:/lists/{list_id}/items

Calling this URL with POST, and a body like this:

{
    "fields": {
        "FileLeafRef": "757391.pdf",
        "ContentType": "Document",
        "Application_x0020_Name": "ABC",
    }
}

It is giving error as

"message": "Files and folders should only be added to a DocumentLibrary via the OneDrive API"

Can some one help on this, how to fix this issue?

Here I am trying to create metadata for document, with in a list.

Upvotes: 1

Views: 4503

Answers (1)

Marc LaFleur
Marc LaFleur

Reputation: 33122

You cannot upload a file (i.e. create a new ListItem in a Document Library) like that. You will need to first upload the file using the OneDrive endpoints:

PUT /v1.0/sites/{site-id}/drive/items/{parent-id}:/{filename}:/content
POST /v1.0/sites/{siteId}/drive/items/{driveItem-id}/createUploadSession

Once you have the file in the document library, you can then update the metadata on the existing file using the SharePoint endpoints:

PATCH h/v1.0/sites/{site-id}/lists/{list-id}/items/{listItem-id}/fields

{
    "FileLeafRef": "757391.pdf",
    "ContentType": "Document",
    "Application_x0020_Name": "ABC"
}

Upvotes: 3

Related Questions