-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript6.js
More file actions
137 lines (119 loc) · 4.58 KB
/
Script6.js
File metadata and controls
137 lines (119 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var hostweburl = _spPageContextInfo.siteAbsoluteUrl;;
var appweburl = window.location.protocol + "//" + window.location.host + _spPageContextInfo.webServerRelativeUrl;;
var eTag;
var formDigestValue = "";
var scriptbase = "";
$(document).ready(function () {
//var scriptbase = hostweburl + "/_layouts/15/";
//$.getScript(scriptbase + "SP.RequestExecutor.js", retrieveFormDigest);
$("#btnSubmit").on("click", function () {
prepareItem();
addItem();
});
});
function prepareItem() {
var scriptbase = hostweburl + "/_layouts/15/";
$.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
}
function execCrossDomainRequest() {
// var scriptbase = hostweburl + "/_layouts/15/";
// $.getScript(scriptbase + "SP.RequestExecutor.js", retrieveFormDigest);
var executor = new SP.RequestExecutor(appweburl);
var fullUrl = hostweburl + appweburl + "/_api/web/lists/getbytitle('Feedback')/items";
alert(fullUrl);
var listname = "Feedback";
var itemType = GetItemTypeForListName(listname);
var requestBody = JSON.stringify({
'__metadata': { 'type': itemType },
'Title': "kristest",
'Category': "General Feedback",
'Description': "hello"
});
executor.executeAsync(
{
url: fullUrl,
method: "POST",
body: requestBody,
headers: {
"content-length": requestBody.length,
"Content-Type": "application/json;odata=verbose"
},
success: function (successData) {
var dataMsg = "insert successful";
alert("good here " + dataMsg);
$('#digest').text(dataMsg);
},
error: function (errorData, errorCode, errorMessage) {
var errMsg = "Error retrieving the form digest value: " + errorMessage;
$("#error").text(errMsg);
}
}); //executeAsync
}
function GetItemTypeForListName(list) {
return "SP.Data." + list.charAt(0).toUpperCase() + list.split(" ").join(" ").slice(1) + "ListItem";
}
//notes
function retrieveFormDigest() {
var contextInfoUri = appweburl + "/_api/contextinfo";
var executor = new SP.RequestExecutor(appweburl)
executor.executeAsync({
url: contextInfoUri,
method: "POST",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
var jsonObject = JSON.parse(data.body);
formDigestValue = jsonObject.d.GetContextWebInformation.FormDigestValue;
$('#digest').text(formDigestValue);
},
error: function (data, errorCode, errorMessage) {
var errMsg = "Error retrieving the form digest value: "
+ errorMessage;
$("#error").text(errMsg);
}
});
}
//METHOD 2 FORBIDDEN
/*
function execCrossDomainRequest() {
// executor: The RequestExecutor object
// Initialize the RequestExecutor with the app web URL.
var executor = new SP.RequestExecutor(appweburl);
var fullUrl = appweburl + "/_api/web/lists/getbytitle('Feedback')/items";
// Issue the call against the app web.
// To get the title using REST we can hit the endpoint:
// addinweburl/_api/web/lists/getbytitle('listname')/items
// The response formats the data in the JSON format.
// The functions successHandler and errorHandler attend the
// sucess and error events respectively.
var listname = "Feedback";
var itemType = GetItemTypeForListName(listname);
var item = {
'__metadata': { 'type': itemType },
'Title': "kristest",
'Category': "General Feedback",
'Description': "hello"
};
executor.executeAsync(
{
url: fullUrl,
method: "POST",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(item),
dataType: "json",
contentType: "application/json;charset=utf-8",
headers: {
"Accept": "application/json; odata=verbose",
"X-RequestDigest": $('#digest').val()
},
success: function (data) {
var jsonObject = JSON.parse(data.body);
formDigestValue = jsonObject.d.GetContextWebInformation.FormDigestValue;
$('#digest').text(formDigestValue);
},
error: function (data, errorCode, errorMessage) {
var errMsg = "Error retrieving the form digest value: " + errorMessage;
$("#error").text(errMsg);
}
}); //executeAsync
}
*/