I try to build an SharePoint hosted app, have started with a custom menu action. When I choose it I want to copy the selected item and automatically change some metadata for it.
Problem is I can't get the data from the selected item.
I have tested this example but don't get it to work: Copy List items using REST/JSON
function copyListItem(webUrl, sourceListName, targetListName, listItemId) {
getListItem(webUrl, sourceListName, listItemId,
function (sourceItem) {
//prepare item
var copyItem = {};
var targetItemType = GetItemTypeForListName(targetListName);
copyItem['__metadata'] = { 'type': targetItemType }; //mandatory, do not remove metadata attribute!
//specify explicitly what fields to copy here..
//...
copyItem['Title'] = sourceItem['Title']; //e.g. copy title
createListItem(webUrl, targetListName, copyItem, function (targetItem) {
complete(sourceItem, targetItem);
}, failure);
},
failure);
}
function getListItem(webUrl, listName, listItemId) {
$.ajax({
url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + listItemId + ")",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
complete(data.d);
},
error: function (data) {
failure(data);
}
});
}
I'm trying with or without complete and failure but then I got
Uncaught ReferenceError: complete/failure is not defined ??
and when I use complete and failure in calls I get :
XMLHttpRequest cannot load http://dev1.sp2013.local/_api/web/lists/getbytitle('skistardoks')/items(33). No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://app-33cc412e378e5f.app.dev1.sp2013.local' is therefore not allowed access. The response had HTTP status code 401.
--
using this call is ok?
$.getScript(scriptbase + 'SP.RequestExecutor.js', getListItem(hostweburl, "skistardoks", "33", "complete", "failure"));
new code:
function getListItem(webUrl, listName, listItemId, complete, failure) {
var executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: webUrl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('" + listName + "')/items(" + listItemId + ")?@target='" + encodeURIComponent(webUrl) + "'",
method: "GET",
headers: { 'Accept': 'application/json; odata=verbose' }, // consider using the other odata options that have a lower payload
success: function (data) {
complete(data.d);
},
error: function (data) {
failure(data);
}
});
error in
Uncaught Error: Invalid field or parameter requestInfo.url. SP.RequestExecutor.js:2