2014年12月30日 星期二

CRM 2011 Get Current User ID in JavaScript

CRM 2011 Get Current User ID in JavaScript

JavaScript:
function OnLoadForm()
{
   var userId = Xrm.Page.context.getUserId();
}

Other Sample(Ref Web):
function getCurrentUserFullName() {
    var serverUrl;
    if (Xrm.Page.context.getClientUrl !== undefined) {
        serverUrl = Xrm.Page.context.getClientUrl();
    } else {
        serverUrl = Xrm.Page.context.getServerUrl();
    }
    var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
    var userRequest = new XMLHttpRequest();
    userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')", false);
    userRequest.setRequestHeader("Accept", "application/json");
    userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    userRequest.send();
    if (userRequest.status === 200) {
        var retrievedUser = JSON.parse(userRequest.responseText).d;
        var userFullName = retrievedUser.FullName;
        return userFullName;
    }
    else {
        return "error";
    }
}
http://msdn.microsoft.com/en-us/library/gg334511.aspx

沒有留言:

張貼留言