0

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

But I found an other possible solution here:

https://social.msdn.microsoft.com/Forums/office/en-US/9d1a2995-72c2-45db-9131-39b3eab192cf/how-we-get-random-item-from-list-using-javascript-object-modeljsom?forum=appsforsharepoint

Can someone explain how this will work?

http://server:port/_api/search/query?querytext='ListID:yourListID'&sortlist='[random:seed=5432]:ascending'&rowlimit=1

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 Mar 11 '16 at 9:58
1

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])

0

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.

0

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);
        clientContext.Load<SP.ListItemCollection>(items);
        clientContext.ExecuteQuery();
        if (items.Count > 0)
            listItem = items[0];
        return listItem;
    }`

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.