2

I've used the following code to retrieve the list items on my SP site. The following code will return "Value does not fall within the expected range" if I try to get the AttachmentFiles:

            bool success = true;
            const int max = 20;  //when i % 25 == 0 , upload all at once 
            int counter = 1;

            //grab all items in list and include AttachmentFiles in load
            List tracker = ctx.Web.Lists.GetByTitle("Tracker");
            string[] viewFields = { "Title" "ThisTime", "Proprietary" };
            ListItemCollection listItems = tracker.GetItems(CamlQuery.CreateAllItemsQuery(5000, viewFields));
            ctx.Load(listItems, _coll => _coll.Include(itm => itm.AttachmentFiles));

            try
            {
                ctx.ExecuteQuery();
            }
            catch (Exception ex)
            {
                LogError.Log("Failed to access list items", ex.Message, ctx);
                return false;
            }

Unless I add "Attachments" as one of the view fields, this returns the range error. When I add "Attachments" as one of the view fields and leave the rest of the code as-is, this does remove the error and give me the attachment collection. However, none of the other fields are retrieved. I'm guessing that I can get around this by adding "item["Title"]" and so on inside the Include method. But is this really the most efficient method? Would I not be better off just excluding the ViewFields parameter? I attempted to avoid using the Include altogether, but the attachment collection is not initialized.

3

You are getting this error since attachments are not included via query (view fields). Attachments column is not the same as AttachmentFiles property, first one only returns status whether attachments are included or not, while the second returns attachments info.

Since your goal is to retrieve a specific set of fields plus attachments included, you could consider the following approach:

List tracker = ctx.Web.Lists.GetByTitle("Tracker"); 
ListItemCollection listItems = tracker.GetItems(CamlQuery.CreateAllItemsQuery(5000));
ctx.Load(listItems, icol => icol.Include(i => i.AttachmentFiles, i => i["Title"], i => i["ThisTime"], i => i["Proprietary"]));

try
{
      ctx.ExecuteQuery();
 }
 catch (Exception ex)
 {
      LogError.Log("Failed to access list items", ex.Message, ctx);
 }
  • I understand that Attachments aren't included in the view fields. I was saying that I get the error unless I include Attachments in the view fields because I actually don't get that error when I add it there. I was wondering if there was a relationship that could be taken advantage of here since this removes the "range" error. /n But.. the answer I was looking for was more about if I should exclude the viewfields at all and just spell them out in the Include. From your answer, I'm thinking that's a yes. Thanks :) – Dinerdo Nov 1 '15 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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