Top
Photo by Alexander Grey on Unsplash

Create SharePoint list item in a folder using SPO REST API


With this post I would like to share with you how to easily create an item on a list, but not directly along with other items, but inside a subfolder.

The problem

Trying to create an item inside a subfolder on the list is absolutely impossible using the existing SharePoint Online action.

Create item action

Its UI does not allow to choose the path under which an item should be created. The best solution is to use SharePoint Online REST API.

Use SharePoint REST API

The endpoint you should use to perform such operation is /AddValidateUpdateItemUsingPath with a POST request:

Send HTTP Request to SharePoint
  1. It should be a POST request
  2. You can try the “GetByTitle” uri, although I couldn’t make it work so I switched to guid, and it worked fine ๐Ÿ™‚
  3. Use Accept header for the data that is returned from the successful request and Content-Type to tell the endpoint what type of data is being sent. I am using ;odata=nometadata to receive only the minimal information in response.
  4. DecodedUrl parameter defines the path to the subfolder where item should be created.
  5. List of FieldName + FieldValue can be used to create item with metadata. Just remember, it must be a valid JSON, so if one value contains quotes, they must be escaped.

Basically to set values of basic fields, use the following JSON:

   "formValues": [
    {
      "FieldName": "Title",
      "FieldValue": "VALUE"
    },
    {
      "FieldName": "SingleChoice",
      "FieldValue": "VALUE 1"
    },
    {
      "FieldName": "MultiChoice",
      "FieldValue": "VALUE 1;VALUE 2;VALUE 3"
    },
    {
      "FieldName": "DateTime",
      "FieldValue": "DATE IN CORRECT FORMAT"
    },
    {
      "FieldName": "RichText",
      "FieldValue": "VALUE - REMEMBER TO ESCAPE QUOTES!"
    },
    {
      "FieldName": "Number",
      "FieldValue": "1000.00"
    },
    {
      "FieldName": "YesNo",
      "FieldValue": "True or False"
    },
    {
      "FieldName": "SingleLookup",
      "FieldValue": "1"
    },
    {
      "FieldName": "MultiLookup",
      "FieldValue": "I FAILED HERE :("
    },
    {
      "FieldName": "MultiplePerson",
      "FieldValue": "I FAILED HERE AS WELL"
    }
  ]

Important! I wasn’t able to successfully populate data in complex columns, such as lookup, person or managed metadata. Look for a solution below.

After the action is successfully executed, the response body will contain data for each populated column together with ID of the created item:

{
  "value": [
    {
      "ErrorCode": 0,
      "ErrorMessage": null,
      "FieldName": "Title",
      "FieldValue": "Test 1",
      "HasException": false,
      "ItemId": 0
    },
    {
      "ErrorCode": 0,
      "ErrorMessage": null,
      "FieldName": "Id",
      "FieldValue": "0",
      "HasException": false,
      "ItemId": 0
    }
  ]
}

Important! Even if response code is 200 suggesting, that the request was placed successfully, the item doesn’t need to be created. Check the response body for error messages.

If there is an error, column that caused it will have the following response JSON:

    {
      "ErrorCode": 0,
      "ErrorMessage": "Value does not fall within the expected range.",
      "FieldName": "Lookup",
      "FieldValue": "[1;2]",
      "HasException": true,
      "ItemId": 0
    },

To get all fields, where an error was raised during the creation of item, you can use the “Filter” action on body('Send an HTTP request to SharePoint')?['value'] and filter @equals(item()?['HasException'], true):

Filter response to get array of errors.

In return you will get an array of all fields that caused errors.

Set values of complex fields

I was trying to update (but failed) fields such as:

  • Lookup
  • Person
  • Managed metadata

But I was only able to set value of a single-lookup column ๐Ÿ™ So I came up with a different idea. Use the above action, to create an item under a desired subfolder path, then obtain its ID and use a regular “Update item” action to set the desired values even for the complex field types:

Update create item

To achieve that again use the “Filter” action on body('Send an HTTP request to SharePoint')?['value'] and filter @equals(item()?['FieldName'], 'Id'). Then in get the value of that Id by using the following expression:
body('Filter')?[0]?['FieldValue'].

And that’s it! Let me know how this worked for you.


Tomasz Poszytek

Hi, I am Tomasz. I am expert in the field of process automation and business solutions' building using Power Platform. I am Microsoft MVP and Nintex vTE.

6 Comments
  • Perhotelan

    I found the article on creating a SharePoint list item in a folder using SPO REST API quite informative. It’s amazing how Power Automate can automate repetitive tasks like this with just a few clicks. The step-by-step explanation provided in the article is clear and easy to follow. I can see how this would be helpful in saving time and increasing productivity. Thanks for sharing!

    May 14, 2023 at 9:50 am Reply
  • Levent

    Thanks !! Tomasz, This was really helpfull and solved my problem…

    May 24, 2023 at 1:21 pm Reply
  • Piotr

    Hi Tomasz,
    Great article!
    I found that a managed metadata field can be updated with json below:
    {
    “FieldName”: “field_internal_name”,
    “FieldValue”: “term_label|term_guid”
    }

    December 18, 2023 at 6:33 pm Reply
    • Tomasz Poszytek

      Hey, thank you very much! We are all learning from each others ๐Ÿ™‚

      January 13, 2024 at 7:49 pm Reply

Post a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.