Saturday, April 15, 2023

A SharePoint JSON Example For Adding In A Record To A List

 
I've been involved in more projects with SharePoint where different stakeholders want to use SharePoint for a front-end but want to get data from different data sources, typically from JSON data, so it makes it easy to help transform that data.

One of the easier ways to add data to a SharePoint List is via JSON with the key/values with a few rules to follow:

  1. If a column name is not found, the post will error out.
  2. The column name has to be the actual column name in the List.
  3. For simple types (see the example of Title and FirstName) it's just a key/value pairing.
  4. For complex types you have to include the __metadata information for each column and the type.

See the example below. 

{
  "__metadata": {
    "type""SP.Data.NewsReservationList_x0020_Request_x0020_NewEntry"
  },

  "Title""New 4:30 Reservation",
  "FirstName""Ginny",
  "LastName""Joe",

  "Time": {
    "__metadata": {
      "type""Collection(Edm.String)"
    },
    "results": [
      "4:30 pm - 6 pm"
    ]
  },

  "HearAboutUs": {
    "__metadata": {
      "type""Collection(Edm.String)"
    },
    "results": [
      "Advertisement",
      "Family"     ]
  }
}

What can be tricky at first is seeing a SharePoint List with one value, even though it can handle more, and thinking it's not a complex type, when it really is.