Search code examples
case-sensitivesplist

GetItems case sensitive from SPList (SharePoint)


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!


Solution

  • I found on sharepoint.stackexchange.com an answer:

    It's all case insensitive, after getting the results, it is necessary to iterate through and do the case sensitivity checking.

    Edit:
    Because 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;
    }