Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint 2010 REST API JQUery Insert, Update, Delete

Can anyone explain or point me to a link with samples of doing Update, Delete using Jquery with the SharePoint 2010 Rest API?

I have the insert working and of course queries since the MSDN documentation explains and every tutorial on the net explains queries but just wondering if anyone ever inserts, updates, deletes data instead of only samples and tutorials on querying? Yes I know I can use the CSOM but I want to learn how this is done via jquery and sharepoint rest?

Also I want to use Merge for updating.

Here's the working insert code:

function insertMilestone() {
            var mileStonesListUrl = "/_vti_bin/listdata.svc/Milestones";    
               var milestone = {};
               milestone.Title = "Testing from REST";

               var entry = JSON.stringify(milestone);

               $.ajax({
                   type: "POST",
                   url: mileStonesListUrl,
                   data: entry,
                   contentType: "application/json; charset=utf-8",
                   error: function (xhr) {
                       alert(xhr.status + ": " + xhr.statusText);
                   },

                   success: function () {
                       getAll();

                   }
               });
           }
like image 226
Fab Avatar asked Jul 23 '13 20:07

Fab


1 Answers

How to perform CRUD operations using SharePoint 2010 REST Interface

Create

In order to perform a Create operation via REST, you must perform the following actions:

  • Create an HTTP request using the POST verb.
  • Use the service URL of the list to which you want to add an entity as the target for the POST.
  • Set the content type to application/json.
  • Serialize the JSON objects that represent your new list items as a string, and add this value to the request body

JavaScript example:

function createListItem(webUrl,listName, itemProperties, success, failure) {

    $.ajax({
        url: webUrl + "/_vti_bin/listdata.svc/" + listName,
        type: "POST",
        processData: false,
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(itemProperties),
        headers: {
            "Accept": "application/json;odata=verbose"
        },
        success: function (data) {
            success(data.d);
        },
        error: function (data) {
            failure(data.responseJSON.error);
        }
    });
}

Usage

var taskProperties = {
    'TaskName': 'Order Approval',
    'AssignedToId': 12
};

createListItem('https://contoso.sharepoint.com/project/','Tasks',taskProperties,function(task){
    console.log('Task' + task.TaskName + ' has been created'); 
  },
  function(error){
    console.log(JSON.stringify(error));
  }
);

Read

In order to perform a Read operation via REST, you must perform the following actions:

  • Create an HTTP request using the GET verb.
  • Use the service URL of the list item to which you want to add an entity as the target for the GET.
  • Set the content type to application/json.

JavaScript example:

function getListItemById(webUrl,listName, itemId, success, failure) {
    var url = webUrl + "/_vti_bin/listdata.svc/" + listName + "(" + itemId + ")";
    $.ajax({
        url: url,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            success(data.d);
        },
        error: function (data) {
            failure(data.responseJSON.error);
        }
    });
}

Usage

getListItemById('https://contoso.sharepoint.com/project/','Tasks',2,function(taskItem){
    console.log(taskItem.TaskName); 
  },
  function(error){
    console.log(JSON.stringify(error));
  }
);

Update

To update an existing entity, you must perform the following actions:

  • Create an HTTP request using the POST verb.
  • Add an X-HTTP-Method header with a value of MERGE.
  • Use the service URL of the list item you want to update as the target for the POST
  • Add an If-Match header with a value of the entity’s original ETag.

JavaScript example:

function updateListItem(webUrl,listName,itemId,itemProperties,success, failure)
{
   getListItemById(webUrl,listName,itemId,function(item){

      $.ajax({
         type: 'POST',
         url: item.__metadata.uri,
         contentType: 'application/json',
         processData: false,
         headers: {
                "Accept": "application/json;odata=verbose",
                "X-HTTP-Method": "MERGE",
                "If-Match": item.__metadata.etag
         },
         data: Sys.Serialization.JavaScriptSerializer.serialize(itemProperties),
         success: function (data) {
                success(data);
         },
         error: function (data) {
                failure(data);
         }
      });

   },
   function(error){
       failure(error);
   });

}

Usage

var taskProperties = {
    'TaskName': 'Approval',
    'AssignedToId': 12  
};


updateListItem('https://contoso.sharepoint.com/project/','Tasks',2,taskProperties,function(item){
    console.log('Task has been updated'); 
  },
  function(error){
    console.log(JSON.stringify(error));
  }
);

Delete

To delete an entity, you must perform the following actions:

  • Create an HTTP request using the POST verb.
  • Add an X-HTTP-Method header with a value of DELETE.
  • Use the service URL of the list item you want to update as the target for the POST
  • Add an If-Match header with a value of the entity’s original ETag.

JavaScript example:

function deleteListItem(webUrl, listName, itemId, success, failure) {
    getListItemById(webUrl,listName,itemId,function(item){
        $.ajax({
            url: item.__metadata.uri,
            type: "POST",
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-Http-Method": "DELETE",
                "If-Match": item.__metadata.etag
            },
            success: function (data) {
                success();
            },
            error: function (data) {
                failure(data.responseJSON.error);
            }
        });
    },
   function (error) {
       failure(error);
   });
}

Usage

deleteListItem('https://contoso.sharepoint.com/project/','Tasks',3,function(){
    console.log('Task has been deleted'); 
  },
  function(error){
    console.log(JSON.stringify(error));
  }
);

Please follow List Items manipulation via REST API in SharePoint 2010 article for a more details.

like image 84
Vadim Gremyachev Avatar answered Nov 12 '22 19:11

Vadim Gremyachev