I've created a very simple SharePoint Hosted Add-in, its primary purpose is to create a list on the HostWeb, using the JSOM.
Code:
ExecuteOrDelayUntilScriptLoaded(function () {
var scriptbase = _spPageContextInfo.siteAbsoluteUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.RequestExecutor.js", function () {
$("#CreateStuff").click(function () {
var site = "https://myHostWeb.sharepoint.com/sites/dev";
var context = new SP.ClientContext(site);
var factory = new SP.ProxyWebRequestExecutorFactory(site);
context.set_webRequestExecutorFactory(factory);
var web = context.get_web();
var listcreation = new SP.ListCreationInformation();
listcreation.set_title("MyList1");
listcreation.set_templateType(SP.ListTemplateType.genericList)
var list = web.get_lists().add(listcreation);
context.load(list);
context.executeQueryAsync(onQuerySucceeded, onQueryFailed);
function onQuerySucceeded() {
alert("Created");
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
});
});
}, "sp.js");
I've tried many different ways, and I'm always getting a vague error when it hits OnQueryFailed.
Request Failed, and a correlation ID....
I've hardcoded my hostweb, just to make sure the URL was right... and I've tried using the SP.RequestExecutor, and the normal way of getting the client context.
The user I'm logged in with, is a site collection administrator on the hostweb.
Any ideas? Feel like I'm missing something obvious.
** Updated Code:
var context = SP.ClientContext.get_current();
var appContextSite = new SP.AppContextSite(context, site);
var oWebsite = appContextSite.get_web();
var oList = oWebsite.get_lists();
var listcreation = new SP.ListCreationInformation();
listcreation.set_title("MyList2");
listcreation.set_templateType(SP.ListTemplateType.genericList)
var list = oList.add(listcreation);
context.load(list);
context.executeQueryAsync(onQuerySucceeded, onQueryFailed);
With the above modified code, I'm now getting an access denied error. Too confirm, the logged in account is a site collection admin on the host web. Any Ideas?