Question

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
SP.SOD.executeFunc('sp.js','SP.ClientContext',runWhenLoaded);

//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");
        clientContext.load(oList);

        // 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'); } );

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

    }

</script>
Was it helpful?

Solution

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>" +
                        "</View>");
collListItem = oList.getItems(camlQuery);
context.load(oList);
context.load(collListItem);
context.executeQueryAsync(function(){
        var listItemEnumerator = collListItem.getEnumerator();
        while (listItemEnumerator.moveNext()) {
            var oListItempname = listItemEnumerator.get_current();
            alert(oListItempname.get_item('hymDisplayName'));      
        }

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

Note: Refer this Link for CAML basics

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top