I'm very new to sharepoint and have the following query on my Shared Documents. It is returning null for all three documents I have.

I'm wondering where to check in database to see if values are null or how to debug this basically. I can see where it breaks in VS but doesn't give me any clue as to where null is coming from.


//ExecuteOrDelayUntilScriptLoaded(getWebUserData, "sp.js");
var context = null;
var web = null;
var currentUser = null;
var collListItems = null;
var username = null;
var loginname = null;
var isgettinglist = false;
function DisplayUser() {
    isgettinglist = false;
function DisplayList() {
    isgettinglist = true;
function GetUserData() {
    //## Getting the context
    context = new SP.ClientContext.get_current();
    //## Getting the web
    web = context.get_web();
    //## Getting the current user
    currentUser = web.get_currentUser();
    //## Load the query
    //## Execute the query Async
    context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod),
    Function.createDelegate(this, this.onFailureMethod));
function onSuccessMethod(sender, args) {
    //## Setting the object to get the current user...again...
    var userObject = web.get_currentUser();
    //## Adding the user into a Variable
    username = userObject.get_title();
    loginname = userObject.get_loginName();
    if (isgettinglist == false) {
        alert('User name:' + username + '\n Login Name:' + loginname);
    if (isgettinglist == true) {
        //## Getting the results from the list
function onFailureMethod(sender, args) {
    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
function SayHello() {
    alert('Hello Word');

function GetListByName() {

    //## Get the context again
    context = new SP.ClientContext.get_current();
    //## Get the list
    var list = context.get_site().get_rootWeb().get_lists().getByTitle('Shared Documents');
    //## Builds a CAMEL querty to return items
    var query = new SP.CamlQuery();
    query.set_viewXml('<View><Query><Where><Eq><FieldRef Name="Author"/><Value Type="User">' + username + '</Value></Eq></Where></Query><ViewFields><FieldRef Name="Title"/><FieldRef Name="FileLeafRef"/></ViewFields></View>');
    //## Geting the items
    this.collListItems = list.getItems(query);
    //## Execute the query Async
    context.executeQueryAsync(Function.createDelegate(this, this.mySuccessFunction),                   
    Function.createDelegate(this, this.myFailFunction));

function mySuccessFunction() {
    // Do something useful like loop through your returned list items and output them somewhere
    // create an enumerator to loop through the list with 
    var listItemEnumerator = this.collListItems.getEnumerator();
    var strHtml = "RESULTS:";
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        strHtml += oListItem.get_item('Title');
function myFailFunction() { alert('Request fail'); }
  • can you let us know how you solved this problem? I am facing a similar issue
    – user24631
    Commented Mar 25, 2014 at 11:52

2 Answers 2


Confirm these points before running CAML query,

-> Are you calling GetListByName function after loading SP.js? If not use this statement to call your function after SP.js was loaded.

ExecuteOrDelayUntilScriptLoaded(yourfunction, "SP.js");

-> Are you passing username as full name? For eg: If username is Arun Balaji, you should pass whole name as it is. Here Arun is First name and Balaji is Last name.

  • I can see in -> Team Site Shared Documents Document Library Settings that there is a column named 'Title'. I am calling the function after SP.js
    – topcat3
    Commented Apr 16, 2013 at 13:44
  • 1
    You can try executing the CAML query without where condition. This is to verify whether problem exists in existing CAML query or anything else. You can also try using alert before executing the query to check list loaded or not.
    – Suren
    Commented Apr 16, 2013 at 14:25
  • Does CAML query works fine? What changes you made?
    – Suren
    Commented Apr 17, 2013 at 10:14
  • I couldnt get it to work. It appears the username is fine as to whats in the shared documents table but it just returns null for each document and not the actual name or title. your advice was welcome though. I'll have to work this one out on my own I think
    – topcat3
    Commented Apr 17, 2013 at 10:18
  • Do you have CAML Query Builder? You can verify your query on that. Else try getting all values from Shared Document.
    – Suren
    Commented Apr 17, 2013 at 10:42

In your example I fail to see where do you collect USERNAME. One option could be (let CAML do the job for you by retrieving Current User )

        <FieldRef Name='Author' />
        <Value Type='Integer'><UserID/></Value>


        <FieldRef Name='Author'/>
        <Value Type='Text'>Lastname, Firstname</Value>
  • I updated with more of the code
    – topcat3
    Commented Apr 16, 2013 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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