3

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.

1

You can include it like below.

clientContext.Load(listItems,items => items.Include(item => item.Id), items => items.ListItemCollectionPosition);
0

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);
            }

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.