Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers 3

up vote 1 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); }
                        );
                    }
                );
            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];
                }
            }
share|improve this answer
    
its giving error.. –  Rajesh Joshi Jan 7 '13 at 6:48
1  
@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
show 11 more comments

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.. msdn.microsoft.com/en-us/library/fp179925.aspx –  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: sharepoint.stackexchange.com/questions/94059/… –  GoldBishop Mar 25 at 18:29
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

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.