Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Batch operations using CSOM in a Sharepoint 2013 hosted app

$
0
0

Hi All,

I have a SharePoint 2013 hosted app and use CSOM queries to create multiple list items. After executing my query usingclientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed); I want to retrieve the ID's of the newly created items without additional queries. I am assuming they are already in theargs parameter of onQuerySucceeded(sender, args) function. Am I in the right direction and if I am how do I retrieve all of the ID's of the newly created items? Here is the core of my code:

function createListItems(selectedItems, Username) { var itemArray = []; var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl")); var appUrl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl")); var oWebsite; var oList; var numItems = selectedPMs.length; var clientContext = new SP.ClientContext(appUrl); var appContextSite = new SP.AppContextSite(clientContext, hostUrl); oWebsite = appContextSite.get_web(); oList = oWebsite.get_lists().getByTitle("MyList"); for (var i = 0; i < numItems; i++) { // getItem is my own function var orgData = getItem(selectedItems[i]); var itemCreateInfo = new SP.ListItemCreationInformation(); var oListItem = oList.addItem(itemCreateInfo); oListItem.set_item('CustomerID', Number(orgData.CustomerID)); oListItem.set_item('CustlocID', Number(orgData.CustomerLocationID)); oListItem.set_item('Description', orgData.Description); oListItem.set_item('EnteredBy', Username); oListItem.update(); itemArray[i] = oListItem; clientContext.load(itemArray[i]); } clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed); function onQuerySucceeded(sender, args) {

// I am assuming there is some returned data in args, if there is // I want to be able to retrieve and use the ID's of the // newly created items after extracting them from args } function onQueryFailed(sender, args) { alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); } }

There may be other ways to do it, I am not insisting in the args parameter. The most important task here is to avoid extra calls to server because I may be processing a batch of 100 items so I don't want to have to make a server call to read one item's ID.

Thanks in advance!


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>