1

here I wish to retrieve the URL of SharePoint hosted list it's like

http://app-0329d4c1001648.spapps.com/sites/site1/EmployeeList/EmpList

I tried some approaches but these are not that much useful,with them I can fetch list which is not hosted. the code I am using

var hostWebUrl;
var appWebUrl;
var appCtxSite;
hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));

function manageQueryStringParameter(paramToRetrieve) {
    var params =
    document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve) {
            return singleParam[1];
        }
    }
}
$(document).ready(function () {   
    var scriptbase = hostWebUrl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.RequestExecutor.js");    
    $(".View").click(function () {             
        listAllCategories();
    });   
});

function listAllCategories() {

    var ctx = new SP.ClientContext(appWebUrl);
    var context = new SP.AppContextSite(ctx, hostWebUrl);
    var web = context.get_web(); //Get the Web 
    var list = web.get_lists().getByTitle("EmpList"); //Get the List

    var query = new SP.CamlQuery(); //The Query object. This is used to query for data in the List

    query.set_viewXml('<View><RowLimit></RowLimit>10</View>');

    var items = list.getItems(query);

    ctx.load(list); //Retrieves the properties of a client object from the server.
    ctx.load(items);

    var table = $("#tblcategories");
    //var table1 = $("#tblcategories1");
    var innerHtml = "<tr><td>Title</td><td> First Name</td><td> Last Name</td><td> Designation</td><td> Department</td><td> DOJ</td><td>Id</td></tr>";

    //Execute the Query Asynchronously
    ctx.executeQueryAsync(
        Function.createDelegate(this, function () {
            var itemInfo = '';
            var enumerator = items.getEnumerator();
            while (enumerator.moveNext()) {
                var currentListItem = enumerator.get_current();
                innerHtml += "<tr><td>" + currentListItem.get_item('Title') + "</td><td>" + currentListItem.get_item('E_FirstName') + "</td><td>" + currentListItem.get_item('E_LastName') + "</td><td>" + currentListItem.get_item('E_Desig') + "</td><td>" + currentListItem.get_item('E_Dept') + "</td><td>" + currentListItem.get_item('E_DOJO') + "</td><td>" + currentListItem.get_item('E_Id') + "</td></tr>";
            }

            table.html(innerHtml);
            //innerHtml1 = " <Table><tr><td><input type='button' name='back' value='Back' class='back'/></td></tr></Table>";
            //table1.html(innerHtml1);
        }),

        Function.createDelegate(this, fail)
        );

}

function fail() {
    alert("Operation failed  " + arguments[1].get_message());
}


but this is not giving me the url of hosted list,how do I achieve that

4
  • Add some code snippet that you have already tried. Jun 2 '16 at 7:13
  • i have added the code to question
    – Madhav
    Jun 2 '16 at 7:27
  • what error ae you getting?
    – Codec
    Jun 2 '16 at 7:42
  • 404 for list,list not found
    – Madhav
    Jun 2 '16 at 8:24
0

This article on Microsoft would be opt one for you.

They mentioned that url:

appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Posts')/items?

in their code.

Hope this helps. If this answer your query please do not forget to mark it as answer.

1
  • thanks,but i tried this approach as well but this is didn't worked for me
    – Madhav
    Jun 2 '16 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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