//Common script for adding and updating list item
//Paramaters Required
//1) AppWebUrl
//2) HostUrl
//3) Listname
//4) Metadata of List item to be saved
//5) Item Id to be updated
//For Save call function as below
//saveitem(AppWebUrl,HostUrl,listName,metadata,function(ItemId,data)
//{
//ItemId -->Saved/Updated Item Id, Returns 0 if error
//Data -->success data
//});
//for update call function as below
// UpdateItem(AppWebUrl,HostUrl,listName,metadata,ItemIdtobeupdated,function(ItemId,data)
//{
//ItemId -->Saved/Updated Item Id, Returns 0 if error
//Data -->success data
//});
//DeleteItem(AppWebUrl, HostWebUrl, ListName,ItemId,function(Success,data)
//{
//Success --> returns 0--> If Error or ItemId--> If Success
//Data -->success data
//});
function saveitem(e, t, o, n, a) {
var Saveexecutor = new SP.RequestExecutor(e);
var r = e + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + o + "')/items?@target='" + t + "'";
Saveexecutor.executeAsync({
url: r,
method: "POST",
body: n,
headers: {
"content-type": "application/json; odata=verbose",
accept: "application/json;odata=verbose"
},
contentType: "application/json",
success: function(e) {
var t = JSON.parse(e.body);
a(t.d.Id,e)
},
error: function(e) {
console.log(JSON.stringify(e.body)), a(0,e)
}
})
}
function UpdateItem(e, t, o, n, a, r) {
var Saveexecutor = new SP.RequestExecutor(e);
var c = e + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + o + "')/items(" + a + ")?@target='" + t + "'";
Saveexecutor.executeAsync({
url: c,
method: "POST",
body: n,
headers: {
Accept: "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-HTTP-Method": "MERGE",
"IF-MATCH": "*"
},
contentType: "application/json",
success: function(e) {
r(a,e)
},
error: function(e) {
console.log(JSON.stringify(e.body)), r(0,e)
}
})
}
function DeleteItem(AppWebUrl, HostWebUrl, ListName,ItemId, callback) {
var Saveexecutor = new SP.RequestExecutor(AppWebUrl);
var c = AppWebUrl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + ListName + "')/items(" + ItemId + ")?@target='" + HostWebUrl + "'";
Saveexecutor.executeAsync({
url: c,
method: "POST",
headers: {
Accept: "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-HTTP-Method": "DELETE",
"IF-MATCH": "*"
},
contentType: "application/json",
success: function(data) {
callback(ItemId,data)
},
error: function(data) {
console.log(JSON.stringify(data.body)), callback(0,data)
}
})
}
//Paramaters Required
//1) AppWebUrl
//2) HostUrl
//3) Listname
//4) Metadata of List item to be saved
//5) Item Id to be updated
//For Save call function as below
//saveitem(AppWebUrl,HostUrl,listName,metadata,function(ItemId,data)
//{
//ItemId -->Saved/Updated Item Id, Returns 0 if error
//Data -->success data
//});
//for update call function as below
// UpdateItem(AppWebUrl,HostUrl,listName,metadata,ItemIdtobeupdated,function(ItemId,data)
//{
//ItemId -->Saved/Updated Item Id, Returns 0 if error
//Data -->success data
//});
//DeleteItem(AppWebUrl, HostWebUrl, ListName,ItemId,function(Success,data)
//{
//Success --> returns 0--> If Error or ItemId--> If Success
//Data -->success data
//});
function saveitem(e, t, o, n, a) {
var Saveexecutor = new SP.RequestExecutor(e);
var r = e + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + o + "')/items?@target='" + t + "'";
Saveexecutor.executeAsync({
url: r,
method: "POST",
body: n,
headers: {
"content-type": "application/json; odata=verbose",
accept: "application/json;odata=verbose"
},
contentType: "application/json",
success: function(e) {
var t = JSON.parse(e.body);
a(t.d.Id,e)
},
error: function(e) {
console.log(JSON.stringify(e.body)), a(0,e)
}
})
}
function UpdateItem(e, t, o, n, a, r) {
var Saveexecutor = new SP.RequestExecutor(e);
var c = e + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + o + "')/items(" + a + ")?@target='" + t + "'";
Saveexecutor.executeAsync({
url: c,
method: "POST",
body: n,
headers: {
Accept: "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-HTTP-Method": "MERGE",
"IF-MATCH": "*"
},
contentType: "application/json",
success: function(e) {
r(a,e)
},
error: function(e) {
console.log(JSON.stringify(e.body)), r(0,e)
}
})
}
function DeleteItem(AppWebUrl, HostWebUrl, ListName,ItemId, callback) {
var Saveexecutor = new SP.RequestExecutor(AppWebUrl);
var c = AppWebUrl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + ListName + "')/items(" + ItemId + ")?@target='" + HostWebUrl + "'";
Saveexecutor.executeAsync({
url: c,
method: "POST",
headers: {
Accept: "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-HTTP-Method": "DELETE",
"IF-MATCH": "*"
},
contentType: "application/json",
success: function(data) {
callback(ItemId,data)
},
error: function(data) {
console.log(JSON.stringify(data.body)), callback(0,data)
}
})
}
No comments:
Post a Comment