Skip to content

Instantly share code, notes, and snippets.

@tristian2
Created January 12, 2016 14:09
Show Gist options
  • Save tristian2/1dc20eb815a1212ef6d8 to your computer and use it in GitHub Desktop.
Save tristian2/1dc20eb815a1212ef6d8 to your computer and use it in GitHub Desktop.
CRUD SharePoint List REST Methods
//just for the console
var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
// ... give time for script to load, then type.
jQuery.noConflict()
//just for the console END
var listurl = "http://sharepointsupport/smallsys/testscr/Lists/TestKnockoutCRUD/AllItems.aspx";
var listURIRoot = "//sharepointsupport/smallsys/testscr";
var xRequestDigest = jQuery("#__REQUESTDIGEST").val();
console.log(xRequestDigest);
var listItemEntityTypeFullName = '';
var listItemId = 58;
var listTitle = "TestKnockoutCRUD";
jQuery.support.cors = true;
//CREATE
var createListItem = function () {
//first get the content type
jQuery.ajax({
crossDomain: true,
headers: {
"Accept": "application/json; odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": xRequestDigest
},
xhrFields: { withCredentials: true },
url: listURIRoot + "/_api/web/lists/GetByTitle('" + listTitle + "')",
type: "GET",
success: function (response) {
listItemEntityTypeFullName = response.d.ListItemEntityTypeFullName
console.log('ok creating list get ListItemEntityTypeFullName ' + listItemEntityTypeFullName);
},
dataType: "json",
error: function (xhr, status) {
console.log('failed fetchting list get ListItemEntityTypeFullName');
}
}).then(function () {
console.log('then');
//now call the create method...
//To do this operation, you must know the ListItemEntityTypeFullName property of the list and pass
jQuery.ajax({
url: listURIRoot + "/_api/web/lists/GetByTitle('" + listTitle + "')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify({
'__metadata': {
'type': listItemEntityTypeFullName
},
'Title': 'Test'
}),
headers: {
"Accept": "application/json; odata=verbose",
"X-RequestDigest": xRequestDigest
},
success: function (response) {
console.log('ok created list item ' + listTitle);
},
dataType: "json",
error: function (xhr, status) {
console.log('failed create list item ' + listTitle);
}
})
}).done(function () {
console.log('done');
});
}
//READ
var getListItems = function () {
//list items sample code
jQuery.ajax({
crossDomain: true,
headers: { "Accept": "application/json; odata=verbose" },
xhrFields: { withCredentials: true },
url: listURIRoot + "/_api/lists/GetByTitle('" + listTitle + "')/items",
type: "GET",
success: function (response) {
jQuery(response.d.results).each(function () {
console.log(this.Modified);
console.log(this.Title);
});
},
dataType: "json",
error: function (xhr, status) {
console.log('error');
}
})
}
//UPDATE
var updateListItem = function () {
//first get the content type
jQuery.ajax({
crossDomain: true,
headers: {
"Accept": "application/json; odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": xRequestDigest
},
xhrFields: { withCredentials: true },
url: listURIRoot + "/_api/web/lists/GetByTitle('" + listTitle + "')",
type: "GET",
success: function (response) {
console.log('ok!');
listItemEntityTypeFullName = response.d.ListItemEntityTypeFullName;
},
dataType: "json",
error: function (xhr, status) {
console.log('error');
}
}).then(function () {
console.log('then');
//now call the update method...
//To do this operation, you must know the ListItemEntityTypeFullName property of the list and pass
jQuery.ajax({
url: listURIRoot + "/_api/web/lists/GetByTitle('" + listTitle + "')/items(" +listItemId + ")",
type: "MERGE",
contentType: "application/json;odata=verbose",
data: JSON.stringify({
'__metadata': {
'type': listItemEntityTypeFullName
},
'Title': 'lorem ipsum'
}),
headers: {
"IF-MATCH": "*",
"Accept": "application/json; odata=verbose",
"X-RequestDigest": xRequestDigest
},
success: function (response) {
console.log('ok!');
console.log(response);
},
dataType: "json",
error: function (xhr, status) {
console.log('error');
}
})
}).done(function () {
console.log('done');
});
}
//DELETE
var deleteListItem = function () { //perhaps make this deletelistitems, so that we can iterate throu an array of ids?
jQuery.ajax({
url: listURIRoot + "/_api/web/lists/GetByTitle('" + listTitle + "')/items(" +listItemId + ")",
type: "DELETE",
contentType: "application/json;odata=verbose",
headers: {
"IF-MATCH": "*",
"Accept": "application/json; odata=verbose",
"X-RequestDigest": xRequestDigest
},
success: function (response) {
console.log('ok del!');
console.log(response);
},
dataType: "json",
error: function (xhr, status) {
console.log('error');
}
})
}
/*CRUD*/
createListItem();
getListItems();
updateListItem();
deleteListItem();
@tristian2
Copy link
Author

make this into the knockout viewModel for a demo of using knockout for FULL list manipulation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment