3

I am writing a SharePoint add-in to add list items to a host web list. I use an AppContextSite object to fetch the list and add an item to it. I then try to do an appSiteContext.load(listItem) call since this is how it is done through a normal client context object. However it appears that this call does not work for an AppContextSite object (error says load is not a function). Anyone know the equivalent call to load a list item to a host web list? I have tried to research however MSDN provides very little documentation about the AppContextSite object. Here is my code:

    function editHostList() {
    var hostWebContext = new SP.AppContextSite(context, "https://urimdev.byu.edu/cmoodysite");
    var oList = hostWebContext.get_web().get_lists().getByTitle('Scripts Test');

    var itemCreateInfo = new SP.ListItemCreationInformation();
    var oListItem = oList.addItem(itemCreateInfo);
    oListItem.set_item('Title', 'myItem');
    oListItem.set_item('Amount', '5');
    oListItem.update();

    hostWebContext.load(oListItem);
    hostWebContext.executeQueryAsync(
        Function.createDelegate(this, onQuerySucceeded),
        Function.createDelegate(this, onQueryFailed)
    );

    function onQuerySucceeded() {
        alert('Item created: ' + oListItem.get_id());
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() +
            '\n' + args.get_stackTrace());
    }
}

Any insight will be greatly appreciated.

1 Answer 1

0

You should use context to call load and executeQueryAsync, which is:

context.load(oListItem);
context.executeQueryAsync(
    Function.createDelegate(this, onQuerySucceeded),
    Function.createDelegate(this, onQueryFailed)
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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