So back again with this code, I got some help on yesterday...

The code uses SP.js functionality to grab all the entries from a list which is populated by a UPS extract (that bit is working fine so out of scope for this question). Right now it's using alert() to pump out the usernames but that was just to prove it works.

What I need it to do in the code below is only retrieve the item from DS_HaveYouMet where the value of list column scriptIndex is equal to the random number generated in variable currentUserIndex; properties of that user object will then be inserted into the div at the top of the web part (it's for one of those 'Have You Met' type WPS that shows a random user).

I know it's something to do with constructing the CAML query but I'm not so hot on doing those (tried a few times before and failed miserably) so any advice as to what I need to change?

<div id="hymUser">Replace me with the count</div>

<script type="text/javascript">

// Call the function once page is loaded

//Shared Global Variables
var clientContext;
var oWeb;
var oList;
var currentUserIndex;
var userCount;

function runWhenLoaded() {
// Create context, get the rootweb, then get the list
        clientContext = SP.ClientContext.get_current(); 
        oWeb =  clientContext.get_web();
        oList = oWeb.get_lists().getByTitle("DS_HaveYouMet");

        // Execute the queued context commands (passes back to the global vars)
        clientContext.executeQueryAsync(runOnSuccess,function() { alert("Error"); } );

// Callback function for async success
function runOnSuccess() {
            // Assign the total items count to the pre-created global variable
            userCount = oList.get_itemCount();

        // Generate a random number between 1 and the number of users
        currentUserIndex = Math.random() * (userCount - 1) + 1;
        currentUserIndex = Math.floor(currentUserIndex);

        // Put the value inside the WP
        document.getElementById('hymUser').innerHTML = 'User ' + currentUserIndex + ' of ' + userCount;

        // Get all list items via CAML
        var camlQuery = SP.CamlQuery.createAllItemsQuery();
        var allItems = oList.getItems(camlQuery);
        clientContext.load(allItems, 'Include(hymDisplayName)');

        //clientContext.executeQueryAsync(runListQuery,function() { alert('List get failed'); } );

                var listEnum = allItems.getEnumerator();
                while (listEnum.moveNext()) {
                    var currentItem = listEnum.get_current();
        },function() { alert('List get failed'); } );



1 Answer 1


Set CAML query to camlQuery.set_viewXml() like below,

var oList = oWeb.get_lists().getByTitle('DS_HaveYouMet');
camlQuery.set_viewXml("<View><Query><Where>" +
                            "<Eq>" +
                                "<FieldRef Name='scriptIndex' />" +
                                "<Value Type='Text'>" + currentUserIndex + "</Value>" +
                            "</Eq>" +
                        " </Where>" +
                        "</Query>" +
collListItem = oList.getItems(camlQuery);
        var listItemEnumerator = collListItem.getEnumerator();
        while (listItemEnumerator.moveNext()) {
            var oListItempname = listItemEnumerator.get_current();

    },function() { alert('List get failed'); }

Note: Refer this Link for CAML basics

  • Does the 'value type=text' cause a problem if the field is not the text data type, or is this not actually related to the field data types and just telling CAML to interpet it as text so it can match? Jul 5, 2017 at 10:24
  • Value Type is the type of column that we are querying in the sharepoint list.
    – Vishnu S
    Jul 5, 2017 at 10:32
  • Refer this link : joshmccarty.com/a-caml-query-quick-reference
    – Vishnu S
    Jul 5, 2017 at 10:33

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.