I want to create an Sharepoint hosted app but how to access list in these App ??
3 Answers
Here is a sample code of JavaScript Client Object Model to perform action you want.
var context;
var hostweburl;
var appweburl;
var appContextSite;
var list;
var web;
$(document).ready(function () {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getUrl);
});
function getUrl () {
hostweburl = getQueryStringParameter("SPHostUrl");
appweburl = getQueryStringParameter("SPAppWebUrl");
hostweburl = decodeURIComponent(hostweburl);
appweburl = decodeURIComponent(appweburl);
var scriptbase = hostweburl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js",
function () { $.getScript(scriptbase + "SP.RequestExecutor.js", execOperation); }
);
}
);
event.preventDefault();
}
function execOperation() {
context = new SP.ClientContext(appweburl);
var factory =
new SP.ProxyWebRequestExecutorFactory(
appweburl
);
context.set_webRequestExecutorFactory(factory);
appContextSite = new SP.AppContextSite(context, hostweburl);
web = appContextSite.get_web();
context.load(web);
list = web.get_lists().getByTitle("User Detail");
context.load(list);
context.executeQueryAsync(onSuccess, onFail);
}
function onSuccess() {
alert("List loaded Successfully");
}
// This function is executed if the above call fails
function onFail(sender, args) {
alert( args.get_message());
}
function getQueryStringParameter(paramToRetrieve) {
var params =
document.URL.split("?")[1].split("&");
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve)
return singleParam[1];
}
}
-
-
1@Torque I think the executeQueryAsync statement and the success and callback functions are missing from your code. Commented Jan 7, 2013 at 7:01
-
-
@RajeshJoshi please check the code again. I have updated the code. Commented Jan 7, 2013 at 7:07
-
its giving error at hostweburl = getQueryStringParameter("SPHostUrl"); Commented Jan 7, 2013 at 7:16
In SharePoint 2013 when you create any SharePoint Hosted App it will create separate web for the app you created so when you will open your app check the URL there will be
1) SPHostUrl 2) SPAppWebUrl
If you want to access List from SpHostWeb you will have to use Cross Domain Concept to access it.
Follow this MSDN Link it shows how to access List in SharePoint hosted app.
-
-
Do one thing. Open your App which you have created and then check this Link below.. msdn.microsoft.com/en-us/library/fp179925.aspx Commented Jan 7, 2013 at 7:18
-
Problem with your MSDN link, is that it assumes you are packaging the List with your (Client)WebApp. Here is my problem: sharepoint.stackexchange.com/questions/94059/… Commented Mar 25, 2014 at 18:29
First, you need to use the SP.AppContextSite" instead of default "SP.ClientContext" to get the SharePoint site URL.
SP.AppContextSite used to get the host site URL and SP.ClientContext used to get the app web URL.
Please refer my below link on how to access the list: http://sharepointquicksolutions.blogspot.in/2013/01/is-list-exists-or-not-using-ecmascript.html
Please reach out me for any issue or additional information required.
-Thanks, Sasi Kumar Reddy http://sharepointquicksolutions.blogspot.in