我的问题如下:
我在网站集根目录下有一个列表,代表名为"项目注册"的项目。该列表中的每一项都代表一个项目,我将在其中存储项目名称、状态、URL和其他一些属性。之所以存储URL,是因为每个项目都将在同一网站集下具有相应的SharePoint子网站,该网站集是通过在Visual Studio中创建的自定义事件接收器动态创建的。
每个项目子站点都有一个名为"成员"的列表,用于跟踪分配给该项目的人员。这是一项业务需求,此列表用于本问题范围之外的其他目的。
现在,我的挑战是,我需要在另一个名为"时间表"的列表的下拉列表中显示日志用户被分配到的项目。这是为了让用户能够输入在这些项目中花费的时间。此时间表列表只是一个开箱即用的SharePoint日历列表,其中包含一些自定义字段。
以下是我正在使用的代码,但在检索成员列表时,它一直给我以下错误。
未捕获的类型错误:无法调用未定义的方法"apply"
有人能给我指正确的方向吗?
我使用的代码在一个自定义.js文件中,我在主页中正确引用了这个文件:
$(document).ready(function () {
SP.SOD.executeOrDelayUntilScriptLoaded(loadMyProjects,'SP.js');
});
var $ddlMyProjectsNewItem;
var allProjects = new Array();
function loadMyProjects()
{
$ddlMyProjectsNewItem = $("#ctl00_ctl32_g_03acc13b_954d_4bdc_9544_b379206bc9d5_ctl00_ctl05_ctl00_ctl00_ctl00_ctl04_ctl00_Lookup");
var isTimesheetNewForm = $ddlMyProjectsNewItem.attr("id") != undefined ? true : false;
if(!isTimesheetNewForm)
{
return;
}
$ddlMyProjectsNewItem.empty();
// Get the current client context.
var clientContext = SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle('Project Register');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View>" +
"<ViewFields>" +
"<FieldRef Name='Full_x0020_Project_x0020_Name' />" +
"<FieldRef Name='ID' />" +
"<FieldRef Name='Links' />" +
"</ViewFields>" +
"<Query>" +
"<Where>" +
"<Eq>" +
"<FieldRef Name='Status' />" +
"<Value Type='Lookup'>WIP</Value>" +
"</Eq>" +
"</Where>"+
"<OrderBy>" +
"<FieldRef Name='Full_x0020_Project_x0020_Name' Ascending='FALSE' />" +
"</OrderBy>" +
"</Query>" +
"</View>");
this.collListItem = list.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onProjectsQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onProjectsQuerySucceeded(sender, args) {
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var projectId = oListItem.get_id();
var projectName = oListItem.get_item('Full_x0020_Project_x0020_Name');
var projectUrl = oListItem.get_item('Links');
allProjects.push(projectId + "|" + projectName + "|" + projectUrl);
}
$.each( allProjects, function( key, value ) {
var projectMembers = new ProjectSiteMembers(value);
projectMembers.get_members();
});
}
function ProjectSiteMembers(projectData) {
// console.log(projectData);
var projectValues = projectData.split('|');
this.projectId = projectValues[0];
this.projectName = projectValues[1];
this.projectUrl = projectValues[2];
this.clientContext = null;
this.website = null;
this.membersList = null;
this.collListItemMembers = null;
}
// Define the class methods.
ProjectSiteMembers.prototype = {
get_members: function () {
//console.log(this.projectUrl);
this.clientContext = new SP.ClientContext(this.projectUrl);
this.website = this.clientContext.get_web();
this.membersList = this.clientContext.get_web().get_lists().getByTitle('Members')
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View>" +
"<ViewFields>" +
"<FieldRef Name='Member' />" +
"</ViewFields>" +
"<Query>" +
"<Where>" +
"<Eq>" +
"<FieldRef Name='Member' />" +
"<Value Type='Integer'>" +
"<UserID Type='Integer' />" +
" </Value>" +
"</Eq>" +
"</Where>"+
"</Query>" +
"</View>");
this.collListItemMembers = this.membersList.getItems(camlQuery);
this.clientContext.load(this.collListItemMembers);
// Everything works until this point
// This is the line giving me the error
this.clientContext.executeQueryAsync(Function.createDelegate(this, this.onProjectMembersQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
};
function onProjectMembersQuerySucceeded(sender, args) {
//console.log(this.projectId + " - " + this.projectName);
var listItemInfoMember = '';
var listItemMemberEnumerator = collListItemMembers.getEnumerator();
while (listItemMemberEnumerator.moveNext()) {
var oListItemMember = listItemMemberEnumerator.get_current();
$ddlMyProjectsNewItem
.append('<option value="' + this.projectId + '">' + this.projectName + '</option>');
}
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + 'n' + args.get_stackTrace());
}
经过与同事的审查,我们设法解决了问题。我似乎对一个没有在javascript类的作用域中定义的方法进行了不正确的调用。那些愚蠢的错误会让你分心好几个小时,因为你忽略了它们。
以下是经过一些重构后的实际工作代码,因此任何人都可以将其放在这里以供将来参考。
$(document).ready(function () {
SP.SOD.executeOrDelayUntilScriptLoaded(loadMyProjects,'SP.js');
});
var $ddlMyProjects;
var $ddlMyProjectsNewItem;
var $ddlMyProjectsEditItem;
var allProjects = new Array();
function loadMyProjects()
{
$ddlMyProjectsNewItem = $("#ctl00_ctl32_g_03acc13b_954d_4bdc_9544_b379206bc9d5_ctl00_ctl05_ctl00_ctl00_ctl00_ctl04_ctl00_Lookup");
$ddlMyProjectsEditItem= $("#ctl00_ctl32_g_23803b04_bdec_4c98_a4af_0b863da464c1_ctl00_ctl05_ctl00_ctl00_ctl00_ctl04_ctl00_Lookup");
var isTimesheetNewForm = $ddlMyProjectsNewItem.attr("id") != undefined ? true : false;
var isTimesheetEditForm = $ddlMyProjectsEditItem.attr("id") != undefined ? true : false;
if(isTimesheetNewForm)
{
$ddlMyProjects = $ddlMyProjectsNewItem;
}
else if(isTimesheetEditForm)
{
$ddlMyProjects = $ddlMyProjectsEditItem;
}
else
{
return;
}
$ddlMyProjects.empty();
// Get the current client context.
var clientContext = SP.ClientContext.get_current();
var web = clientContext.get_web();
var list = web.get_lists().getByTitle('Project Register');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View>" +
"<ViewFields>" +
"<FieldRef Name='Full_x0020_Project_x0020_Name' />" +
"<FieldRef Name='ID' />" +
"<FieldRef Name='Links' />" +
"</ViewFields>" +
"<Query>" +
"<Where>" +
"<Eq>" +
"<FieldRef Name='Status' />" +
"<Value Type='Lookup'>WIP</Value>" +
"</Eq>" +
"</Where>"+
"<OrderBy>" +
"<FieldRef Name='Full_x0020_Project_x0020_Name' Ascending='TRUE' />" +
"</OrderBy>" +
"</Query>" +
"</View>");
this.collListItem = list.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onProjectsQuerySucceeded), Function.createDelegate(this, this.onProjectsQueryFailed));
}
function onProjectsQuerySucceeded(sender, args) {
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var projectId = oListItem.get_id();
var projectName = oListItem.get_item('Full_x0020_Project_x0020_Name');
var projectUrl = oListItem.get_item('Links');
allProjects.push(projectId + "|" + projectName + "|" + projectUrl);
}
$.each( allProjects, function( key, value ) {
var projectMembers = new ProjectSiteMembers(value);
projectMembers.get_members();
});
}
function onProjectsQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + 'n' + args.get_stackTrace());
}
function ProjectSiteMembers(projectData) {
// console.log(projectData);
var projectValues = projectData.split('|');
this.projectId = projectValues[0];
this.projectName = projectValues[1];
this.projectUrl = projectValues[2];
this.clientContext = null;
this.website = null;
this.membersList = null;
this.collListItemMembers = null;
}
// Define the class methods.
ProjectSiteMembers.prototype = {
onProjectMembersQuerySucceeded: function (sender, args) {
console.log(this.projectId + " - " + this.projectName);
var listItemInfoMember = '';
var listItemMemberEnumerator = this.collListItemMembers.getEnumerator();
while (listItemMemberEnumerator.moveNext()) {
var oListItemMember = listItemMemberEnumerator.get_current();
$ddlMyProjects.append('<option value="' + this.projectId + '">' + this.projectName + '</option>');
return false;
}
},
onProjectMembersQueryFailed: function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + 'n' + args.get_stackTrace());
},
get_members: function () {
console.log(this.projectUrl);
this.clientContext = new SP.ClientContext(this.projectUrl);
this.website = this.clientContext.get_web();
this.membersList = this.clientContext.get_web().get_lists().getByTitle('Members')
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View>" +
"<ViewFields>" +
"<FieldRef Name='Member' />" +
"</ViewFields>" +
"<Query>" +
"<Where>" +
"<Eq>" +
"<FieldRef Name='Member' />" +
"<Value Type='Integer'>" +
"<UserID Type='Integer' />" +
" </Value>" +
"</Eq>" +
"</Where>"+
"</Query>" +
"</View>");
this.collListItemMembers = this.membersList.getItems(camlQuery);
this.clientContext.load(this.collListItemMembers );
this.clientContext.executeQueryAsync(Function.createDelegate(this, this.onProjectMembersQuerySucceeded), Function.createDelegate(this, this.onProjectMembersQueryFailed));
}
};