How to create SharePoint list item with Microsoft

2019-07-26 06:22发布

问题:

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.

回答1:

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"
}