2

I want to get list item with caml query for SharePoint Hosted Add'in.

Query will returned one item.. This is my code:

function getUserData() {
    var employeeList = clientContext.get_site().get_rootWeb().get_lists().getByTitle('Служители');
    var userTitle = user.get_title();
    var collListItem;
    var oListItem;

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(
                          "<View><Query>" +
                          "<Where>" +
                                  "<Eq><FieldRef Name=\"Title\"/><Value Type=\"Text\">" + userTitle + "</Value></Eq>" +
                          "</Where>" +
                          "</Query></View>");
    collListItem = employeeList.getItems(camlQuery);

    clientContext.load(collListItem, 'Include(Title, empNickName, empPosition, empDepartment)');
    clientContext.executeQueryAsync(
        Function.createDelegate(this, successHandler),
        Function.createDelegate(this, errorHandler)
    );

    function successHandler() {
        var listItemEnumerator = collListItem.getEnumerator();
        oListItem = listItemEnumerator.get_current();
        userDepartment.innerHTML = oListItem.get_item('empNickName');
    }

    function errorHandler() {
        userDepartment.innerHTML = "Request failed: " + arguments[1].get_message();
    }
}

Where i'm wrong ? Thank you previously!

3

You need to put while loop for listItemEnumerator.

Try below code:

function getUserData() {
    var employeeList = clientContext.get_site().get_rootWeb().get_lists().getByTitle('Служители');
    var userTitle = user.get_title();
    var collListItem;
    var oListItem;

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(
                          "<View><Query>" +
                          "<Where>" +
                                  "<Eq><FieldRef Name=\"Title\"/><Value Type=\"Text\">" + userTitle + "</Value></Eq>" +
                          "</Where>" +
                          "</Query></View>");
    collListItem = employeeList.getItems(camlQuery);

    clientContext.load(collListItem, 'Include(Title, empNickName, empPosition, empDepartment)');
    clientContext.executeQueryAsync(
        Function.createDelegate(this, successHandler),
        Function.createDelegate(this, errorHandler)
    );

    function successHandler() {
        var listItemEnumerator = collListItem.getEnumerator();
        while(listItemEnumerator.moveNext()) {    
        var oListItem = listItemEnumerator.get_current();
        alert(oListItem.get_item('empNickName'));
        }
        //userDepartment.innerHTML = oListItem.get_item('empNickName');
    }

    function errorHandler() {
        userDepartment.innerHTML = "Request failed: " + arguments[1].get_message();
    }
}

It will give you alert for all your items.

Thanks @Kai.

Reference:

Hope this will help you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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