1) Batch Add Items to List:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createListItems() { | |
var itemArray = []; | |
var clientContext = SP.ClientContext.get_current(); | |
var oList = clientContext.get_web().get_lists().getByTitle('TestList'); | |
for(var i = 0; i< 5; i++){ | |
var itemCreateInfo = new SP.ListItemCreationInformation(); | |
var oListItem = oList.addItem(itemCreateInfo); | |
oListItem.set_item('Title', 'My New Item!' + i); | |
oListItem.update(); | |
itemArray[i] = oListItem; | |
clientContext.load(itemArray[i]); | |
} | |
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed); | |
} | |
function onQuerySucceeded() { | |
alert('Items created'); | |
} | |
function onQueryFailed(sender, args) { | |
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); | |
} | |
createListItems(); |
2) Batch Update Items from List:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function updateListItems() { | |
var itemArray = []; | |
var clientContext = SP.ClientContext.get_current(); | |
var oList = clientContext.get_web().get_lists().getByTitle('TestList'); | |
for(var i = 1; i<= 5; i++){ | |
var oListItem = oList.getItemById(i); | |
oListItem.set_item('Title', 'My Updated Item!' + i); | |
oListItem.update(); | |
itemArray[i] = oListItem; | |
clientContext.load(itemArray[i]); | |
} | |
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed); | |
} | |
function onQuerySucceeded() { | |
alert('Items Updated'); | |
} | |
function onQueryFailed(sender, args) { | |
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); | |
} | |
updateListItems(); |
3) Batch Delete Items from List:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deleteListItems() { | |
var clientContext = SP.ClientContext.get_current(); | |
var oList = clientContext.get_web().get_lists().getByTitle('TestList'); | |
for(var i = 0; i<= 5; i++){ | |
var oListItem = oList.getItemById(i); | |
oListItem.deleteObject(); | |
} | |
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed); | |
} | |
function onQuerySucceeded() { | |
alert('Items Deleted'); | |
} | |
function onQueryFailed(sender, args) { | |
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); | |
} | |
deleteListItems(); |
Now as you can see, only one call is made from the client to the server. It contains all the XML necessary for creating the 5 items. Here is the relevant XML:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="Javascript Library"> | |
<Actions> | |
<ObjectPath Id="41" ObjectPathId="40"></ObjectPath> | |
<Method Name="SetFieldValue" Id="42" ObjectPathId="40"> | |
<Parameters> | |
<Parameter Type="String">Title</Parameter> | |
<Parameter Type="String">My New Item!0</Parameter> | |
</Parameters> | |
</Method> | |
<Method Name="Update" Id="43" ObjectPathId="40"></Method> | |
<Query Id="44" ObjectPathId="40"> | |
<Query SelectAllProperties="true"> | |
<Properties> | |
<Property Name="FileSystemObjectType" ScalarProperty="true"></Property> | |
<Property Name="Id" ScalarProperty="true"></Property> | |
<Property Name="Title" ScalarProperty="true"></Property> | |
</Properties> | |
</Query> | |
</Query> | |
<ObjectPath Id="46" ObjectPathId="45"></ObjectPath> | |
<Method Name="SetFieldValue" Id="47" ObjectPathId="45"> | |
<Parameters> | |
<Parameter Type="String">Title</Parameter> | |
<Parameter Type="String">My New Item!1</Parameter> | |
</Parameters> | |
</Method> | |
<Method Name="Update" Id="48" ObjectPathId="45"></Method> | |
<Query Id="49" ObjectPathId="45"> | |
<Query SelectAllProperties="true"> | |
<Properties> | |
<Property Name="FileSystemObjectType" ScalarProperty="true"></Property> | |
<Property Name="Id" ScalarProperty="true"></Property> | |
<Property Name="Title" ScalarProperty="true"></Property> | |
</Properties> | |
</Query> | |
</Query> | |
<ObjectPath Id="51" ObjectPathId="50"></ObjectPath> | |
<Method Name="SetFieldValue" Id="52" ObjectPathId="50"> | |
<Parameters> | |
<Parameter Type="String">Title</Parameter> | |
<Parameter Type="String">My New Item!2</Parameter> | |
</Parameters> | |
</Method> | |
<Method Name="Update" Id="53" ObjectPathId="50"></Method> | |
<Query Id="54" ObjectPathId="50"> | |
<Query SelectAllProperties="true"> | |
<Properties> | |
<Property Name="FileSystemObjectType" ScalarProperty="true"></Property> | |
<Property Name="Id" ScalarProperty="true"></Property> | |
<Property Name="Title" ScalarProperty="true"></Property> | |
</Properties> | |
</Query> | |
</Query> | |
<ObjectPath Id="56" ObjectPathId="55"></ObjectPath> | |
<Method Name="SetFieldValue" Id="57" ObjectPathId="55"> | |
<Parameters> | |
<Parameter Type="String">Title</Parameter> | |
<Parameter Type="String">My New Item!3</Parameter> | |
</Parameters> | |
</Method> | |
<Method Name="Update" Id="58" ObjectPathId="55"></Method> | |
<Query Id="59" ObjectPathId="55"> | |
<Query SelectAllProperties="true"> | |
<Properties> | |
<Property Name="FileSystemObjectType" ScalarProperty="true"></Property> | |
<Property Name="Id" ScalarProperty="true"></Property> | |
<Property Name="Title" ScalarProperty="true"></Property> | |
</Properties> | |
</Query> | |
</Query> | |
<ObjectPath Id="61" ObjectPathId="60"></ObjectPath> | |
<Method Name="SetFieldValue" Id="62" ObjectPathId="60"> | |
<Parameters> | |
<Parameter Type="String">Title</Parameter> | |
<Parameter Type="String">My New Item!4</Parameter> | |
</Parameters> | |
</Method> | |
<Method Name="Update" Id="63" ObjectPathId="60"></Method> | |
<Query Id="64" ObjectPathId="60"> | |
<Query SelectAllProperties="true"> | |
<Properties> | |
<Property Name="FileSystemObjectType" ScalarProperty="true"></Property> | |
<Property Name="Id" ScalarProperty="true"></Property> | |
<Property Name="Title" ScalarProperty="true"></Property> | |
</Properties> | |
</Query> | |
</Query> | |
</Actions> | |
<ObjectPaths> | |
<Method Id="40" ParentId="6" Name="AddItem"> | |
<Parameters> | |
<Parameter TypeId="{54cdbee5-0897-44ac-829f-411557fa11be}"> | |
<Property Name="FolderUrl" Type="Null"></Property> | |
<Property Name="LeafName" Type="Null"></Property> | |
<Property Name="UnderlyingObjectType" Type="Number">0</Property> | |
</Parameter> | |
</Parameters> | |
</Method> | |
<Method Id="45" ParentId="6" Name="AddItem"> | |
<Parameters> | |
<Parameter TypeId="{54cdbee5-0897-44ac-829f-411557fa11be}"> | |
<Property Name="FolderUrl" Type="Null"></Property> | |
<Property Name="LeafName" Type="Null"></Property> | |
<Property Name="UnderlyingObjectType" Type="Number">0</Property> | |
</Parameter> | |
</Parameters> | |
</Method> | |
<Method Id="50" ParentId="6" Name="AddItem"> | |
<Parameters> | |
<Parameter TypeId="{54cdbee5-0897-44ac-829f-411557fa11be}"> | |
<Property Name="FolderUrl" Type="Null"></Property> | |
<Property Name="LeafName" Type="Null"></Property> | |
<Property Name="UnderlyingObjectType" Type="Number">0</Property> | |
</Parameter> | |
</Parameters> | |
</Method> | |
<Method Id="55" ParentId="6" Name="AddItem"> | |
<Parameters> | |
<Parameter TypeId="{54cdbee5-0897-44ac-829f-411557fa11be}"> | |
<Property Name="FolderUrl" Type="Null"></Property> | |
<Property Name="LeafName" Type="Null"></Property> | |
<Property Name="UnderlyingObjectType" Type="Number">0</Property> | |
</Parameter> | |
</Parameters> | |
</Method> | |
<Method Id="60" ParentId="6" Name="AddItem"> | |
<Parameters> | |
<Parameter TypeId="{54cdbee5-0897-44ac-829f-411557fa11be}"> | |
<Property Name="FolderUrl" Type="Null"></Property> | |
<Property Name="LeafName" Type="Null"></Property> | |
<Property Name="UnderlyingObjectType" Type="Number">0</Property> | |
</Parameter> | |
</Parameters> | |
</Method> | |
<Identity Id="6" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:5b2a524b-edc8-4da9-8904-4d28943abb39:web:26fc0fbf-d565-4c81-a9f5-ac858f67255a:list:a1e33484-c701-484e-8023-3ad360c23022"></Identity> | |
</ObjectPaths> | |
</Request> |
Hope you enjoyed this blog post. Happy SharePointing!
Addendum: I would also like to point out one of the hidden gems of the SharePoint world: if you are using Server Side Code (Full Trust), there is a similar batch method available called SPWeb.ProcessBatchData which can be used to bulk add, edit and delete list items. This is really helpful because it can operate on large number of items at once without making repeated calls to the database.
16 comments:
Good one ...Liked it... REST API is very powerful. And at least you have not used spservices to do the same.
Awsome post. Like the idea of optimizing calls for server from client infrastructure to perform batch operation.
NIce and useful article
If you have multiple columns, should they all be on the same row within the oListItem.set_Item() line or separate oListItem.set_Item lines?
I would go for multiple oListItem.set_Item lines.
Hello,
Can we do this using C# (using Microsoft.SharePoint.dll) ?
Hello
Can we do this using C#(Client object model)?
Thank you Vardhaman. Small doubt.
What if you dont have ID's and If you have to iterate ListItemCollection that you got from a CAML Query to update?
So far to iterate such collection, i have used async's success method. But now, after i get the collection and in iteration, i have to update each item. In this case, do i need to call one more async's success method??
please clarify my doubt.
ItemArray is not required in say, creating list items... clientContext.load(oListItem); should be sufficient since you are loading it in loop..it works without the array. Or may be you have used it for a specific reason which I have not understood ? :)
is there a way to track this batch process? e.g. if I send 50 items batch to delete those items, server takes its own time to delete these 50 items and only then we get to know that its done.
I would really like to have counter shown in console.log, when it deletes item, so that end user wont live status.
I tried stupid solution of adding, console.log inside for Loop, but figured out its Async call. Might not be possible at all unless there is inbuilt method to track.
But your insights will be great, oe how you would do it? :).. thank you!
Thanks for the great article, can we do the same for getItems(camlQuery) from different lists? And in OnSuccess() how to handle different listItemCollections?
Something like below,
for (var i = 0; i < listCollection.length; i++) {
var list = list + i;
list = hostContext.get_web().get_lists().getByTitle(listCollection[i]);
listItems = list.getItems(camlQuery);
clientContext.load(listItems);//not sure how this will work
}
clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
Any recommendations on how to retrieve all document libraries from multiple sites within the same collection?
@Kessy,
Could you not do it using search?
What is the maximum size of the request that is allowed for batch operations?
How to check if data exist in list before inserting bulk data
Hi , can you please help, how to save date from datepicker in SharePoint list, date is getting saved but one day earlier.
Post a Comment