How to create a new item with different types of fields in a SharePoint list in the Graph API

There are different types of fields you can create in the SharePoint list, but there is no documentation on how to create these fields in the Graph API.

I've tried to create a guide with examples on how to create those fields in the Graph API.

Create a list item

Create a new listItem in a list by making a POST request

POST /v1.0/sites/{site-id}/lists/{list-id}/items

In the request body, specify the fields and their values.

{
  "fields": {
    "Field1Name": <value>,
    "Field2Name": <value>,
    "Field3Name": <value>
  }

The tricky part is that it's not so easy to create some fields, and yes, some fields cannot be created at all via the Graph API.

One-line text field

To create a text field, simply specify the string value.

POST /v1.0/sites/{site-id}/lists/{list-id}/items
{
    "fields": {
        "OneLineTextField": "One line"
    }
}

Multi-line text field

When creating a multi-line text field, use \n as a separator between lines.

POST /v1.0/sites/{site-id}/lists/{list-id}/items
{
    "fields": {
        "MultiLineTextField": "Multi line 1\nMulti line 2"
    }
}

Numeric field

To create a numeric field, specify the numeric value.

POST /v1.0/sites/{site-id}/lists/{list-id}/items
{
    "fields": {
        "NumberField": 25
    }
}

Currency field

To create a currency field, specify the numeric value.

POST /v1.0/sites/{site-id}/lists/{list-id}/items
{
    "fields": {
        "CurrencyField": 25
    }
}

Choice field

To create a choice field, specify the name of a choice.

POST /v1.0/sites/{site-id}/lists/{list-id}/items
{
    "fields": {
        "ChoiceField": "Option 1"
    }
}

Multi-choice field

When creating a multi-choice field, you need to specify the OData type of the multi choice-field. Declare the type as a collection of string values.

POST /v1.0/sites/{site-id}/lists/{list-id}/items
{
    "fields": {
        "MultiChoiceField@odata.type": "Collection(Edm.String)",
        "MultiChoiceField": [
            "Option 1",
            "Option 2"
        ]
    }
}

DateTime field

To create a date time field, use either ISO 8601 format or yyyy-MM-dd format.

POST /v1.0/sites/{site-id}/lists/{list-id}/items
{
    "fields": {
        "DateTimeField": "2023-09-27T15:30:00Z"
    }
}

Lookup field

For the lookup fields, the Graph API has a LookupId field referencing the listItem targeted in the lookup. The name of the LookupId field is the original field name followed by LookupId.

POST /v1.0/sites/{site-id}/lists/{list-id}/items
{
    "fields": {
        "PersonLookupId": "10"
    }
}

Lookup field with multiple values

For the multi lookup fields, you need to declare that the type as a collection of number.

POST /v1.0/sites/{site-id}/lists/{list-id}/items
{
    "fields": {
        "PeopleLookupId@odata.type": "Collection(Edm.Int32)",
        "PeopleLookupId": [
            10,
            20
        ]
    }
}

Yes/No field

To create a yes/no fields, specify the boolean value true/false.

POST /v1.0/sites/{site-id}/lists/{list-id}/items
{
    "fields": {
        "YesNo": true
    }
}

Picture

Creating a picture field is quite tricky and requires more steps.

  1. List hidden site lists to find the id of the system list called SiteAssets.
GET /v1.0/sites/{site-id}/lists?$select=id,name,system

The response looks like this

{
  "value": [
    ..
    {
      "@odata.etag": "\"a9c9beee-0ed4-4c7a-80c3-7c5ffa879cce,28\"",
      "id": "a9c9beee-0ed4-4c7a-80c3-7c5ffa879cce",
      "name": "SiteAssets",
      "system": {}
    },
    ..
  ]
}
  1. Use the id of the SiteAssets list to get a drive associated with the list. The root of the drive contains a folder named Lists with subfolders. The names of the subfolders are identical to the ids of the site list. Upload a picture to the subfolder that matches with the id of the list where you want to create a list item with a picture field.
PUT /v1.0/sites/{site-id}/lists/{assets_list_id}/drive/root:/Lists/{list-id}/file.jpg:/content

The response looks like this

{
    "createdDateTime": "2023-08-28T13:26:07Z",
    "eTag": "\"{836D16C1-5474-49E2-BA91-9D978B9D0B9C},1\"",
    "id": "01HSS52ZGBCZWYG5CU4JE3VEM5S6FZ2C44",
    "lastModifiedDateTime": "2023-08-28T13:26:07Z",
    "name": "file.txt",
    ...
}

Parse the guid from the eTag property (836D16C1-5474-49E2-BA91-9D978B9D0B9C in my case).

  1. Get id of picture field
GET /v1.0/sites/{site-id}/lists/{list-id}/columns?$select=id,name

The response

{
  "value": [
    {
      "id": "b7180741-a517-46b0-93d0-3377285c3ce0",
      "name": "Picture"
    }
  ]
}
  1. Create a list item
POST /v1.0/sites/{site-id}/lists/{list-id}/items
{
    "fields": {
        "Picture": "{\"type\":\"thumbnail\",\"fileName\":\"file.jpg\",\"nativeFile\":{},\"fieldName\":\"Picture\",\"serverUrl\":\"https://your_tenant_name.sharepoint.com\",\"fieldId\":\"{picture_field_name_id}\",\"serverRelativeUrl\":\"/sites/{site_name}/SiteAssets/Lists/{list_id}/file.jpg\",\"id\":\"{guid_from_id}\"}",
    }
}

Managed metadata

You can't directly set the value of your managed metadata field, but you need to set the value of the hidden column name associated with the managed metadata field.

  1. List all columns included the hidden columns
GET /v1.0/sites/{site-id}/lists/{list-id}/columns?$select=id,name,hidden,displayName

The response looks like this

{
  "value": [
    {
      "displayName": "ManagedMetadata",
      "hidden": false,
      "id": "062e47e1-021e-4b11-9632-61c8968209a7",
      "name": "ManagedMetadata"
    },
    {
      "displayName": "ManagedMetadata_0",
      "hidden": true,
      "id": "08be9a90-5e7a-404e-b5f8-77121d7973c3",
      "name": "g62e47e1021e4b11963261c8968209a7"
    }
  ]
}

Suppose that the name of the field is ManagedMetadata. This field has a hidden field with the displayName ManagedMetadata_0 and the name g62e47e1021e4b11963261c8968209a7 which we use in a POST request.

POST /v1.0/sites/{site-id}/lists/{list-id}/items
{
    "fields": {
        "{hidden_field_name}": "{TermGuid}"
    }
}

Where {hidden_field_name} in my case is g62e47e1021e4b11963261c8968209a7.

Managed metadata field with multiple values

The POST request is very similar to the previous one; the only difference is that the term guids are separated by a semicolon.

POST /v1.0/sites/{site-id}/lists/{list-id}/items
{
    "fields": {
        "{hidden_field_name}": "{TermGuid1};{TermGuid2};{TermGuid3}"
    }
}

Creating a new hyperlink field is not supported.

Geolocation field

Creating a new geolocation field is not supported.

Conclusion

Almost all fields except hyperlink and geolocation fields can be created. If you know another way how to update any of the columns mentioned in this article, please leave a comment.

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