Gets the current user and enters string into specified field

function loggedUser(field) {
    var user = Xrm.Page.context.getUserId();
    var userId = user.substring(1, 37);
    var serverUrl = document.location.protocol;
    serverUrl += "//" + document.location.host;
    serverUrl += "/" + Xrm.Page.context.getOrgUniqueName();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    var ODATA_EntityCollection = "/SystemUserSet";
    var ODATA_Query = "(guid\'" + userId + "')";
    var ODATA_Final_url = serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection + ODATA_Query;

    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: ODATA_Final_url,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            var userName = data.d.FullName;
            Xrm.Page.getAttribute(field).setSubmitMode("always");
            Xrm.Page.getAttribute(field).setValue(userName);
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            alert('Error: ' + ODATA_Final_url);

        }
    });

}

Comments

Popular posts from this blog

Workflow Activity Input and Output Parameters and Data Types in MS CRM 2013/15/16

Get Workflow Guid in Javascript using OData query

Convert Plugin to Sandbox mode,