Dynamics 365 Web Api 檢查使用者是否具有某個安全形色

vic0228發表於2018-03-28

   使用者和安全形色的關係是N:N,所以要查詢使用者是否擁有某個安全形色即去查詢對應的中間表取值即可,那web api如何來查詢N:N關係。

   先看下查詢的xml,這裡的entity name即是實體關係名稱

    var roleXml = "<fetch no-lock='true' output-format='xml-platform'>\
                      <entity name='systemuserroles'>\
                        <attribute name='systemuserid'/>\
                        <link-entity name='role' from='roleid' to ='roleid'>\
                          <filter type='and'>\
                            <condition attribute='name' operator='like' value='%審批%'></condition>\
                          </filter>\
                        </link-entity>\
                        <filter type='and'>\
                          <condition attribute='systemuserid' operator='eq' value='" + userId + "'></condition>\
                    </filter></entity></fetch>";

  但在發起request時的entityName卻是systemuserrolescollection,這個你可以在後設資料列表中查詢到,後設資料地址如下,對應到你自己的系統

https://org.domain.com:446/api/data/v8.2/$metadata

var userRole = execFecthXml('systemuserrolescollection', roleXml);
function execFecthXml(entityName, fetchXml) {

    var fetchStr = this.getWebAPIUrl() + entityName + "?fetchXml=" + fetchXml;
    var req = CRMSdk.ajax.getXHR();
    req.open("GET", encodeURI(fetchStr), false);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");

    req.send();

    if (req.status == 200)
        return JSON.parse(req.responseText).value;
    else
        throw new Error(JSON.parse(req.responseText).error.message);

}

相關文章