CRUD operation on SharePoint List using REST/Jquery
In the previous post we have discussed about the CRUD operations on Web object using rest/jquery. In this blog post, we'll discuss about the operations performed on sharepoint list
object using REST api.
Starting with the creation of List property in the "RESTApiHelper" namespace created in previous blog post.
RESTApiHelper.List = function (SiteUrl,ListName) {
this.SiteUrl = SiteUrl;
this.ListName = ListName;
Get List and List Properties
We can get the rest response for list using the url syntax involving the list name as "http://demositeurl/_api/web/lists/GetByTitle('DemoList')" or alternatively you can use the guid of the list as "http://demositeurl/_api/web/lists(guid'51925dd7-2108-481a-b1ef-4bfa4e69d48b')"
this.GetList = function (doSuccess, doError) {
jQuery.ajax({
url: SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')",
type: "GET",
headers: {
"accept": "application/json;odata=verbose"
},
success: doSuccess,
error: doError
});
}
Below is the code to call the function
var restList = new RESTApiHelper.List(siteUrl, ListTitle);
restList.GetList(doSuccess,doError);
Response will be returned in json format. You can see the sample response here.
Create List
this.CreateList = function (ListDescription, doSuccess, doError) {
jQuery.ajax({
url: SiteUrl + "/_api/web/lists",
type: "POST",
data: JSON.stringify({
'__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true,
'BaseTemplate': 100, 'ContentTypesEnabled': true, 'Description': ListDescription, 'Title': this.ListName
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"content-length": 100,
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: doSuccess,
error: doError
});
}Update List
this.UpdateList = function (NewListTitle, NewListDescription, doSuccess, doError) {
jQuery.ajax({
url: SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')",
type: "POST",
data: JSON.stringify({
'__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true,
'BaseTemplate': 100, 'ContentTypesEnabled': true, 'Description': NewListDescription, 'Title': NewListTitle
}
),
headers: {
"IF-MATCH": "*",
"X-HTTP-Method": "MERGE",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: doSuccess,
error: doError
});
}
Delete List
this.DeleteList = function (doSuccess, doError) {
jQuery.ajax({
url: SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')",
type: "POST",
headers: {
"IF-MATCH": "*",
"X-HTTP-Method": "DELETE",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: doSuccess,
error: doError
});
}
Get ListItemEntityTypeFullName
ListItemEntityTypeFullName is required when we are going to create list item in the list using rest syntax. We will see it in action in the next post when we will be working with listitems.
this.ListItemEntityTypeFullName = function (doError) {
var listItemEntityTypeFullName = '';
jQuery.ajax({
url: SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')/listItemEntityTypeFullName",
type: "GET",
headers: {
"accept": "application/json;odata=verbose"
},
success: function (data) {
listItemEntityTypeFullName = data.d.ListItemEntityTypeFullName;
},
error: doError
});
return listItemEntityTypeFullName;
}
}
Click here to get the full script. This is it for the list object. In the next post we will work on list items. So stay tuned and enjoy, have fun with REST and JQuery !!!!
Starting with the creation of List property in the "RESTApiHelper" namespace created in previous blog post.
RESTApiHelper.List = function (SiteUrl,ListName) {
this.SiteUrl = SiteUrl;
this.ListName = ListName;
Get List and List Properties
We can get the rest response for list using the url syntax involving the list name as "http://demositeurl/_api/web/lists/GetByTitle('DemoList')" or alternatively you can use the guid of the list as "http://demositeurl/_api/web/lists(guid'51925dd7-2108-481a-b1ef-4bfa4e69d48b')"
this.GetList = function (doSuccess, doError) {
jQuery.ajax({
url: SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')",
type: "GET",
headers: {
"accept": "application/json;odata=verbose"
},
success: doSuccess,
error: doError
});
}
Below is the code to call the function
var restList = new RESTApiHelper.List(siteUrl, ListTitle);
restList.GetList(doSuccess,doError);
Response will be returned in json format. You can see the sample response here.
Create List
this.CreateList = function (ListDescription, doSuccess, doError) {
jQuery.ajax({
url: SiteUrl + "/_api/web/lists",
type: "POST",
data: JSON.stringify({
'__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true,
'BaseTemplate': 100, 'ContentTypesEnabled': true, 'Description': ListDescription, 'Title': this.ListName
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"content-length": 100,
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: doSuccess,
error: doError
});
}Update List
this.UpdateList = function (NewListTitle, NewListDescription, doSuccess, doError) {
jQuery.ajax({
url: SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')",
type: "POST",
data: JSON.stringify({
'__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true,
'BaseTemplate': 100, 'ContentTypesEnabled': true, 'Description': NewListDescription, 'Title': NewListTitle
}
),
headers: {
"IF-MATCH": "*",
"X-HTTP-Method": "MERGE",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: doSuccess,
error: doError
});
}
Delete List
this.DeleteList = function (doSuccess, doError) {
jQuery.ajax({
url: SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')",
type: "POST",
headers: {
"IF-MATCH": "*",
"X-HTTP-Method": "DELETE",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: doSuccess,
error: doError
});
}
Get ListItemEntityTypeFullName
ListItemEntityTypeFullName is required when we are going to create list item in the list using rest syntax. We will see it in action in the next post when we will be working with listitems.
this.ListItemEntityTypeFullName = function (doError) {
var listItemEntityTypeFullName = '';
jQuery.ajax({
url: SiteUrl + "/_api/web/lists/GetByTitle('" + this.ListName + "')/listItemEntityTypeFullName",
type: "GET",
headers: {
"accept": "application/json;odata=verbose"
},
success: function (data) {
listItemEntityTypeFullName = data.d.ListItemEntityTypeFullName;
},
error: doError
});
return listItemEntityTypeFullName;
}
}
Click here to get the full script. This is it for the list object. In the next post we will work on list items. So stay tuned and enjoy, have fun with REST and JQuery !!!!
This comment has been removed by a blog administrator.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete