Last active
August 29, 2015 14:21
-
-
Save DWboutin/7ec04dd7896c6d44a175 to your computer and use it in GitHub Desktop.
Sharepoint 2010 Javascript CSOM
This file contains hidden or 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
/* Usage http://allthatjs.com/2012/04/03/using-sharepoint-csom-in-html5-apps/ | |
* | |
* | |
var SPQH = new SPQueryHandler('/intranet/ei/docei/conceptetfonctionnement'); | |
SPQH.doWhenReady(function(){ | |
// set operations here | |
SPQH.getPages(); | |
}); | |
* | |
*/ | |
var SPQueryHandler= function(url){ | |
var that = this; | |
this.siteUrl = url; | |
this.clientContext; | |
this.isReady = false; | |
this.fnStack = new Array(); | |
var interval = setInterval(function(){ | |
if(SP !== undefined){ | |
clearInterval(interval); | |
that.init(); | |
} | |
},20); | |
}; | |
SPQueryHandler.prototype.init = function(){ | |
var that = this; | |
var interval = setInterval(function(){ | |
if(SP.ClientContext !== undefined){ | |
clearInterval(interval); | |
that.clientContext = new SP.ClientContext(that.siteUrl); | |
that.clientContext.add_requestFailed(function (sender, args) { | |
alert('Request failed: ' + args.get_message()); | |
}); | |
that.isReady = true; | |
if(that.whenReady !== undefined){ | |
that.whenReady(); | |
} | |
} | |
}); | |
}; | |
SPQueryHandler.prototype.doWhenReady = function(fn){ | |
if(typeof fn == 'function'){ | |
this.whenReady = fn; | |
} | |
}; | |
// http://allthatjs.com/2012/04/03/using-sharepoint-csom-in-html5-apps/ | |
SPQueryHandler.prototype.getDocuments = function(){ | |
var that = this; | |
var web = that.clientContext.get_web(); | |
var list = web.get_lists().getByTitle('Documents'); | |
var items = list.getItems(''); | |
var itemsArray = []; | |
that.clientContext.load(items); | |
that.clientContext.executeQueryAsync(function () { | |
var listEnumerator = items.getEnumerator(); | |
while (listEnumerator.moveNext()) { | |
var item = listEnumerator.get_current(); | |
itemsArray.push(item.get_fieldValues()); | |
console.log(item.get_fieldValues()); | |
} | |
return itemsArray; | |
}); | |
}; | |
SPQueryHandler.prototype.getPages = function(){ | |
var that = this; | |
var web = that.clientContext.get_web(); | |
var list = web.get_lists().getByTitle('Pages'); | |
var items = list.getItems(SP.CamlQuery.createAllItemsQuery()); | |
that.clientContext.load(items); | |
that.clientContext.add_requestSucceeded(function(){ | |
var itemsValues = items.get_data().map(function(item){ | |
return item.get_fieldValues() | |
}); | |
console.log(itemsValues); //result | |
}); | |
that.clientContext.executeQueryAsync(); | |
}; | |
return SPQueryHandler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment