0

I need to add items into custom list which is into SharePoint Online modern UI. I tried inserting the data using ajax call but it is not working in it although it is working in Classic UI. Please suggest how can we add items into Sharepoint custom list using client side scripting

4
  • So what is the code you are using?
    – Ciamas
    May 17, 2018 at 8:13
  • @Ciamas : ajax call using Jquery
    – Muskan
    May 17, 2018 at 9:26
  • Still don't know anything about your code and it would simply be best to post it. From the REST perspective there is no difference between the UI's of SharePoint. What is the error you get?
    – Ciamas
    May 17, 2018 at 9:58
  • Which browser you are looking into? How many items do the SharePoint list have? May 17, 2018 at 12:16

2 Answers 2

1

We can add the SharePoint list item using REST API's, following is the code,

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: $("#__REQUESTDIGEST").val()
    accept: "application/json;odata=verbose"
    content-type: "application/json;odata=verbose"
    content-length:length of post body

You can follow the link,

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-lists-and-list-items-with-rest

1
  • $("#__REQUESTDIGEST").val() returns null in modern UI
    – Muskan
    May 18, 2018 at 7:44
0

If you are using the SharePoint Framework to develop and create custom code WebParts, you can (and should) use the context provided in the parent WebPart component this.context and send it to your Root WebPart Component (the one rendered) through its properties.

Now, your component has the context and the capabilities to use the provided SPHttpClient which does the authentication and hardwork for you : this.props.context.spHttpClient. with the methods get(), post() and fetch().

Now if you do not use the SharePoint Framework, you find yourself in a SharePoint Page where you cannot retrieve a Request Digest anymore (and you need one to push data). You must therefore ask for one first, and use it in a X-RequestDigest header in your post requests.

Office365 App Page doesn't have RequestDigest

The method is to make a first POST to <sitecollection>/_api/contextinfo.

Hope this helps!

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.