Manage List sp.js

//récupère ma liste des éléments d'une liste dans un dossier
Utilities.GetListItemsByFolder = function (webUrl, ListTitle, query, folderName, success, error) {

var clientContext = new SP.ClientContext(webUrl);
 


var oList = clientContext.get_web().get_lists().getByTitle(ListTitle);
 var rootFolder = oList.get_rootFolder();
 clientContext.load(rootFolder); //charger le root folder
 clientContext.executeQueryAsync(function (sender, args) {

var camlQuery = new SP.CamlQuery();
 camlQuery.set_viewXml('<View>' + query + '</View>');

if (folderName != null) {
 camlQuery.set_folderServerRelativeUrl('Lists/' + rootFolder.get_serverRelativeUrl().split('/').pop() + '/' + folderName);
 }
 var collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);
 clientContext.executeQueryAsync(function (sender, args) {

var listItemEnumerator = collListItem.getEnumerator();
 success(listItemEnumerator);

}, error);
 }, error);

}


//récupère ma liste des éléments d'une liste dans un dossier
Utilities.GetListFilesByFolder = function (webUrl, ListTitle, query, folderName, success, error) {
 var clientContext = new SP.ClientContext(webUrl);
 var oList = clientContext.get_web().get_lists().getByTitle(ListTitle);
 var rootFolder = oList.get_rootFolder();
 clientContext.load(rootFolder); //charger le root folder
 clientContext.executeQueryAsync(function (sender, args) {

var camlQuery = new SP.CamlQuery();
 camlQuery.set_viewXml('<View>' + query + '</View>');

if (folderName != null) {
 camlQuery.set_folderServerRelativeUrl(rootFolder.get_serverRelativeUrl().split('/').pop() + '/' + folderName);
 }
 var collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);
 clientContext.executeQueryAsync(function (sender, args) {

var listItemEnumerator = collListItem.getEnumerator();
 success(listItemEnumerator);

}, error);
 }, error);

}


//récupère ma liste des éléments d'une liste
Utilities.GetListItems = function (webUrl, ListTitle, query, success, error) {
 var clientContext = new SP.ClientContext(webUrl);
 var oList = clientContext.get_web().get_lists().getByTitle(ListTitle);

var camlQuery = new SP.CamlQuery();
 camlQuery.set_viewXml('<View>' + query + '</View>');
 var collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);
 clientContext.executeQueryAsync(function (sender, args) {

var listItemEnumerator = collListItem.getEnumerator();
 success(listItemEnumerator);

}, error);

}

//récupère un item BYID
Utilities.GetListItemById = function (webUrl, ListTitle, itemId, success, error) {
 var clientContext = new SP.ClientContext(webUrl);
 var oList = clientContext.get_web().get_lists().getByTitle(ListTitle);

var listItem = oList.getItemById(itemId);

clientContext.load(listItem);
 clientContext.executeQueryAsync(function (sender, args) {
 success(listItem);

}, error);

}

//edit liste Item [void]
//properties sous forme de [{fieldName:'InternaleName',fieldValue:'value'}]
Utilities.EditListItemById = function (webUrl, ListTitle, itemId, properties, success, error) {
 var clientContext = new SP.ClientContext(webUrl);
 var oList = clientContext.get_web().get_lists().getByTitle(ListTitle);

var oListItem = oList.getItemById(itemId);

$(properties).each(function (i) {
 oListItem.set_item(properties[i].fieldName, properties[i].fieldValue);
 });


 oListItem.update();

clientContext.executeQueryAsync(success, error);
}

// Add list Item [void]
Utilities.AddListItem = function (webUrl, ListTitle, properties, success, error) {
 var clientContext = new SP.ClientContext(webUrl);
 var oList = clientContext.get_web().get_lists().getByTitle(ListTitle);


 var itemCreateInfo = new SP.ListItemCreationInformation();
 var oListItem = oList.addItem(itemCreateInfo);

$(properties).each(function (i) {
 oListItem.set_item(properties[i].fieldName, properties[i].fieldValue);
 });

oListItem.update();

clientContext.executeQueryAsync(success, error);
}
Advertisements
This entry was posted in JSOM, sharepoint online. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s