Context.ExecuteQueryAsync not trigerring
-
21-01-2021 - |
Pergunta
I am debugging the code below and the line Context.ExecuteQueryAsync is not being trigerred. I am not getting any error.
async function updateListItem(listName, itemId, itemProperties, pageurl, context, listItem) {
try {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + itemId + ")",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(itemProperties),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": '*'
},
success: function (data) {
if (M_DEBUG) {
console.log("department added");
}
console.log('department added');
// publish the news
listItem.get_file().publish("Publishing");
console.log(context);
try{
context.executeQueryAsync(
function () {
if (M_DEBUG) {
console.log("file published");
}
console.log(pageurl);
// redirect to news page in edit mode
window.location.replace(pageurl + "?ControlMode=Edit&DisplayMode=Design");
},
function (sender, args) {
if (M_DEBUG) {
console.log("error while publishing file:" + args.get_message() +
'\n' + args.get_stackTrace());
}
}
);}
catch(err){
console.log(err.message);
}
},
error: function (data) {
if (M_DEBUG) {
console.log(data);
}
}
});
} catch (err) {
console.log(err.message);
}
}
Solução
Ensure the item(file) is checked out before updating.
You can print the error details using error handler as demonstrated below:
$.ajax({
url: siteurl + "/_api/web/lists/getbytitle('" + listName + "')/items("+itemId+")",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
//...
},
error: function (data) {
console.log(JSON.stringify(data));
}
});
Try the following script to update and publish the item:
Update();
function Update() {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
var listName = "Pages";
var itemId = 1; // Update Item Id here
var titleVal = "test";
var itemType = GetItemTypeForListName(listName);
var itemProperties = {
"__metadata": { "type": itemType },
"Title": titleVal
};
var pageurl = "https://tenant.sharepoint.com/Sites/<site>/Pages/p1.aspx";
var context = new SP.ClientContext();
var oList = context.get_web().get_lists().getByTitle(listName);
var listItem = oList.getItemById(itemId);
context.load(listItem);
context.load(listItem.get_file());
context.executeQueryAsync(
Function.createDelegate(this, function(){
console.log(listItem.get_file().get_checkOutType());
console.log(SP.CheckOutType.online);
if(listItem.get_file().get_checkOutType() !== SP.CheckOutType.online) {
checkOutItem(listName,itemId,itemProperties, pageurl, context, listItem);
}else{
//item is checked out
updateListItem(listName,itemId,itemProperties, pageurl, context, listItem);
}
}),
Function.createDelegate(this, this.onQueryFailed)
);
});
}
function checkOutItem(listName,itemId,itemProperties, pageurl,context, listItem){
listItem.get_file().checkOut();
context.executeQueryAsync(function () {
console.log("successfully item is checked out");
updateListItem(listName,itemId,itemProperties, pageurl, context, listItem);
},
function (sender, args) {
console.log("failed: " + args.get_message());
});
}
async function updateListItem(listName, itemId, itemProperties, pageurl,context, listItem) {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items("+itemId+")",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(itemProperties),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
success: function (data) {
console.log('Success');
//check in
listItem.get_file().checkIn();
// publish
listItem.get_file().publish("Publishing");
context.executeQueryAsync(function () {
console.log("item is published");
window.location.replace(pageurl + "?ControlMode=Edit&DisplayMode=Design");
},
function (sender, args) {
console.log("failed: " + args.get_message());
});
},
error: function (data) {
console.log(JSON.stringify(data));
}
});
}
// Get List Item Type metadata
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "Item";
}
function onQueryFailed(sender, args) {
console.log('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
Update: check out item before redirecting page.
Update();
function Update() {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
var listName = "Pages";
var itemId = 1; // Update Item Id here
var titleVal = "test";
var itemType = GetItemTypeForListName(listName);
var itemProperties = {
"__metadata": { "type": itemType },
"Title": titleVal
};
var pageurl = "https://tenant.sharepoint.com/Sites/<site>/Pages/p1.aspx";
var context = new SP.ClientContext();
var oList = context.get_web().get_lists().getByTitle(listName);
var listItem = oList.getItemById(itemId);
context.load(listItem);
context.load(listItem.get_file());
context.executeQueryAsync(
Function.createDelegate(this, function(){
console.log(listItem.get_file().get_checkOutType());
console.log(SP.CheckOutType.online);
if(listItem.get_file().get_checkOutType() !== SP.CheckOutType.online) {
checkOutItem(listName,itemId,itemProperties, pageurl, context, listItem);
}else{
//item is checked out
updateListItem(listName,itemId,itemProperties, pageurl, context, listItem);
}
}),
Function.createDelegate(this, this.onQueryFailed)
);
});
}
function checkOutItem(listName,itemId,itemProperties, pageurl,context, listItem){
listItem.get_file().checkOut();
context.executeQueryAsync(function () {
console.log("successfully item is checked out");
updateListItem(listName,itemId,itemProperties, pageurl, context, listItem);
},
function (sender, args) {
console.log("failed: " + args.get_message());
});
}
async function updateListItem(listName, itemId, itemProperties, pageurl,context, listItem) {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items("+itemId+")",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(itemProperties),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "MERGE",
"If-Match": "*"
},
success: function (data) {
console.log('Success');
//check in
listItem.get_file().checkIn();
// publish
listItem.get_file().publish("Publishing");
context.executeQueryAsync(function () {
console.log("item is published");
//redirect page
redirectPage(listItem,context,pageurl)
},
function (sender, args) {
console.log("failed: " + args.get_message());
});
},
error: function (data) {
console.log(JSON.stringify(data));
}
});
}
// Get List Item Type metadata
function GetItemTypeForListName(name) {
return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "Item";
}
function redirectPage(listItem,context,pageurl){
listItem.get_file().checkOut();
context.executeQueryAsync(function () {
console.log("successfully item is checked out");
window.location.replace(pageurl + "?ControlMode=Edit&DisplayMode=Design");
},
function (sender, args) {
console.log("failed: " + args.get_message());
});
}
function onQueryFailed(sender, args) {
console.log('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange