3

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

0

In order to modify data on the host site you need to use the Cross-Domain Library. For SharePoint-Hosted apps, this is the Sp.RequestExecutor.js library. To get your code to work, simply replace the $.ajax() with the cross-domain equivalent:

var executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
    url: appweburl + "/_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);
    }
});

Note that the executor uses method in place of type, and body in place of data.

In order to access the library, either utilize $.getScript(), or add the following script tag:

<script type="text/javascript" src="/_layouts/15/SP.RequestExecutor.js"></script>

Here is a working example from Microsoft.

  • thank you, changed it but then I get: codeUncaught Error: Invalid field or parameter requestInfo.url. SP.RequestExecutor.js:2 Uncaught Sys.InvalidOperationException: Sys.InvalidOperationException: Type SP.IWebRequestExecutorFactory has already been registered. The type may be defined multiple times or the script file that defines it may have already been loaded. A possible cause is a change of settings during a partial update.code – mapa0402 Oct 17 '14 at 13:59
  • Can you add your updated code and specify which line causes the error? – wjervis Oct 17 '14 at 14:08
  • You are setting the appweburl variable, yes? – wjervis Oct 17 '14 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.