如何使用 CAML 修复“当前用户的过滤器查找”
How to fix ' Filter Lookup by Current User using CAML
我正在筛选 SharePoint Online 上的查找列,但如果我检查浏览器并且筛选不起作用,我会不断收到错误消息。
我已使用 jquery.SPServices.SPGetCurrentUser 获取登录用户的详细信息。然后 CAML 查询过滤并获取列表 Activity 计划。
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script
src="//cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices-2014.02.min.js"></script>
<script type="text/javascript">
let custom = {}
custom.myQuery = jQuery;
console.log(custom)
console.log($().SPServices.SPGetCurrentUser({
fieldNames: ["ID", "Name", "Title", "Email","DepartMent", "JobTitle",
"FirstName", "LastName", "UserName", "SIP Address"],
debug: false
}));
var userId = $().SPServices.SPGetCurrentUser({
fieldName: "ID",
debug: false
})
_spBodyOnLoadFunctionNames.push("loadSP")
function loadSP() {
var query = `<Query><Where><And><Eq><FieldRef Name='AssignedTo'
LookupId='TRUE'/><Value Type='Integer'>${userId}</Value></Eq><Eq>
<FieldRef Name='Status' LookupId='TRUE'/><Value
Type='Text'>Approved</Value></Eq></And></Where></Query>`;
custom.myQuery().SPServices({
operation: "GetListItems",
async: false,
listName: "Activity Plan",
CAMLQuery: query,
//CAMLQueryOptions: "<QueryOptions><ViewAttributes
Scope='RecursiveAll' IncludeRootFolder='True' /></QueryOptions>",
CAMLViewFields: `<ViewFields>
<FieldRef Name='Deliverable' />
<FieldRef Name='Task Status' />
<FieldRef Name='ID' />
</ViewFields>`,
completefunc: function (xData, Status) {
var newSites = "(none)";
var ID = "0";
console.log(xData)
$(xData.responseXML).find("z\:row, row").each(function () {
var DeliverableLookup = $(this).attr("Deliverable");
var owId = $(this).attr("ID");
if (DeliverableLookup != '') {
var newValue = DeliverableLookup;
if (newSites.indexOf(newValue) == -1) {
newSites = newSites + "|" + newValue;
ID += '| ' + owId;
}
}
});
updateValues(newSites, ID);
}
});
}
function getField(fieldType,fieldTitle) {
var docTags = document.getElementsByTagName(fieldType);
for (var i=0; i < docTags.length; i++) {
if (docTags[i].title == fieldTitle) {
return docTags[i];
}
}
return false;
}
function updateValues(newSites, ID) {
lookupFieldText = getField('select','My Planned Tasks');
//lookupFieldId = document.getElementById(lookupFieldText.id);
$(lookupFieldText).empty();
let arrSites = newSites.split('|');
let arrId = ID.split('|');
for (let i = 0; i < arrSites.length; i++){
$(lookupFieldText).append(`<option id='${arrId[i]}'
value='${arrSites[i]}'> ${arrSites[i]} </option>`)
}
//lookupFieldText.choices = newSites;
}
function CustomAction(){
console.log("done loading")
}
</script>
我希望查找列“我的计划任务”仅显示我创建且状态为“已批准”的可交付成果,但它返回了所有任务。
检查您的控制台记录的数据并查看返回的内容,如果没有返回任何内容,请检查您的状态是否为真,如果不是则您的查询是错误的。检查查询中的空格。
我正在筛选 SharePoint Online 上的查找列,但如果我检查浏览器并且筛选不起作用,我会不断收到错误消息。
我已使用 jquery.SPServices.SPGetCurrentUser 获取登录用户的详细信息。然后 CAML 查询过滤并获取列表 Activity 计划。
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script
src="//cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices-2014.02.min.js"></script>
<script type="text/javascript">
let custom = {}
custom.myQuery = jQuery;
console.log(custom)
console.log($().SPServices.SPGetCurrentUser({
fieldNames: ["ID", "Name", "Title", "Email","DepartMent", "JobTitle",
"FirstName", "LastName", "UserName", "SIP Address"],
debug: false
}));
var userId = $().SPServices.SPGetCurrentUser({
fieldName: "ID",
debug: false
})
_spBodyOnLoadFunctionNames.push("loadSP")
function loadSP() {
var query = `<Query><Where><And><Eq><FieldRef Name='AssignedTo'
LookupId='TRUE'/><Value Type='Integer'>${userId}</Value></Eq><Eq>
<FieldRef Name='Status' LookupId='TRUE'/><Value
Type='Text'>Approved</Value></Eq></And></Where></Query>`;
custom.myQuery().SPServices({
operation: "GetListItems",
async: false,
listName: "Activity Plan",
CAMLQuery: query,
//CAMLQueryOptions: "<QueryOptions><ViewAttributes
Scope='RecursiveAll' IncludeRootFolder='True' /></QueryOptions>",
CAMLViewFields: `<ViewFields>
<FieldRef Name='Deliverable' />
<FieldRef Name='Task Status' />
<FieldRef Name='ID' />
</ViewFields>`,
completefunc: function (xData, Status) {
var newSites = "(none)";
var ID = "0";
console.log(xData)
$(xData.responseXML).find("z\:row, row").each(function () {
var DeliverableLookup = $(this).attr("Deliverable");
var owId = $(this).attr("ID");
if (DeliverableLookup != '') {
var newValue = DeliverableLookup;
if (newSites.indexOf(newValue) == -1) {
newSites = newSites + "|" + newValue;
ID += '| ' + owId;
}
}
});
updateValues(newSites, ID);
}
});
}
function getField(fieldType,fieldTitle) {
var docTags = document.getElementsByTagName(fieldType);
for (var i=0; i < docTags.length; i++) {
if (docTags[i].title == fieldTitle) {
return docTags[i];
}
}
return false;
}
function updateValues(newSites, ID) {
lookupFieldText = getField('select','My Planned Tasks');
//lookupFieldId = document.getElementById(lookupFieldText.id);
$(lookupFieldText).empty();
let arrSites = newSites.split('|');
let arrId = ID.split('|');
for (let i = 0; i < arrSites.length; i++){
$(lookupFieldText).append(`<option id='${arrId[i]}'
value='${arrSites[i]}'> ${arrSites[i]} </option>`)
}
//lookupFieldText.choices = newSites;
}
function CustomAction(){
console.log("done loading")
}
</script>
我希望查找列“我的计划任务”仅显示我创建且状态为“已批准”的可交付成果,但它返回了所有任务。
检查您的控制台记录的数据并查看返回的内容,如果没有返回任何内容,请检查您的状态是否为真,如果不是则您的查询是错误的。检查查询中的空格。