How to create and update a hyperlink field of the SharePoint list item using Graph API

Imagine you have a SharePoint list named "Projects" with the following columns:

  • Title (Single line of text)
  • ProjectUrl (Hyperlink)

To create a new item in the "Projects" list with a hyperlink field using Microsoft Graph API, you can use the following HTTP POST request:

POST https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items
Content-Type: application/json
{
  "fields": {
    "Title": "Project1",
    "ProjectUrl": {
      "Description": "Project 1 home site",
      "Url": "https://www.project1.com"
    }
  }
}

The request will fail with the error message Invalid request. Same for the beta endpoint.

Internally, the request is routed to the internal SharePoint API, which doesn't support creating or updating a hyperlink field.

Good new is that Microsoft is working on this issue and beta version of the internal SharePoint API has support for the hyperlink field.

You can enforce the request to be routed to the beta version of the internal SharePoint API by adding the Prefer:apiversion=2.1 header to your request.

POST https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items
Content-Type: application/json
Prefer: apiversion=2.1
{
  "fields": {
    "Title": "Project1",
    "ProjectUrl": {
      "Description": "Project 1 home site",
      "Url": "https://www.project1.com"
    }
  }
}

To update the hyperlink field of an existing SharePoint list item, you can use the following HTTP PATCH request:

PATCH https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/items/{list-item-id}/fields
Content-Type: application/json
{
  "ProjectUrl": {
    "Description": "Project 1 Home Site",
    "Url": "https://www.project-1.com"
  }
}

Of course, without the Prefer: apiversion=2.1 header, the request will fail with the error message Invalid request.

With the Prefer: apiversion=2.1 header, the request will succeed and the hyperlink field will be updated.

Conclusion

To be able to create and update a hyperlink field of a SharePoint list item using Microsoft Graph API, you need to add the Prefer: apiversion=2.1 header to your requests.

Be aware that the request will be routed to the beta version of the internal SharePoint API, which means that the functionality might change in the future.

0
Buy Me a Coffee at ko-fi.com
An error has occurred. This application may no longer respond until reloaded. Reload x