Javascript 无法解决';标题「;尚未初始化
我试图禁用用户在编辑模式下删除与当前记录关联的任何附件的功能。我在上找到了这个代码。为了让它运行,我不得不做一些更改。我认为我现在的问题是我的依赖脚本没有正确加载,因此函数出错。代码是:Javascript 无法解决';标题「;尚未初始化,javascript,sharepoint-2013,Javascript,Sharepoint 2013,我试图禁用用户在编辑模式下删除与当前记录关联的任何附件的功能。我在上找到了这个代码。为了让它运行,我不得不做一些更改。我认为我现在的问题是我的依赖脚本没有正确加载,因此函数出错。代码是: //if (document.referrer == "" || document.referrer == null) if (window.location.pathname.toLowerCase().indexOf("editform.aspx") > -1) { // These two
//if (document.referrer == "" || document.referrer == null)
if (window.location.pathname.toLowerCase().indexOf("editform.aspx") > -1) {
// These two lines are required, without it my code won't run.
SP.SOD.executeFunc('SP.js', 'SP.ClientContext');
SP.SOD.executeFunc('sp.runtime.js');
var listName = "BSMRequests";
var currentItemID;
var attachmentAuthor = [];
var itemArray = [];
$(document).ready(function(){
// Get ID of the current item.
currentItemID = window.location.href.toLowerCase();
currentItemID = currentItemID.substring(currentItemID.toLowerCase().indexOf("?id=") + 4,currentItemID.toLowerCase().indexOf("&source="));
// Remove the line below in case the URL of your item is
// not shown as a modal dialog.
//currentItemID = currentItemID.substring(0, currentItemID.toLowerCase().indexOf("&isdlg"));
//console.log('currentItemID='+currentItemID);
// Save the ID of the current item in the session
// (not necessary, but I prefer it this way)
sessionStorage.setItem("SessionItemCurrentItemID", currentItemID);
// Get attachments of current item.
var url = "/_api/Web/Lists/getByTitle('" + listName + "')/Items(" + currentItemID + ")/AttachmentFiles",
qs = "?$select=ID,Author/Title,*&$expand=Author,AttachmentFiles",
siteUrl = "https://share.health.wisconsin.gov/hc/teams/MES";
$.ajax( {
url : siteUrl + url + qs,
type : 'GET',
headers : {
'accept' : 'application/json;odata=verbose',
'content-type' : 'application/json;odata=verbose'
},
success : successHandler,
fail : failHandler
});
function successHandler(data) {
if (data) {
// If the item has attachments, then run this function.
//console.log('yes? attachments');
$.each(data.d.results, function() {
//console.log('processing results');
getWebProperties(sessionStorage.getItem("SessionItemCurrentItemID"));
});
} //else {console.log('No attachments');}
}
function failHandler(data, errCode, errMessage) {
console.log('Error: ' + errMessage);
}
function getWebProperties(itemID) {
var attachmentFiles;
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var attachmentFolder = web.getFolderByServerRelativeUrl('Lists/'+listName+'/Attachments/' + itemID);
attachmentFiles = attachmentFolder.get_files();
ctx.load(attachmentFiles);
ctx.executeQueryAsync(function(){
// I can't remember what the $2_1 was again, but anyway...
for (var j = 0; j < attachmentFiles["$2_1"].length; j++) {
var author = attachmentFiles.itemAt(j).get_author();
attachmentAuthor.push([attachmentFiles.itemAt(j).get_name(),author]);
// You'll need to load the author along with the title parameter,
// in order to be able to fetch the name of the author later.
ctx.load(author, 'Title');
ctx.executeQueryAsync(function(){}, function(err) {});
}
// Loop is not necesarrily required, but you will need to set a
// timeout. Comes in handy when you have a lot of attachments.
checkAttachmentsLoop();
}, function(err) {});
}
function checkAttachmentsLoop() {
setTimeout(function(){
if (attachmentAuthor.length) {
checkAttachments();
}
else {
checkAttachmentsLoop();
}
},100);
}
function checkAttachments() {
for (var h = 0; h < attachmentAuthor.length; h++) {
// if you log attachmentAuthor[h][0] to the concole, you'll get
// the name of the attachment.
// if you log attachmentAuthor[h][1].get_title() to the console,
// you'll get the name of the author.
var currentAuthor = attachmentAuthor[h][1].get_title();
// If the current user is not the author of the current attachment,
// then we disable the ability to delete the attachment from the item.
// var currentUser = "Harvancik, Steve Skkk";
var currentUser = sessionStorage.getItem("sessionItemUserName");
if (currentAuthor != currentUser) {
var tr = document.getElementById("idAttachmentsTable").getElementsByTagName("tr");
for (var i = 0; i < tr.length; i++) {
var currentTR = $(tr)[i];
var anchors = $(currentTR).find("a")[0];
var deletes = $(currentTR).find("a")[1];
if (anchors.innerHTML == attachmentAuthor[h][0]) {
$(currentTR).attr("disabled", "disabled");
$(anchors).css("color", "#b1b1b1");
$(anchors).removeAttr("href");
$(anchors).removeAttr("onclick");
$(deletes).css("text-decoration", "line-through");
$(deletes).css("color", "#b1b1b1");
$(deletes).removeAttr("href");
$(deletes).removeAttr("onclick");
}
}
}
}
}
})
}
else {
if (sessionStorage.getItem("SessionItemCurrentItemID") != null) {
sessionStorage.removeItem("SessionItemCurrentItemID");
}
}
我首先尝试将其放入函数中,并通过ExecuteOrderLayUntilScriptLoaded调用它,但没有成功。在运行之前调用此函数并加载依赖脚本的最佳方法是什么
谢谢在
ctx.executeQueryAsync
之前,您将作者
推送到附件作者
,因此值尚未加载,您可以在检查附件
中加载它
测试脚本:
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript">
//if (document.referrer == "" || document.referrer == null)
if (window.location.pathname.toLowerCase().indexOf("editform.aspx") > -1) {
// These two lines are required, without it my code won't run.
SP.SOD.executeFunc('SP.js', 'SP.ClientContext');
SP.SOD.executeFunc('sp.runtime.js');
var listName = "MyList";
var currentItemID;
var attachmentAuthor = [];
var itemArray = [];
$(document).ready(function () {
// Get ID of the current item.
currentItemID = window.location.href.toLowerCase();
currentItemID = currentItemID.substring(currentItemID.toLowerCase().indexOf("?id=") + 4, currentItemID.toLowerCase().indexOf("&source="));
// Remove the line below in case the URL of your item is
// not shown as a modal dialog.
//currentItemID = currentItemID.substring(0, currentItemID.toLowerCase().indexOf("&isdlg"));
//console.log('currentItemID='+currentItemID);
// Save the ID of the current item in the session
// (not necessary, but I prefer it this way)
sessionStorage.setItem("SessionItemCurrentItemID", currentItemID);
// Get attachments of current item.
var url = "/_api/Web/Lists/getByTitle('" + listName + "')/Items(" + currentItemID + ")/AttachmentFiles",
qs = "?$select=ID,Author/Title,*&$expand=Author,AttachmentFiles",
siteUrl = _spPageContextInfo.webAbsoluteUrl;
$.ajax({
url: siteUrl + url + qs,
type: 'GET',
headers: {
'accept': 'application/json;odata=verbose',
'content-type': 'application/json;odata=verbose'
},
success: successHandler,
fail: failHandler
});
function successHandler(data) {
if (data) {
// If the item has attachments, then run this function.
//console.log('yes? attachments');
$.each(data.d.results, function () {
//console.log('processing results');
getWebProperties(sessionStorage.getItem("SessionItemCurrentItemID"));
});
} //else {console.log('No attachments');}
}
function failHandler(data, errCode, errMessage) {
console.log('Error: ' + errMessage);
}
function getWebProperties(itemID) {
var attachmentFiles;
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var attachmentFolder = web.getFolderByServerRelativeUrl('Lists/' + listName + '/Attachments/' + itemID);
attachmentFiles = attachmentFolder.get_files();
ctx.load(attachmentFiles);
ctx.executeQueryAsync(function () {
// I can't remember what the $2_1 was again, but anyway...
for (var j = 0; j < attachmentFiles["$2_1"].length; j++) {
var author = attachmentFiles.itemAt(j).get_author();
attachmentAuthor.push([attachmentFiles.itemAt(j).get_name(), author]);
}
// Loop is not necesarrily required, but you will need to set a
// timeout. Comes in handy when you have a lot of attachments.
checkAttachmentsLoop();
}, function (err) { });
}
function checkAttachmentsLoop() {
setTimeout(function () {
if (attachmentAuthor.length) {
checkAttachments();
}
else {
checkAttachmentsLoop();
}
}, 100);
}
function checkAttachments() {
var ctx = new SP.ClientContext.get_current();
for (var h = 0; h < attachmentAuthor.length; h++) {
// if you log attachmentAuthor[h][0] to the concole, you'll get
// the name of the attachment.
// if you log attachmentAuthor[h][1].get_title() to the console,
// you'll get the name of the author.
// You'll need to load the author along with the title parameter,
// in order to be able to fetch the name of the author later.
ctx.load(attachmentAuthor[h][1], 'Title');
ctx.executeQueryAsync(function () {
var currentAuthor = attachmentAuthor[h][1].get_title();
console.log(currentAuthor);
}, function (err) {
console.log(err);
});
//var currentAuthor = attachmentAuthor[h][1].get_title();
// If the current user is not the author of the current attachment,
// then we disable the ability to delete the attachment from the item.
// var currentUser = "Harvancik, Steve Skkk";
var currentUser = sessionStorage.getItem("sessionItemUserName");
if (currentAuthor != currentUser) {
var tr = document.getElementById("idAttachmentsTable").getElementsByTagName("tr");
for (var i = 0; i < tr.length; i++) {
var currentTR = $(tr)[i];
var anchors = $(currentTR).find("a")[0];
var deletes = $(currentTR).find("a")[1];
if (anchors.innerHTML == attachmentAuthor[h][0]) {
$(currentTR).attr("disabled", "disabled");
$(anchors).css("color", "#b1b1b1");
$(anchors).removeAttr("href");
$(anchors).removeAttr("onclick");
$(deletes).css("text-decoration", "line-through");
$(deletes).css("color", "#b1b1b1");
$(deletes).removeAttr("href");
$(deletes).removeAttr("onclick");
}
}
}
}
}
})
}
else {
if (sessionStorage.getItem("SessionItemCurrentItemID") != null) {
sessionStorage.removeItem("SessionItemCurrentItemID");
}
}
</script>
//if(document.referer==“”| | document.referer==null)
if(window.location.pathname.toLowerCase().indexOf(“editform.aspx”)>-1){
//这两行是必需的,没有它我的代码将无法运行。
SP.SOD.executeFunc('SP.js','SP.ClientContext');
SP.SOD.executeFunc('SP.runtime.js');
var listName=“MyList”;
var-currentItemID;
var attachmentAuthor=[];
var itemArray=[];
$(文档).ready(函数(){
//获取当前项的ID。
currentItemID=window.location.href.toLowerCase();
currentItemID=currentItemID.substring(currentItemID.toLowerCase().indexOf(“?id=”)+4,currentItemID.toLowerCase().indexOf(“&source=”);
//如果项目的URL为,请删除下面的行
//不显示为模式对话框。
//currentItemID=currentItemID.substring(0,currentItemID.toLowerCase().indexOf(“&isdlg”);
//log('currentItemID='+currentItemID);
//在会话中保存当前项目的ID
//(没有必要,但我更喜欢这种方式)
setItem(“SessionItemCurrentItemID”,currentItemID);
//获取当前项目的附件。
var url=“/_api/Web/Lists/getByTitle(“+listName+”)/Items(“+currentItemID+”)/AttachmentFiles”,
qs=“?$select=ID,Author/Title,*&$expand=Author,AttachmentFiles”,
siteUrl=\u spPageContextInfo.webAbsoluteUrl;
$.ajax({
url:siteUrl+url+qs,
键入:“GET”,
标题:{
“accept”:“application/json;odata=verbose”,
“内容类型”:“应用程序/json;odata=verbose”
},
success:successHandler,
失败:失败处理程序
});
函数successHandler(数据){
如果(数据){
//如果项目具有附件,则运行此功能。
//console.log(“是?附件”);
$.each(data.d.results,function(){
//console.log(“处理结果”);
getWebProperties(sessionStorage.getItem(“SessionItemCurrentItemID”);
});
}//else{console.log('No attachments');}
}
函数故障处理程序(数据、错误代码、错误消息){
log('Error:'+errMessage);
}
函数getWebProperties(itemID){
var附件文件;
var ctx=new SP.ClientContext.get_current();
var web=ctx.get_web();
var attachmentFolder=web.getFolderByServerRelativeUrl('Lists/'+listName+'/Attachments/'+itemID);
attachmentFiles=attachmentFolder.get_files();
ctx.load(附件文件);
executeQueryAsync(函数(){
//我不记得2美元1是什么了,但不管怎样。。。
对于(var j=0;j <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript">
//if (document.referrer == "" || document.referrer == null)
if (window.location.pathname.toLowerCase().indexOf("editform.aspx") > -1) {
// These two lines are required, without it my code won't run.
SP.SOD.executeFunc('SP.js', 'SP.ClientContext');
SP.SOD.executeFunc('sp.runtime.js');
var listName = "MyList";
var currentItemID;
var attachmentAuthor = [];
var itemArray = [];
$(document).ready(function () {
// Get ID of the current item.
currentItemID = window.location.href.toLowerCase();
currentItemID = currentItemID.substring(currentItemID.toLowerCase().indexOf("?id=") + 4, currentItemID.toLowerCase().indexOf("&source="));
// Remove the line below in case the URL of your item is
// not shown as a modal dialog.
//currentItemID = currentItemID.substring(0, currentItemID.toLowerCase().indexOf("&isdlg"));
//console.log('currentItemID='+currentItemID);
// Save the ID of the current item in the session
// (not necessary, but I prefer it this way)
sessionStorage.setItem("SessionItemCurrentItemID", currentItemID);
// Get attachments of current item.
var url = "/_api/Web/Lists/getByTitle('" + listName + "')/Items(" + currentItemID + ")/AttachmentFiles",
qs = "?$select=ID,Author/Title,*&$expand=Author,AttachmentFiles",
siteUrl = _spPageContextInfo.webAbsoluteUrl;
$.ajax({
url: siteUrl + url + qs,
type: 'GET',
headers: {
'accept': 'application/json;odata=verbose',
'content-type': 'application/json;odata=verbose'
},
success: successHandler,
fail: failHandler
});
function successHandler(data) {
if (data) {
// If the item has attachments, then run this function.
//console.log('yes? attachments');
$.each(data.d.results, function () {
//console.log('processing results');
getWebProperties(sessionStorage.getItem("SessionItemCurrentItemID"));
});
} //else {console.log('No attachments');}
}
function failHandler(data, errCode, errMessage) {
console.log('Error: ' + errMessage);
}
function getWebProperties(itemID) {
var attachmentFiles;
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var attachmentFolder = web.getFolderByServerRelativeUrl('Lists/' + listName + '/Attachments/' + itemID);
attachmentFiles = attachmentFolder.get_files();
ctx.load(attachmentFiles);
ctx.executeQueryAsync(function () {
// I can't remember what the $2_1 was again, but anyway...
for (var j = 0; j < attachmentFiles["$2_1"].length; j++) {
var author = attachmentFiles.itemAt(j).get_author();
attachmentAuthor.push([attachmentFiles.itemAt(j).get_name(), author]);
}
// Loop is not necesarrily required, but you will need to set a
// timeout. Comes in handy when you have a lot of attachments.
checkAttachmentsLoop();
}, function (err) { });
}
function checkAttachmentsLoop() {
setTimeout(function () {
if (attachmentAuthor.length) {
checkAttachments();
}
else {
checkAttachmentsLoop();
}
}, 100);
}
function checkAttachments() {
var ctx = new SP.ClientContext.get_current();
for (var h = 0; h < attachmentAuthor.length; h++) {
// if you log attachmentAuthor[h][0] to the concole, you'll get
// the name of the attachment.
// if you log attachmentAuthor[h][1].get_title() to the console,
// you'll get the name of the author.
// You'll need to load the author along with the title parameter,
// in order to be able to fetch the name of the author later.
ctx.load(attachmentAuthor[h][1], 'Title');
ctx.executeQueryAsync(function () {
var currentAuthor = attachmentAuthor[h][1].get_title();
console.log(currentAuthor);
}, function (err) {
console.log(err);
});
//var currentAuthor = attachmentAuthor[h][1].get_title();
// If the current user is not the author of the current attachment,
// then we disable the ability to delete the attachment from the item.
// var currentUser = "Harvancik, Steve Skkk";
var currentUser = sessionStorage.getItem("sessionItemUserName");
if (currentAuthor != currentUser) {
var tr = document.getElementById("idAttachmentsTable").getElementsByTagName("tr");
for (var i = 0; i < tr.length; i++) {
var currentTR = $(tr)[i];
var anchors = $(currentTR).find("a")[0];
var deletes = $(currentTR).find("a")[1];
if (anchors.innerHTML == attachmentAuthor[h][0]) {
$(currentTR).attr("disabled", "disabled");
$(anchors).css("color", "#b1b1b1");
$(anchors).removeAttr("href");
$(anchors).removeAttr("onclick");
$(deletes).css("text-decoration", "line-through");
$(deletes).css("color", "#b1b1b1");
$(deletes).removeAttr("href");
$(deletes).removeAttr("onclick");
}
}
}
}
}
})
}
else {
if (sessionStorage.getItem("SessionItemCurrentItemID") != null) {
sessionStorage.removeItem("SessionItemCurrentItemID");
}
}
</script>