I have checked the question here: What is the best way to retrieve random item?

But I found an other possible solution here:


Can someone explain how this will work?


I tried the query, but I always receive the same item and not all the fields. Is this due to managed properties?

My scenario:

List A has, lets say 5 items. The ID's are not 1-5 due to the fact that some items have been deleted.

So the only approach I was thinking of is loop through the list and save all ID's in an array. Then the random number gets one entry from the array. Kind of what Supriyo SB Chatterjee is saying in the other question.

I don't know how to do the approach from Yuri Leontyev in the question.

So I hope someone can get my on to the right track here. Every response will be appreciated.

  • The random:seed query is interesting, I've never seen that before. I'm going to assume however that if you keep using the same seed, you'll always get the same item.
    – Paul Lucas
    Commented Mar 11, 2016 at 9:58

3 Answers 3


It would depend on the number of items in the list. For example if its a small list, (say 100 items or less), then you could generate the random number between 0 and number of items -1, and select the random item from list.Items[randomNumber].

If it's a larger list, then you might be better to query for all the ids of all the items, cache them into an array, and then generate the random number based on the number of items in the array. Then you would use list.getItemById(arrayodids[randomnumber])


When using [random:seed=<seed>], basically you are telling Search component to randomize list items and rowlimit=1 says to take only the very first entry in resulting set.

See documentation:

The seed value is input to a function that generates a random number. This random number is used in the final sorting. Using only the seed option will give you a randomly sorted query result set. The sorting order for the same query (when using the same seed) may change after an index update.

So we can think of <seed> (in your sample, 5432) as of unique result set identifier. Kind of transformation ID. So you'll always get same ID with same <seed> and this might or might not change after reindexing.

@PaulLucas's answer covers both methods available from OM code. I'd prefer to implement the second one: first query to get only IDs, select one, then get all required fields of the item with chosen ID. The only possible problem is that list might be changed between two queries - definitely something to keep in mind.


You can create a function like below to get a list item using CAMl query, that works on all data types and and random value.

`private SP.ListItem FindListItem(List selectedList, Field field, string value, ClientContext clientContext)
        SP.ListItem listItem = null;

        CamlQuery camlQuery = new CamlQuery();          

            camlQuery.ViewXml = @"<View><Query><Where><BeginsWith><FieldRef Name=" + field.InternalName + @"/><Value Type=" + field.TypeAsString + @">" + value + @"</Value></BeginsWith></Where></Query></View>";

        SP.ListItemCollection items = selectedList.GetItems(camlQuery);
        if (items.Count > 0)
            listItem = items[0];
        return listItem;

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.