Button actions delay
Hi everyone,
After clicking the save button I'm executing the following javascript code.
var group = JSON.stringify({
description: [[Description]],
displayName: "Team " + [[Title]],
groupTypes: [
"Unified"
],
mailEnabled: true,
mailNickname: [[Title]],
securityEnabled: false
});
function createOffice365Group(token) {
jQuery.ajax({
type: "POST",
url: "https://graph.microsoft.com/v1.0/groups",
headers: {
"Authorization": "Bearer " + token,
"Content-Type": "application/json"
},
data: group,
dataType:"json"
}).done(function (response) {
console.log("Successfully created group.");
}).fail(function () {
console.log("Creating group failed.");
});
}
var userAgentApplication = new Msal.UserAgentApplication('<Application ID>', null, function (errorDes, token, error, tokenType) {
// this callback is called after loginRedirect OR acquireTokenRedirect. It's not used with loginPopup, acquireTokenPopup.
if (error) {
console.log(error + ": " + errorDes);
}
else
console.log("Token type = " + tokenType);
})
userAgentApplication.loginPopup(["user.read"]).then(function (token) {
var user = userAgentApplication.getUser();
if (user) {
console.log("signed in sucessfully");
// get an access token
userAgentApplication.acquireTokenSilent(["user.read"]).then(function (token) {
console.log("Success acquiring access token");
createOffice365Group(token);
}, function (error) {
// interaction required
if (error.indexOf("interaction_required" != -1)) {
userAgentApplication.acquireTokenPopup(["user.read"]).then(function (token) {
console.log("Success acquiring access token");
createOffice365Group(token);
}, function (error) {
console.log("Failure acquiring token: " + error);
});
}
});
} else {
console.log("signed in failure");
}
}, function (error) {
console.log("error: " + error);
});
This javascript creates a new office 365 group.
Now there are two questions:
- The problem is, that I need to authenticate on a popup. During this authentication the other two actions (Save and Redirect) already start.
Is there a possibility to wait until my custom code is finished? - Is it possible to edit the current SharePoint item with the result of REST call?
Thanks for your help.
Best Regards
Manuel Habert
-
Hello Manuel Habert
For this
- The problem is, that I need to authenticate on a popup. During this authentication the other two actions (Save and Redirect) already start.
Is there a possibility to wait until my custom code is finished?
you can use next approach:
In you code you will add next line: (window.O365GroupCreated = true)
function createOffice365Group(token) {
jQuery.ajax({
type: "POST",
url: "https://graph.microsoft.com/v1.0/groups",
headers: {
"Authorization": "Bearer " + token,
"Content-Type": "application/json"
},
data: group,
dataType:"json"
}).done(function (response) {
window.O365GroupCreated = true;
console.log("Successfully created group.");
}).fail(function () {
console.log("Creating group failed.");
});
}Then you must delete redirect action and add form load action which will continuously check whether group was created and if it was created than redirect to your url . Code will looks like:
var redirectUrl = ... // can be formed using placeholders
//check each 1 sec
function checkWhetherO365GroupExist(){
// do whatever you like here
if(window.O365GroupCreated){
window.loacation.href = redirectUrl ;
}
setTimeout(checkWhetherO365GroupExist, 1000);
// Also you can stop checking using some timeout
}
checkWhetherO365GroupExist();Regarding
- Is it possible to edit the current SharePoint item with the result of REST call?
Yes. For example you can add button with action. Than you must add action with type execute code where you will perform your REST call. You REST call will return some value and then you can programatically update listitem . How to do this you can find here https://msdn.microsoft.com/ru-ru/library/office/hh185011(v=office.14).aspx
Kind Regards
Andriy Berezovsky0 - The problem is, that I need to authenticate on a popup. During this authentication the other two actions (Save and Redirect) already start.
-
Thanks for your reply.
Both could solve my problems. I deleted the save and redirect actions and I'm doing everything in my JavaScript.
Best Regards
Manuel Habert
0
Please sign in to leave a comment.
Comments
2 comments