0

I have a custom list in SharePoint (List A) with a choice column (1,2,3).

I have created three items in this list --> item 1, item 2, item 3.

I want to check the value of these items (for the choice column) to get all items that have the value of 2.

Anyone that can help me with this? I prefer to do it in an event receiver if this is possible...

1

First, You should use the CAML Query to retrive the desired items based on a specifc critiria (Choice Field = 2) as the following:

SPQuery Qry = new SPQuery();
Qry.Query = "<Where><Eq><FieldRef Name="choice column name" /><Value Type="Choice">2</Value></Eq></Where>";
Qry.RowLimit = 100;

Note: You can use U2U tool to build your CAML Query based on your requirements, to download the tool check U2U tool

Secondly, you can get all items from a specific list based on your CAML query as the following:

SPList List = myWeb.Lists["ListName"];
SPListItemCollection Items = List.GetItems(Qry);

Finally, Loop for the retrieved items filtered by choice field = 2

foreach (SPListItem Item in Items)
{
 // do something
}

The final code should look like (Not Test)

    SPWeb  Web = properties.OpenWeb();
    SPQuery Qry = new SPQuery();
    Qry.Query = "<Where><Eq><FieldRef Name="choice column name" /><Value Type="Choice">2</Value></Eq></Where>";
    Qry.RowLimit = 100;
    SPList List = web.Lists["ListName"];
    SPListItemCollection Items = List.GetItems(Qry);
    foreach (SPListItem Item in Items)
    {
     // do something
    }
1

If you want to achieve it via JavaScript.

Here is a demo for your reference.

    var siteUrl = 'http://sp/sites/DevSite';
    var clientContext;
    var collListItem;
    var collListItems;

    $(function () {

        clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('TestList');
        var camlQuery = new SP.CamlQuery();
        var camlString = '<Where><Eq><FieldRef Name="choice column name" /><Value Type="Choice">2</Value></Eq></Where>';
        camlQuery.set_viewXml(camlString);
        collListItem = oList.getItems(camlQuery);
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(getAllListItemSucceeded, getAllListItemFailed);
     });

    function getAllListItemSucceeded(sender, args) {
        var listItemEnumerator = collListItem.getEnumerator();
        $("#tbody").empty();
        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            //do something
        }
    }

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.