2

I have troubles with CAML Query and GetItems method from SPList.

I get a SPList from web context:

SPList docTypeList = _web.GetList(string.Format("{0}{1}", web.ServerRelativeUrl, "DocumentType"));

The list contains multiple values.

I want to search in it only the value which has a field called DocumentType with the value "MyValue1". (case sensitive)

I am using the CAML Query:

var queryBuilder = new CAMLQueryBuilder();
            queryBuilder.AddComparison("DocumentType", CAMLQueryBuilder.COMPARISON_TYPE.Eq, "MyValue1");

var query = new SPQuery { Query = queryBuilder.GenerateCAML() };
SPListItemCollection queryResultItems = docTypeList.GetItems(query);

In this case it returns as result a record which has the field "DocumentType" having the value "myvalue1", it does not take care about string sensitive.

Is there a possibility to get Items considering sensitive cases?

Thanks for suggestions!

6
  • Possible DUPLICATE of stackoverflow.com/questions/11404270/…
    – Evariste
    Jul 28, 2015 at 15:40
  • 1
    It's all case insensitive, after getting the results, you'd need to iterate thru and do your own case sensitivity checking. Jul 28, 2015 at 15:42
  • @Evariste -> thanks. It seems the query are case-insensitive, and it is necessary to filter the results.
    – mihai
    Jul 28, 2015 at 15:46
  • @Eric -> you're right, thanks. Maybe you know, is there a simple way of iterating through items within the SPListItemCollection and extract item Fields values ?
    – mihai
    Jul 28, 2015 at 15:46
  • 2
    The only way is to run your CAML query, get all results (case-insensitive) and process each of them in a foreach loop to check item["DocumentType"]against "MyValue1"(case-sensitive comparision with string.Equals).
    – Evariste
    Jul 28, 2015 at 15:48

1 Answer 1

3

As @Evariste and @Eric mentioned in comments: CAML Query is case-insensitive.

The solution was to filter the results from

SPListItemCollection queryResultItems = docTypeList.GetItems(query);

and check for case-sensitive.

I created a small method which does this.

private SPListItem FindCaseSensitive(string documentType, IEnumerable items)
{
    SPListItem foundItem = null;
    if (items == null) return null;

    foreach (var item in items)
    {
        var spItem = item as SPListItem;
        if (spItem == null) continue;

        var propertyValue = spItem["DocumentType"];
        if (!propertyValue.ToString().Equals(documentType)) continue;

        foundItem = spItem;
        break;
    }
    return foundItem;
}

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.