Retrieving a List Title Using Javascript

Recently had a project where I needed display the title of a SharePoint list inside an html file.

This can be accomplished by working with SharePoint’s Client Object Model using some Javascript.

In this example I use JavaScript to retrieve the title from the SharePoint list called ‘MySharePointList.’

Start first by creating an empty div inside the body tag and give it an id value.

<div id="MyListTitle" > </div>

Next use the javascript below:

<script type="text/javascript"> 
ExecuteOrDelayUntilScriptLoaded(

function GetListTitle() {

clientContext = new SP.ClientContext.get_current();

spWebobj = this.clientContext.get_web();

spListobj&nbsp;= spWebobj.get_lists().getByTitle("MySharePointList");

var spCamlQuery = new SP.CamlQuery();

var spViewXML = '';

spCamlQuery.set_viewXml(spViewXML);

spListobjItems = spListobj.getItems(spCamlQuery);

clientContext.load(spWebobj);

clientContext.load(spListobjItems);

clientContext.executeQueryAsync(onSuccess, onFail);

}, "sp.js");

function onSuccess() {

liCount = spListobjItems.get_count();

for (x = 0; x &lt; liCount; x++) {

var listItem = spListobjItems.itemAt(x);

var itemTitle&nbsp; = listItem.get_item('Title');

document.getElementById("MyListTitle").innerHTML&nbsp;= itemTitle;

}

}

function onFail() {

alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());

}

</script> 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: