8 Aralık 2015 Salı

CRM 2011 Javascript - Kullanıcı Departmanını Alma (Get User Departman)


function GetUserDepartman() {
    try {
        var result = "";
        var userid = Xrm.Page.context.getUserId();
        var query = "SystemUserSet?$select=SystemUserId,BusinessUnitId&$filter=SystemUserId eq guid'" + ConverttoStringFromGuid(userid) + "'";
        var userEntity = ODataRetrieveMultipleAjax(query);
        if (userEntity != null && userEntity.results[0] != null) {
            if (userEntity.results[0].BusinessUnitId != null && userEntity.results[0].BusinessUnitId.Id != null) {
                result = userEntity.results[0].BusinessUnitId.Name;
            }
        }

        return result;
    } catch (e) {
        alert("Function GetUserDepartman()\n" + e.message);
        return "";
    }
}

function ConverttoStringFromGuid(guidid) {
    if (guidid != null && guidid != "") {
        return guidid.toString().replace('{', '').replace('}', '').toUpperCase();
    }

    return "00000000-0000-0000-0000-000000000000";
}

function ODataRetrieveMultipleAjax(oDataSelect) {
    var crmOrgSvc = Xrm.Page.context.prependOrgName("/xrmservices/2011/OrganizationData.svc/");
    var result = null;

    oDataSelect = crmOrgSvc + oDataSelect;
    jQuery.support.cors = true;

    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: oDataSelect,
        async: false,
        beforeSend: function (XMLHttpRequest) {
            //Specifying this header ensures that the results will be returned as JSON.
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (data != null && data.d != null) {
                result = data.d;
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown, "Error Function:" + "ODataRetrieveMultipleAjax");
        }
    });
    return result;
}


Hiç yorum yok:

Yorum Gönder