SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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'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'); }
share|improve this question
can you let us know how you solved this problem? I am facing a similar issue – user24631 Mar 25 '14 at 11:52
up vote 1 down vote accepted

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.

share|improve this answer
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 Apr 16 '13 at 13:44
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 Apr 16 '13 at 14:25
Does CAML query works fine? What changes you made? – suren Apr 17 '13 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 Apr 17 '13 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 Apr 17 '13 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>
share|improve this answer
I updated with more of the code – topcat3 Apr 16 '13 at 13:46

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.