SharePoint ListItemPosition - JackHu88/Comm GitHub Wiki

JSOM ListItemPosition

https://www.vrdmn.com/2013/07/batch-operations-using-javascript.html

https://www.vrdmn.com/2012/08/paging-in-sharepoint-javascript-client.html

https://techcommunity.microsoft.com/t5/SharePoint-Developer/Batch-Update-list-items-In-SharePoint-List-Using-JSOM-CSOM/m-p/269133

https://code.msdn.microsoft.com/sharepoint/SharePoint-JSOM-list-5104ca92#content

https://www.sptrenches.com/2016/06/get-all-items-in-5000-large-list-with.html

var listItems;                   // The list of retrieved items.
var query;                       // For paging, reuse the same query object.
var targetList;                  // The list from which to retrieve items.
var clientContext;
var arrayItem=[];

function runCode() {
	arrayItem=[];

    clientContext = new SP.ClientContext();
    targetList = clientContext.get_web().get_lists().getByTitle('List');
    query = new SP.CamlQuery();
    //Specifying the RowLimit will determine how many items will be fetched in one call to the server.
    query.set_viewXml("<View><ViewFields><FieldRef Name='Title'/></ViewFields><RowLimit>1000</RowLimit></View>");
    listItems = targetList.getItems(query);
    clientContext.load(listItems);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
    var message = "Titles, two at a time:\n";
    var listEnumerator = listItems.getEnumerator();
    while (listEnumerator.moveNext()) {
		 var item =listEnumerator.get_current();
		arrayItem.push(item);
        //message += "\nTitle=" + listEnumerator.get_current().get_item("Title")
    }
    //alert(message);

    var position = listItems.get_listItemCollectionPosition();

    if (position != null) {
        query.set_listItemCollectionPosition(position);
        listItems = targetList.getItems(query);
        clientContext.load(listItems);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }else{
		console.log(arrayItem);
	}

}

function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}

CSOM Query ListItems in Batches

https://piyushksingh.com/2016/12/04/query-listitems-in-batches-sharepoint-online/

static void AirlineAssign(ClientContext ctx)
        {
            ListItemCollectionPosition itemPosition = null;
            List taxReportList = ctx.Web.Lists.GetByTitle("TaxReport");

            while (true)
            {
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<View><Query><Where>"
                    + "<And>"
                    + "<Eq><FieldRef Name='Status'/><Value Type='Text'>已签收</Value></Eq>"
                    + "<Eq><FieldRef Name='Expense_x0020_Type'/><Value Type='Text'>Airline Fees - Employee</Value></Eq>"
                    + "</And>"
                    + "</Where></Query><RowLimit>100</RowLimit></View>";
                camlQuery.ListItemCollectionPosition = itemPosition;
                ListItemCollection items = taxReportList.GetItems(camlQuery);

                FieldUserValue user = new FieldUserValue();
                user.LookupId = 2019;    

                try { 
                    ctx.Load(items);
                    ctx.ExecuteQuery();
                    itemPosition = items.ListItemCollectionPosition;
                    foreach (var item in items)
                    {
                    
                        item["T_x002f_A_x0020_Name"] = user;
                        item.Update();
                    }
                    ctx.ExecuteQuery();
                    if (itemPosition == null)
                    {
                        break;
                    }
                }catch(Exception ex){
                    Console.WriteLine(ex.Message);
                }
            }
        }
⚠️ **GitHub.com Fallback** ⚠️