The Microsoft Best Practices for SharePoint guide recommends not enumerating entire SPList.Items collections or SPFolder.Files collections.

I have the need to generate a list of all files in the farm, but I'd like to do it in a way that's considered good practice. How should I approach this?

  • why don't you just drop a List View Web Part ? Sep 30, 2013 at 9:26
  • does it matter in what way you do it? like in a webpart, application or through powershell?
    – Ali Jafer
    Sep 30, 2013 at 10:27

1 Answer 1


Use SPQuery - you can limit ammount of retrieved items per request and add custom filters

SPList list = web.Lists["Your List Title"];
SPQuery query = new SPQuery();
query.ViewAttributes = "Scope='RecursiveAll'";
// OPTIONAL // query.Query = "<Where><Eq><FieldRef Name='ContentTypeId' /><Value Type='ContentTypeId'>[Your Content Type ID]</Value></Eq></Where>";
query.RowLimit = 1000;
    SPListItemCollection itemColl = list.GetItems(query);
    foreach (SPListItem item in itemColl)
        // DO YOUR STUFF
    query.ListItemCollectionPosition = itemColl.ListItemCollectionPosition;
} while (query.ListItemCollectionPosition != null);

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.