我在尝试由用户获取列表过滤器上的值时遇到了一些问题。现在我得到一个view.getItems
不是函数”,当我将它们更改为List.getItems('');
时,它可以工作,但不能过滤。
function sharePointReady() {
var targetClientContext;
var List;
var view;
targetClientContext = new SP.ClientContext.get_current();
List = targetClientContext.get_web().get_lists().getByTitle('My Recognitions');
view = List.get_views().getByTitle('CurrentUser');
this.collList = view.getItems("");
//Get the list view and load it to client context and execute the batch
targetClientContext.load(collList);
targetClientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
}
var value = 0;
var counter = 0;
for (var i = 0; i < 1; i++) {
function onRequestSucceeded() {
var listItemEnum = collList.getEnumerator();
while (listItemEnum.moveNext()) {
view = listItemEnum.get_current();
console.log(view.get_item('ID'));
console.log('Load ' + ++counter);
value = view.get_item('Value');
console.log(value);
}
console.log('');
console.log('Tu puntos son: ' + counter);
document.getElementById("try").innerHTML = counter + ' Points';
}
}
function onRequestFailed(sender, args) {
alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
}
预先感谢
答案 0 :(得分:1)
要获得基于现有视图的列表项的视图,可以通过view.get_viewQuery()
方法获取视图的基础CAML查询XML。
然后,您可以将该字符串输入新的SP.CamlQuery
对象的View XML中,然后将其传递给list.getItems()
以获取结果。
var clientContext = new SP.ClientContext();
var list = clientContext.get_web().get_lists().getByTitle("My Recognitions");
var view = list.get_views().getByTitle("CurrentUser");
clientContext.load(view);
clientContext.executeQueryAsync(function(){
var query = view.get_viewQuery();
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(query);
var items = list.getItems(camlQuery);
clientContext.load(items);
clientContext.executeQueryAsync(
function(){
var itemEnumerator = items.getEnumerator();
var counter = 0;
while(itemEnumerator.moveNext()){
var item = itemEnumerator.get_current();
console.log(item.get_item("ID"));
console.log('Load ' + ++counter);
console.log(item.get_item("Value"));
}
console.log('Tu puntos son: ' + counter);
document.getElementById("try").innerHTML = counter + ' Points';
},
onRequestFailed
);
},onRequestFailed);
function onRequestFailed(sender, args) {
alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
}
请注意,这需要两个异步调用:一个获取视图XML,另一个实际获取项。
答案 1 :(得分:1)
以下代码供您参考。
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(sharePointReady, "sp.js");
function sharePointReady(){
getItemsFromView("My Recognitions", "CurrentUser",
function(items){
for(var i = 0; i < items.get_count(); i++){
var item = items.get_item(i);
console.log(item.get_item('Title'));
}
},
function(sender,args){
console.log(args.get_message())
}
);
}
function getItemsFromView(listTitle, viewTitle,success,error){
var ctx = new SP.ClientContext();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var view = list.get_views().getByTitle(viewTitle);
ctx.load(view,'ViewQuery');
ctx.executeQueryAsync(
function() {
var viewQry = "<View><Query>" + view.get_viewQuery() + "</Query></View>";
getItems(listTitle,viewQry,success,error);
},
error);
}
function getItems(listTitle, queryText,success,error) {
var ctx = new SP.ClientContext();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var query = new SP.CamlQuery();
query.set_viewXml(queryText);
var items = list.getItems(query);
ctx.load(items);
ctx.executeQueryAsync(
function() {
success(items);
},
error
);
}
</script>
在SharePoint 2013中,如果您只想获取当前用户创建的列表项,则可以将REST API与$ filter配合使用来实现。
<script src="http://code.jquery.com/jquery-1.11.3.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var siteURL = _spPageContextInfo.webAbsoluteUrl;
var listname = "My Recognitions";
var currentUserId=_spPageContextInfo.userId
var url = siteURL + "/_api/web/lists/getbytitle('" + listname + "')/items?$filter=Author/Id eq "+currentUserId;
$.ajax({
url: url,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
var items = data.d.results;
$.each(items,function(index,item){
console.log("Title:"+item.Title);
});
},
error: function (error) {
console.log(JSON.stringify(error));
}
});
});
</script>