Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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"> 
window.onload=function(){
var url = window.location.protocol + "//" + window.location.host + _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();
camlQuery.set_viewXml(
    '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
    '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' + 
    '<RowLimit>10</RowLimit></View>'
);
this.collListItem = oList.getItems();

clientContext.load(collListItem);
clientContext.executeQueryAsync(
    Function.createDelegate(this, this.onQuerySucceeded), 
    Function.createDelegate(this, this.onQueryFailed)
); 
};
function onQuerySucceeded(sender, args) {
debugger;
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');
}

alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {

alert('Request failed. ' + args.get_message() + 
    '\n' + args.get_stackTrace());
}
</script>

How can i do this?

share|improve this question
add comment

2 Answers

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;

ExecuteOrDelayUntilScriptLoaded(initialize,"SP.js");

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
add comment

Try this...

$(document).ready(function(){


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='http://schemas.xmlsoap.org/soap/envelope/'><soapenv:Body><GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>"+
              "<listName>" + listname + "</listName> <query><Query>"+query+"</Query></query>"+
              "<viewFields><ViewFields><FieldRef Name='LinkFilename' /></ViewFields></viewFields>"+
              "</GetListItems> </soapenv:Body></soapenv:Envelope>";

$.ajax
({
   url: listurl,
   beforeSend: function (xhr) {
       xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");
   },
   type: "POST",
   dataType: "xml",
   data: soapEnv,
   async:false,
   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
add comment

Your Answer

 
discard

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.