SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Looking for recommendations. Have a file in a document library with name x.txt. In a workflow code behind, I need to get the file to read its contents. What is the best/most efficient way to do this? Was thinking a linq query on the SPList.Item collection but might be off.

share|improve this question
up vote 2 down vote accepted

I'm not sure if I understand you correctly, but if you have the list, you could get your item like this

var myItem = myList.Items.Cast<SPListItem>().Where(n => n.File.Name == "x.txt").FirstOrDefault();
share|improve this answer
- 1. Although this satisfies the question, there could be serious performance ramifications. Using CAML is the way to go. – contactmatt Jun 22 '15 at 20:32
I agree, the other answer is better in most cases – tarjeieo Jun 25 '15 at 8:05

If you use linq to object, you always retrieve ALL records from DB. It is not good if you have got a lot of data. To reduce records you can use CAML query:

SPQuery query = new SPQuery();    
query.Query = "<Where>" +
                    "<Eq>" +
                         "<FieldRef Name=\"FileLeafRef\"/>" +
                         "<Value Type=\"Text\">" + fileName + "</Value>" +
                    "</Eq>" +
SPListItem item = list.GetItems(query).FirstOrDefault();

In this case items are selected from DB by query and then you can get first item.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.