I have read lots of articles on C# Corner, as well as on other online websites about CRUD operations on Sharepoint List. In most articles, I have found some redundant code which can be reused and utilized in such a way that any new developer or learner can learn from the code.

So, based on that, I have started working on creating SPRest Utility which will do lots of work internally to increase development speed with much less coding. As I am still working and it is not fully optimized, there is still redundant code and methods which I have to delete.
SPRest.js

You might be wondering how to use SPRest for CRUD Operation in SharePoint.

First of all, save the above code in a file and then, add the reference to it on the page wherever you want to use it.
Once you have added references, then create the instance of SPRest with one parameter which accepts Sharepoint rootUrl used internally or generating request URL for REST API.

 

For demonstration purposes, I am using “PlaceHolderList” that is having the following columns.
  • Title -Single line of Text
  • UserId – Number
  • Completed -Single line of Text

Get All List Items from Sharepoint List

By default, SharePoint List returns the top 100 records if you have more than 100 records in the list.

Get List Item from SharePoint List by Id

Add New List Item to SharePoint List

Update New List Item By Id in SharePoint List

Delete New List Item By Id in SharePoint List

As per the above code example for CRUD functionality, it looks like a cleaner and easier way to perform the CRUD opeation on SharePoint List. It has a lot of redundant code.
  • Generate Request Url Based on Root Site and List Name and Id
  • Header accept and Content type
  • RequestDigest Headers
  • Combined Delete/Update/Add  Code with reusable functions to prepare payload data for the request
  • Adding and applying metadata for ListItem based on ListName
I removed and combined with reusable functions so it can be used for extending List and Document Library in future. I hope you find this article and utilities helpful. Let me know your comments and any suggestion to extend SPRest util.

Conclusion

In this article, we have learned about an easy and cleaner way to do CRUD Operations with SharePoint List Items using SPRest.