按钮可见基于团队

时间:2016-08-07 10:10:27

标签: dynamics-crm

按钮应该可见基于团队。示例如果登录用户部分"测试团队","测试按钮"应该启用。如果没有禁用。我正在使用webapi。但它给出了null值。任何建议??

 function GetUserTeam() {
var clientUrl1 = Xrm.Page.context.getClientUrl();
var oDataPath1 = clientUrl1 + "/api/data/v8.0/";
var req1 = new XMLHttpRequest();
   try {

    var strTest = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
                    "  <entity name='team'>" +
                    "    <attribute name='name' />" +
                    "    <attribute name='businessunitid' />" +
                    "    <attribute name='teamid' />" +
                    "    <attribute name='teamtype' />" +
                    "    <order attribute='name' descending='false' />" +
                    "    <filter type='and'>" +
                    "      <condition attribute='teamtype' operator='eq' value='0' />" +
                    "    </filter>" +
                    "    <link-entity name='teammembership' from='teamid' to='teamid' visible='false' intersect='true'>" +
                    "      <link-entity name='systemuser' from='systemuserid' to='systemuserid' alias='ab'>" +
                    "        <filter type='and'>" +
                    "          <condition attribute='systemuserid' operator='eq-userid' />" +
                    "        </filter>" +
                    "      </link-entity>" +
                    "    </link-entity>" +
                    "  </entity>" +
                    "</fetch>";
    req1.open("GET", oDataPath1 + "teams?fetchXml=" + encodeURI(strTest), true);
    req1.setRequestHeader("Accept", "application/json");
    req1.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req1.setRequestHeader("OData-MaxVersion", "4.0");
    req1.setRequestHeader("OData-Version", "4.0");
    req1.onreadystatechange = function () {
        if (this.readyState == 4) {
            // debugger;
            req1.onreadystatechange = null;
            if (this.status == 200) {
                var reqResults = JSON.parse(this.response).value;
                if (reqResults != null && reqResults.length > 0) {
                    if ("TestTeam"==reqResults.length[i].name){return true;}

                else{return false;}
                }
                    }//End of for

                }//En of IF

            }

            else {

                var error = JSON.parse(this.response).error;
                alert(error.message);
            }

        }

    };      
    req1.send();              

}
catch (e) {

    alert(e);

}

}

1 个答案:

答案 0 :(得分:0)

感谢您发布示例代码。它有一些语法错误,但我认为这是因为你为帖子编辑了它。例如,for循环没有出现,并且缺少括号。

  1. 请求是异步的。从您的方法返回未定义。您要么将XMLHttpRequest更改为同步运行(通常不建议出于用户体验原因),要么在有结果时传入回调函数进行调用。在这里我添加了onSuccess和onError。 onSuccess将使用true / false作为参数,即是否找到TestTeam。

  2. 在你的for循环中你有reqResults.length [i]你可能在那里reqResults [i]。同样,这可能只是编辑发布的问题。

  3. 因此,如果TestTeam是用户团队之一,此代码将调用onSuccess,其值为true。假如果不是。或者如果发生错误,它将调用onError。您可以编辑以构建要传回的团队数组等。或者您可以编辑.open()调用以使其运行synchronously并且不使用回调。

    function GetUserTeam(onSuccess, onError) {
        var clientUrl1 = Xrm.Page.context.getClientUrl();
        var oDataPath1 = clientUrl1 + "/api/data/v8.0/";
        var req1 = new XMLHttpRequest();
        try {
    
            var strTest = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
                            "  <entity name='team'>" +
                            "    <attribute name='name' />" +
                            "    <attribute name='businessunitid' />" +
                            "    <attribute name='teamid' />" +
                            "    <attribute name='teamtype' />" +
                            "    <order attribute='name' descending='false' />" +
                            "    <filter type='and'>" +
                            "      <condition attribute='teamtype' operator='eq' value='0' />" +
                            "    </filter>" +
                            "    <link-entity name='teammembership' from='teamid' to='teamid' visible='false' intersect='true'>" +
                            "      <link-entity name='systemuser' from='systemuserid' to='systemuserid' alias='ab'>" +
                            "        <filter type='and'>" +
                            "          <condition attribute='systemuserid' operator='eq-userid' />" +
                            "        </filter>" +
                            "      </link-entity>" +
                            "    </link-entity>" +
                            "  </entity>" +
                            "</fetch>";
            req1.open("GET", oDataPath1 + "teams?fetchXml=" + encodeURI(strTest), true);
            req1.setRequestHeader("Accept", "application/json");
            req1.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req1.setRequestHeader("OData-MaxVersion", "4.0");
            req1.setRequestHeader("OData-Version", "4.0");
            req1.onreadystatechange = function () {
                if (this.readyState == 4) {
                    // debugger;
                    req1.onreadystatechange = null;
                        if (this.status == 200) {
                            var returnValue = false;
                            var reqResults = JSON.parse(this.response).value;
                            if (reqResults != null && reqResults.length > 0) {
                                for(var i = 0, j = reqResults.length; i < j; i++){
                                    if ("TestTeam" == reqResults[i].name){
                                        returnValue = true;
                                        break;
                                    }
                                }
                            }
    
                            // Call the handler
                            onSuccess && onSuccess(returnValue);
                        }
                        else {
                            var error = JSON.parse(this.response).error;
                            alert(error.message);
                            onError && onError(error);
                        }
                }
            };      
            req1.send();
        }
        catch (e) {
            alert(e);
            onError && onError(e);
        }
    }