Skip to content

Instantly share code, notes, and snippets.

@joelpalmer
Created March 28, 2015 13:04
Show Gist options
  • Save joelpalmer/0b29fe11bb8e104ac688 to your computer and use it in GitHub Desktop.
Save joelpalmer/0b29fe11bb8e104ac688 to your computer and use it in GitHub Desktop.
SharePoint 2013 client-side base list read code
//We need to run our function only after sp.js file loads
ExecuteOrDelayUntilScriptLoaded(ViewItem, "sp.js");
function ViewItem()
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('<<List Name>>');
//It will fetch all the items from the list
var myquery = new SP.CamlQuery.createAllItemsQuery();
myItems = list.getItems(myquery);
context.load(myItems);
//If the items load correctly then success function will be called
context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}
function success()
{
var text="";
var LinkURL= "";
var TextURL="";
var cssClass="";
var ListEnumerator = this.myItems.getEnumerator();
text='<ul>';
while(ListEnumerator.moveNext())
{
//Will get the current row from the SP List
var currentItem = ListEnumerator.get_current();
//Will get the "Url" column from the current row
LinkURL = currentItem.get_item('Url') ;
TextURL= currentItem.get_item('Title');
cssClass= currentItem.get_item('CssClass');
//String is formed with links pointing to URLs with css applied
text=text+'<li class="'+cssClass+'">';
text=text+('<a href="' + LinkURL+ '">'+TextURL+'</a></li>');
}
text=text+'</ul>';
//Getting the empty div tag
fieldNameElement=document.getElementById('LeftNavForMySite');
//Assign the string formed to the div element's innerHTMl to display on page
fieldNameElement.innerHTML=text;
}
function failed(sender, args)
{
alert("failed. Message:" + args.get_message());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment