Wednesday, 12 August 2015

Get list in Sharepoint Hosted App

Code in App.JS

'use strict';

///The helper method to manage the Host and App Web Url
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];
        }
    }
}


var hostWebUrl;
var appWebUrl;
var list;

// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
$(document).ready(function () {
    hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
    appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
    getUserName();
});

// This function prepares, loads, and then executes a SharePoint query to get the current users information
function getUserName() {
    var context = new SP.ClientContext(appWebUrl);
    var appCtxSite = new SP.AppContextSite(context, hostWebUrl);
    var web = appCtxSite.get_web();
    list = web.get_lists().getByTitle("MySAPLst");
    context.load(list);
    context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}


// This function is executed if the above call is successful
// It replaces the contents of the 'message' element with the user name
function onGetUserNameSuccess() {
    $('#message').text('Hello ' + list.get_title());
}
// This function is executed if the above call fails
function onGetUserNameFail(sender, args) {
    alert('Failed to get user name. Error:' + args.get_message());
}



No comments:

Post a Comment