SPListItemCollection to Dictionary

SharePoint Asked by K. Jones on December 24, 2020

Rather than repeatedly query a SharePoint list for information I need, I wanted to create a dictionary from an SPListItemCollection (where the dictionary key is the “Title” value of each SPListItem, and the value is the SPListItem itself). I tried this:

SPListItemCollection documentCodesListItems = documentCodesList.GetItems(query);

Dictionary<string, SPListItem> dictionary = (from SPListItem i in documentCodesListItems select new { Key = i.Title, Value = (SPListItem)i }).ToDictionary(
                a => a.Key,
                a => a.Value);

But I get "Value does not fall within the expected range." exception. Can anybody see my mistake, or should I do this differently?

One Answer

I was able to reproduce following error when my CAML query is wrong. So it's time to re-check your CAML query again.

Value does not fall within the expected range.

Generating Dictionary from items is perfect. You have to consider following thing while generating Dictionary from items where Title is the Key of Dictionary.

Keys can not be duplicate for Dictionary

Use u2u caml builder for building CAML query

Answered by Atish Dipongkor - MVP on December 24, 2020

Add your own answers!

Related Questions

Sharepoint list field description not picked up in InfoPath form

2  Asked on December 14, 2021 by nick-atkinson


Migrate a webpart from sharepoint 2010 to sharepoint 2013

1  Asked on December 10, 2021 by grzegorz-z


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir