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'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.

code:

//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;
    GetUserData();
}
function DisplayList() {
    isgettinglist = true;
    GetUserData();
}
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
    currentUser.retrieve();
    context.load(web);
    //## 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
        GetListByName();
    }
}
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);
    context.load(collListItems);
    //## 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');
    }
    alert(strHtml);
}
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 at 11:52
add comment

2 Answers 2

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
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 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
show 2 more comments

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 )

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

or

<Where>
    <Eq>
        <FieldRef Name='Author'/>
        <Value Type='Text'>Lastname, Firstname</Value>
    </Eq>
</Where>
share|improve this answer
    
I updated with more of the code –  topcat3 Apr 16 '13 at 13:46
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.