SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to create an Sharepoint hosted app but how to access list in these App ??

share|improve this question
up vote 4 down vote accepted

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); }

    function execOperation() {
        context = new SP.ClientContext(appweburl);
        var factory =
            new SP.ProxyWebRequestExecutorFactory(
        appContextSite = new SP.AppContextSite(context, hostweburl);
        web = appContextSite.get_web();
        list = web.get_lists().getByTitle("User Detail");
        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 =
                for (var i = 0; i < params.length; i = i + 1) {
                    var singleParam = params[i].split("=");
                    if (singleParam[0] == paramToRetrieve)
                        return singleParam[1];
share|improve this answer
its giving error.. – Rajesh Joshi Jan 7 '13 at 6:48
@Torque I think the executeQueryAsync statement and the success and callback functions are missing from your code. – Vardhaman Deshpande Jan 7 '13 at 7:01
Can you please tell me which error?? – Torque Jan 7 '13 at 7:03
Thank you Vardhaman, I forgot to write that part. – Torque Jan 7 '13 at 7:04
@RajeshJoshi please check the code again. I have updated the code. – Torque Jan 7 '13 at 7:07

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.

share|improve this answer
SharePoint Hosted App doesn't create separate web – Rajesh Joshi Jan 7 '13 at 6:50
Do one thing. Open your App which you have created and then check this Link below.. – Torque Jan 7 '13 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:… – GoldBishop Mar 25 '14 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:

Please reach out me for any issue or additional information required.

-Thanks, Sasi Kumar Reddy

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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