2

During the loading of a popup form using Alchemy I want to get the current user. Currently I have a backend Alchemy Controller getting the user and returning it to the form. It works fine in Firefox, but in Chrome I have the error 'Alchemy is undefined'. I have a feeling it is a timing issue and the Alchemy framework is not loaded or available yet.

Here is the code I have to get the user from the backend service, and also the backend controller code.

// Javascript Function to get the User in the Alchemy Popup
function getUser() {
    var username = "";
    try {

        Alchemy.Plugins["BatchPublisher"].Api.BatchPublishService.getUser()
         .success(function (response) {
             console.log("Have user from Alchemy WebService=" + response);
             username = response;
             jQuery('#username').html(response);
         })
          .error(function (errorCode, errorObject) {
              alert("error getting username" + errorCode);
              console.log(errorObject);
         });

    }
    catch (err) {
        alert("Alchemy plugin error!" + err.message)
    }
    return username;
}


// C# Alchemy Controller code to get the current user
[HttpGet]
[Route("GetUser")]
public string GetUser()
{
     string userName= Client.GetCurrentUser().Title;
     return userName;
}
2

Make sure you only call getUser after the entire HTML document has finished loading. The recommended way is to create a View and only call your method in the initialize method of the view or later.

For an example, take a look at my CountItems plugin (Popup.js) - which loads settings from the server when the popup view is initializing (and then starts counting the items automatically, if so configured). All of this happens in the initialize of the Popup View.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.