SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have Created Custom Master Page. and i want to access SharePoint List Data in this Master Page using JavaScript.

I write the following script to access data but it gives error that "sp.clientcontext.get_current() null or undefined"

<script type="text/javascript"> 
var url = window.location.protocol + "//" + + _spPageContextInfo.siteServerRelativeUrl;
var context = new SP.ClientContext.get_current();  // error "sp.clientcontext.get_current() null or undefined"
var clientContext = new SP.ClientContext(url);
var oList = clientContext.get_web().get_lists().getByTitle('MasterPageLinks');

var camlQuery = new SP.CamlQuery();
    '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
    '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' + 
this.collListItem = oList.getItems();

    Function.createDelegate(this, this.onQuerySucceeded), 
    Function.createDelegate(this, this.onQueryFailed)
function onQuerySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();

while (listItemEnumerator.moveNext()) {
    var oListItem = listItemEnumerator.get_current();
    listItemInfo += '\nID: ' + oListItem.get_id() + 
        '\nTitle: ' + oListItem.get_item('Title') + 
        '\nBody: ' + oListItem.get_item('Body');


function onQueryFailed(sender, args) {

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

How can i do this?

share|improve this question
up vote 3 down vote accepted

To use Client OM, you need a reference to SP.js

For production:

<SharePoint:ScriptLink Name="sp.js" runat="server" OnDemand="true" Localizable="false" />

For development purpose:

<SharePoint:ScriptLink Name="sp.debug.js" runat="server" OnDemand="true" Localizable="false" />

Moreover, there is also another way to declare it using the ExecuteOrDelauUntilScriptLoaded method:

var clientContext;


function initialize() {

  this.clientContext = SP.ClientContext.get_current();


Happy SharePointing!

share|improve this answer
still getting same error ... – Rajesh Joshi Jan 10 '13 at 11:32

Try this...


ExecuteOrDelayUntilScriptLoaded(GetDocuments(), "sp.js");

setTimeout(function() {

var listurl = yoursite +"/_vti_bin/lists.asmx";
var listname = "listname";
var query = "";
var soapEnv = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soapenv:Envelope xmlns:soapenv=''><soapenv:Body><GetListItems xmlns=''>"+
              "<listName>" + listname + "</listName> <query><Query>"+query+"</Query></query>"+
              "<viewFields><ViewFields><FieldRef Name='LinkFilename' /></ViewFields></viewFields>"+
              "</GetListItems> </soapenv:Body></soapenv:Envelope>";

   url: listurl,
   beforeSend: function (xhr) {
       xhr.setRequestHeader("SOAPAction", "");
   type: "POST",
   dataType: "xml",
   data: soapEnv,
   complete: function (xData, status) {
  $(xData.responseXML).find("z\\:row").each(function () {
            var title = $(this).attr("ows_Title");
   contentType: "text/xml; charset=utf-8"

}, 5000);

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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