ManuelH

Button actions delay

Discussion created by ManuelH on May 24, 2017
Latest reply on May 26, 2017 by ManuelH

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

Outcomes