5

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

3 Answers 3

5

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];
                }
            }
14
  • its giving error.. Commented Jan 7, 2013 at 6:48
  • 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
  • Can you please tell me which error?? Commented Jan 7, 2013 at 7:03
  • @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
3

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.

3
2

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

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.