3

In my SharePoint add-in, I have already utilized the REST call for creating a new list item:

$.ajax({
    url: "../_api/web/lists/getbytitle('Example List')/items",
    method: "POST",
    contentType: "application/json; odata=verbose",
    headers: {
        "Accept": "application/json; odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "contentType": "application/json; odata=verbose",
    },
    data : JSON.stringify({
        __metadata: {"type": "SP.Data.Example_x0020_ListListItem"},
        Title: "_",
        field1: "val1",
        field2: "val2",
        field3: "val3"
    })
})

My question is, how do I add a list item of a custom content type? What exactly do I put as the value for the "type" key in the __metadata object? I was thinking it would be something like

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

but that doesn't seem to work. Any suggestions?

3
  • Any help from my answer? Jul 28, 2016 at 9:53
  • Not Yet, now when I use the exact same request, with the ContentTypeId key/value added, it is throwing an error with the message "To add an item to a document library, use SPFileCollection.Add()"
    – cmoody4
    Jul 28, 2016 at 14:45
  • It works for lists. I have tested. In document library, you can not try this way. At first, you need to upload a file and thereafter you can modify the metadata. AFAIK Jul 28, 2016 at 14:52

1 Answer 1

3

You don't have to deal with __metadata. Just specify your ContentTypeId in your request body and that's all.

data: JSON.stringify({
    __metadata: {
        "type": "SP.Data.Example_x0020_ListListItem"
    },
    Title: "_",
    field1: "val1",
    field2: "val2",
    field3: "val3",
    ContentTypeId: "Your content type id"
})

In the __metadata you need to specify ListItemEntityTypeFullName and it is same for all content types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.