Sharepoint学习笔记—ECMAScript对象模型系列-- 5、使用ECMA操作列表对象(list)
前面我们使用ECMCA script对象模型操作了Sharepoint 网站对象,这里我们来看看如何操作网站上的列表(List),操作列表List的方式与访问网站对象类似,首先使用 ClientContext构造函数并传递 URL 或 URI 以返回特定的请求上下文。然后,可以使用 Web 类的 lists 属性获取网站中列表的集合。
一、创建一个List
var currentcontext = null;
var currentweb = null;
function CreateList() {
currentcontext = new SP.ClientContext.get_current();
currentweb = currentcontext.get_web();
var ListInfo = new SP.ListCreationInformation();
var ItemInfo = new SP.ListItemCreationInformation();
ListInfo.set_title('List Created From ECMA Script');
ListInfo.set_templateType(SP.ListTemplateType.genericList);
this.createdList = currentweb.get_lists().add(ListInfo);
currentcontext.load(createdList, 'Title', 'Id');
currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccessCreateList),
Function.createDelegate(this, this.ExecuteOnFailureCreateList));
}
function ExecuteOnSuccessCreateList(sender, args) {
alert('list title:' + createdList.get_title() + '\n ID' + createdList.get_id());
}
function ExecuteOnFailureCreateList(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
![](https://images.cnblogs.com/cnblogs_com/wsdj-ittech/2012Y/3M/ECMAList1/1.jpg)
二、检索网站中所有列表的全部属性
若要返回网站的所有列表,可通过 load(clientObject) 方法加载列表集合,然后调用 executeQueryAsync(succeededCallback, failedCallback) 。以下示例显示网站的 URL 以及创建列表的日期和时间。
var siteUrl = '/';
function retrieveAllListProperties() {
var clientContext = new SP.ClientContext(siteUrl);
var oWebsite = clientContext.get_web();
this.collList = oWebsite.get_lists();
clientContext.load(collList);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededretrieveAllListProperties),
Function.createDelegate(this, this.onQueryFailedretrieveAllListProperties));
}
function onQuerySucceededretrieveAllListProperties() {
var listInfo = '';
var listEnumerator = collList.getEnumerator();
while (listEnumerator.moveNext()) {
var oList = listEnumerator.get_current();
listInfo += 'Title: ' + oList.get_title() + ' Created: ' + oList.get_created().toString() + '\n';
}
alert(listInfo);
}
function onQueryFailedretrieveAllListProperties(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
三、检索列表的指定属性
上面的示例返回网站中列表的所有属性。若要减少客户端和服务器之间不必要的数据传输,可以使用 LINQ 查询表达式指定要返回的属性。在 JavaScript 中,可以指定 Include 作为传递给 load(clientObject) 方法的查询字符串的一部分,以便指定要返回的属性。下面的示例使用该方法只返回集合中每个列表的标题和 ID ,由于在Include中只指定了Title与ID,如果尝试显示诸如ParentWebUrl 等内容,您将会收到 PropertyOrFieldNotInitializedException异常。
var siteUrlretrieveSpecificListProperties = '/';
function retrieveSpecificListProperties() {
var clientContext = new SP.ClientContext(siteUrlretrieveSpecificListProperties);
var oWebsite = clientContext.get_web();
this.collList = oWebsite.get_lists();
clientContext.load(collList, 'Include(Title, Id)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededretrieveSpecificListProperties),
Function.createDelegate(this, this.onQueryFailedretrieveSpecificListProperties));
}
function onQuerySucceededretrieveSpecificListProperties() {
var listInfo = '';
var listEnumerator = collList.getEnumerator();
while (listEnumerator.moveNext()) {
var oList = listEnumerator.get_current();
listInfo += 'Title: ' + oList.get_title() + ' ID: ' + oList.get_id().toString() + '\n';
}
alert(listInfo);
}
function onQueryFailedretrieveSpecificListProperties(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
四、在集合中存储检索到的列表
我们还可以使用 loadQuery(clientObjectCollection, exp)方法而不是像上面使用的load(clientObject)方法。loadQuery方法用来在另一集合中存储返回值,而上面使用的Load方法则是将返回值存储在 lists属性中。但是LoadQuery方法不支持托管对象模型使用的 LINQ 方法和操作。
var siteUrlToCollection = '/';
function retrieveSpecificListPropertiesToCollection() {
var clientContext = new SP.ClientContext(siteUrlToCollection);
var oWebsite = clientContext.get_web();
var collList = oWebsite.get_lists();
this.listInfoCollection = clientContext.loadQuery(collList, 'Include(Title, Id)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededretrieveSpecificListPropertiesToCollection), Function.createDelegate(this, this.onQueryFailedretrieveSpecificListPropertiesToCollection));
}
function onQuerySucceededretrieveSpecificListPropertiesToCollection() {
var listInfo = '';
for (var i = 0; i < this.listInfoCollection.length; i++) {
var oList = this.listInfoCollection[i];
listInfo += 'Title: ' + oList.get_title() + ' ID: ' + oList.get_id().toString();
}
alert(listInfo.toString());
}
function onQueryFailedretrieveSpecificListPropertiesToCollection(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
五、检索网站中的列表字段
我们可以将 Include 语句嵌套在 JavaScript 查询中,以返回列表及其字段的元数据。该示例返回网站中所有列表的所有字段,并显示其内部名称包含字符串“name”的所有字段的标题和内部名称。
var siteUrlListsAllFields = '/';
function retrieveAllListsAllFields() {
var clientContext = new SP.ClientContext(siteUrlListsAllFields);
var oWebsite = clientContext.get_web();
var rootWebsite = clientContext.get_site().get_rootWeb();
var collList = oWebsite.get_lists();
this.listInfoArray = clientContext.loadQuery(collList,
'Include(Title,Fields.Include(Title,InternalName))');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededretrieveAllListsAllFields),
Function.createDelegate(this, this._onQueryFailedretrieveAllListsAllFields));
}
function onQuerySucceededretrieveAllListsAllFields() {
var listInfo = '';
alert("OK");
for (var i = 0; i < this.listInfoArray.length; i++) {
var oList = this.listInfoArray[i];
var collField = oList.get_fields();
var fieldEnumerator = collField.getEnumerator();
while (fieldEnumerator.moveNext()) {
var oField = fieldEnumerator.get_current();
var regEx = new RegExp('name', 'ig');
if (regEx.test(oField.get_internalName())) {
listInfo += '\nList: ' + oList.get_title() +
'\n\tField Title: ' + oField.get_title() +
'\n\tField Name: ' + oField.get_internalName();
}
}
}
alert(listInfo);
}
function _onQueryFailedretrieveAllListsAllFields(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
五、检索列表的ContentType
我们可以使用如下代码来获取列表List的ContentTypes
var contentTypeCollection;
var listCollection;
var list;
function getListContentTypes() {
var clientContext = new SP.ClientContext.get_current();
if (clientContext != undefined && clientContext != null) {
var web = clientContext.get_web();
this.listCollection = web.get_lists();
this.list = listCollection.getByTitle("MyTestList");
this.contentTypeCollection = list.get_contentTypes();
clientContext.load(this.contentTypeCollection);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededListContentTypes), Function.createDelegate(this, this.onQueryFailedListContentTypes));
}
}
function onQuerySucceededListContentTypes() {
var contentType = 'List Content Types:\n '
var contentTypeEnumerator = this.contentTypeCollection.getEnumerator();
while (contentTypeEnumerator.moveNext()) {
var content = contentTypeEnumerator.get_current();
contentType += content.get_name() + '\n';
}
alert(contentType);
}
function onQueryFailedListContentTypes(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
</script>