Skip to content

Instantly share code, notes, and snippets.

@girishraja

girishraja/App.js

Created May 14, 2013
Embed
What would you like to do?
var context;
var web;
var user;
// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
function sharePointReady() {
context = SP.ClientContext.get_current();
web = context.get_web();
getUserName();
};
// This function prepares, loads, and then executes a SharePoint query to get the current users information
function getUserName() {
user = web.get_currentUser();
context.load(user);
context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}
// This function is executed if the above OM call is successful
// It replaces the contents of the 'helloString' element with the user name
function onGetUserNameSuccess() {
$('#message').text('Hello ' + user.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());
}
$(document).ready(function () {
//Namespace
window.AppLevelECT = window.AppLevelECT || {};
//Constructor
AppLevelECT.Grid = function (hostElement, surlWeb) {
this.hostElement = hostElement;
if (surlWeb.length > 0 &&
surlWeb.substring(surlWeb.length - 1, surlWeb.length) != "/")
surlWeb += "/";
this.surlWeb = surlWeb;
}
//Prototype
AppLevelECT.Grid.prototype = {
init: function () {
$.ajax({
url: this.surlWeb +
"_api/lists/getbytitle('AccountSet')/items?" +
"$select=BdcIdentity,AccountNumber,Name",
headers: {
"accept": "application/json;odata=verbose, */*",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: this.showItems
});
},
showItems: function (data) {
var items = [];
items.push("<table border='1'>");
items.push("<tr><td>Account Number</td>" +
"<td>Account Name</td></tr>");
$.each(data.d.results, function (key, val) {
items.push('<tr id="' + val.BdcIdentity + '"><td>' +
val.AccountNumber + '</td><td>' +
val.Name + '</td></tr>');
});
items.push("</table>");
$("#displayDiv").html(items.join(''));
}
}
ExecuteOrDelayUntilScriptLoaded(getAccounts, "sp.js");
});
function getAccounts() {
var grid = new AppLevelECT.Grid($("#displayDiv"),
_spPageContextInfo.webServerRelativeUrl);
grid.init();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment