5

I am trying to fetch list items along with its Attachments and display on a SharePoint page using JavaScript JSOM.

My code contains the pagination too which is mentioned here.

Now, for attachment we have to load the AttachmentFiles field separately. So what I did is,

context = new SP.ClientContext(_spPageContextInfo.siteAbsoluteUrl);
list = context.get_web().get_lists().getByTitle(ListName);
camlQuery = new SP.CamlQuery();
camlQuery.set_listItemCollectionPosition(position);
camlQuery.set_viewXml('myCamelQuery');
spItems = list.getItems(camlQuery);
context.load(spItems,'Include(AttachmentFiles,and all remaining fields)');
context.executeQueryAsync(
// My codes );

As mentioned in managePagerControl() method in given link, I have following code:

if (spItems.get_listItemCollectionPosition()) { 
        nextPagingInfo = spItems.get_listItemCollectionPosition().get_pagingInfo(); 
    } else { 
        nextPagingInfo = null; 
    } 

Now, this code gives error,

The property or field 'ListItemCollectionPosition' has not been initialized. It has not been requested or the request has not been executed

So, How to get rid of this error or How can I include ListItemCollectionPosition field too in my request? using ViewFields too give same error.

  • Just want to make sure that have you gone through the multiple related questions of similar issues? E.g. https://sharepoint.stackexchange.com/questions/30828/the-property-or-field-has-not-been-initialized-it-has-not-been-requested-or-the, https://sharepoint.stackexchange.com/questions/42629/the-property-or-field-has-not-been-initialized-it-has-not-been-requested-or-the & https://sharepoint.stackexchange.com/questions/130512/uncaught-error-the-property-or-field-has-not-been-initialized-i-have-tried-man – moe Jun 01 '17 at 11:57
  • Yes, @moe, I know the reason behind the error, I just want to know how can I include ListItemCollectionPosition field too, including this filed in Include also throws error – Trimantra Software Solution Jun 01 '17 at 12:15
  • @TrimantraSoftwareSolution Did you solve this issue? I'm currently facing the same problem... – dns_nx Nov 10 '20 at 13:34

2 Answers2

7

You can include it like below.

clientContext.Load(listItems,items => items.Include(item => item.Id), items => items.ListItemCollectionPosition);
Mohamed El-Qassas MVP
  • 45,382
  • 9
  • 53
  • 96
user64384
  • 71
  • 4
1

The Answer above is correct.

In case you wonder ,how to do it with more than one Property except "Id":

You don't need this if you don't specify which Properties to include in the Load, then all will be returned, but if you want to save bandwith, you might want to specify only the necessary properties.

Here my Example Code:

(Trying to fetch all Documents from a Sharepoint under "Documents" in recursive manner, with circumventing the Row Limit of 5000)

            using (var ctx = new ClientContext(GetUrl()))
            {
                var caml = new CamlQuery
                {
                    ViewXml = @"<View Scope='RecursiveAll'><RowLimit>5000</RowLimit></View>"                        
                };

                List spList = ctx.Web.Lists.GetByTitle("Documents"); 

                List<ListItem> realItems = new List<ListItem>();   

                List<string> internalNames = GetListOfInternalNames(); // Returns a List<string> of Names from the Sharepoint to include in the Load

                internalNames.Add("ContentTypeId"); //Adding manually a Name

                List<Expression<Func<ListItemCollection, object>>> allIncludes = new List<Expression<Func<ListItemCollection, object>>>();

                foreach (var c in internalNames)
                {
                    allIncludes.Add(p => p.Include(q => q[c]));
                }

                allIncludes.Add(p => p.ListItemCollectionPosition);

                do
                {
                    ListItemCollection listItemCollection = spList.GetItems(caml);
                    ctx.Load(listItemCollection,allIncludes.ToArray());
                    ctx.ExecuteQuery();

                    //Adding the current set of ListItems in our single buffer
                    realItems.AddRange(listItemCollection);
                    //Reset the current pagination info
                    caml.ListItemCollectionPosition = listItemCollection.ListItemCollectionPosition;

                } while (caml.ListItemCollectionPosition != null);
            }