0

When a new item is added to list, it has one column which I need to update using Rest API.

how can we do this at the time of adding new item?

  • Adding item from NewForm.aspx, at the same time I have a column which has to be updated with some value at the time of adding this item only. It means, while adding a new item, one column will come from rest api – KumarV Dec 7 '15 at 14:10
  • It seems, you will need a custom form rather than NewForm.aspx – Atish Dipongkor - MVP Dec 7 '15 at 14:17
  • Added update 1 to my answer – MikhailSP Dec 7 '15 at 14:27
2

You can just set the value of this field on the creation time as it shown here: Working with lists and list items with REST

url: http://site url/_api/web/lists/GetByTitle(‘Test')/items
method: POST
body: { '__metadata': { 'type': 'SP.Data.TestListItem' }, 'Title': 'Test'}
headers:
    Authorization: "Bearer " + accessToken
     X-RequestDigest: form digest value
    accept: "application/json;odata=verbose"
    content-type: "application/json;odata=verbose"
    content-length:length of post body

Update 1. From you comment it seems that you need event receiver or custom form for that.

  1. If you create an event receiver or workflow where you can hook the item added event and make call to the REST service and update the item.

  2. If you have a custom form, you can make the REST call before creating item, and pass it result to that item on the creation time.

1

If I got your requirement correctly, you need two request to do this.

  1. Add item in first request
  2. Update items in the second request

Adding Item

/_api/Web/Lists/GetByTitle('List Name')/Items

Add item using above end-point. In the response, you will get the ID of this item. Using this ID, make second request to update item.

Updating Item

/_api/Web/Lists/GetByTitle('SpTutorial')/getItemById('Id of old item')

Update #1

OP said that he is using newForm.aspx to add item. In that case, it is not possible to make second request to update this item.

So next workaround is Workflow

Create a Workflow which will update this item. Run this workflow on item created.

  • I am adding item from newForm.aspx, so from there how would I get the ID of the item added? Adding newItem is not done using rest api, It is directly from newform.aspx. only one column is not added from there which needs to be added using rest. – KumarV Dec 7 '15 at 14:20
  • yeah... I saw your comment. You can create a custom form to do this. Or use workflow to update item. – Atish Dipongkor - MVP Dec 7 '15 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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