简体   繁体   中英

Get The Current User ID and Server URL in an HTML Page - CRM 2011

I'm trying to launch a dialog from an HTML page in Microsoft Dynamics CRM 2011, and to be honest I'm not getting anywhere with it.

Here is my HTML page:

<HTML><HEAD>
<META charset=utf-8>
<SCRIPT>
function getServerUrl(){
    var context, crmServerUrl;
    if (typeof GetGlobalContext != "undefined") {
        context = GetGlobalContext();
    }
    else if (typeof Xrm != "undefined") {
        context = Xrm.Page.context;
    }
    else {
        throw new Error("CRM context is not available.");
    }

    if (context.isOutlookClient() && !context.isOutlookOnline()) {
        crmServerUrl = window.location.protocol + "//" + window.location.host;
    } else {
        crmServerUrl = context.getServerUrl();
        crmServerUrl = crmServerUrl.replace(/^(http|https):\/\/([_a-zA-Z0-9\-\.]+)(:([0-9]{1,5}))?/, window.location.protocol + "//" + window.location.host);
        crmServerUrl = crmServerUrl.replace(/\/$/, ""); // remove trailing slash if any
    }
    return crmServerUrl;
}

function LaunchDialog()
 {
var string = getServerUrl();
alert(string);
var url="/" + getOrg() + "/cs/dialog/rundialog.aspx?DialogId=%7b7B189BA4-84B4-4E41-AE85-2066A379E502%7d&EntityName=systemuser&ObjectId=" + getUser();
    //window.open(url, "", "status=no,scrollbars=no,toolbars=no,menubar=no,location=no");
alert(url);
    //window.open(url);
 }
function getOrg() {
alert("Called");
        ///<summary>
        /// get organisation
        ///</summary>

        var Org = "";
        if (typeof GetGlobalContext == "function") {
            var context = GetGlobalContext();
            Org = context.getOrgUniqueName();
        }
        else {
            if (typeof Xrm.Page.context == "object") {
                Org = Xrm.Page.context.getOrgUniqueName();
            }
            else
            { throw new Error("Unable to access Organisation name"); }
        }
       alert("About to return org");
        return Org;
    }

function getUser() {
alert("Called 1");
        ///<summary>
        /// get logged in user
        ///</summary>

        var User = "";
        if (typeof GetGlobalContext == "function") {
            var context = GetGlobalContext();
            User = context.getUserId();
        }
        else {
            if (typeof Xrm.Page.context == "object") {
                User = Xrm.Page.context.getUserId();
            }
            else
            { throw new Error("Unable to access the UserId"); }
        }
      alert("End User");
        return User;
    }   
</SCRIPT>
</HEAD>
<BODY contentEditable=true>
<H1>Create Record</H1>
<P><BUTTON onclick=LaunchDialog()>Try it</BUTTON></P></BODY></HTML>

Web-programming is my strongest talent by any means, so can anyone provide any suggestions as to how I can solve this?

As Guido says I believe you need a reference to ClientGlobalContext.js.aspx. Here is an MSDN article describing GetGlobalContext(). Here is where it mentions to include a reference to it.

When you need context information outside a form, include a reference to the ClientGlobalContext.js.aspx page in an HTML web resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM