天天看點

CRM V9.1——NavigaiteTo

Dynamics 365 on-premises V9.1 NavigaiteTo

    • 打開對話框
    • 關閉層
    • 擷取傳參
    • 重新整理父頁面資料
    • 擷取父頁面formContext
    • 完整示例

打開對話框

詳情參考微軟官方文檔navigateTo (Client API reference)

此文章介紹其中一種,點選窗體按鈕,打開webresource。

在command裡添加js操作,執行以下js。

var pageInput = {
    pageType: "webresource",
    webresourceName: "tec_TaskApprove.html",
    data: "typename=" + Xrm.Page.data.entity.getEntityName() + "&entityid=" + Xrm.Page.data.entity.getId().replace("{", "").replace("}","")
};
var navigationOptions = {
    target: 2,
    width: 400, // value specified in pixel
    height: 400, // value specified in pixel
    position: 1
};
Xrm.Navigation.navigateTo(pageInput, navigationOptions).then(
    function success() {
        // Run code on success
    },
    function error() {
        // Handle errors
    }
);

           

參數介紹:

  • pageType: String Specify “webresource”.
  • webresourceName String The name of the web resource to load.
  • data String (Optional) The data to pass to the web resource.

關閉層

微軟官方目前未提供關閉dialog的方法,隻能通過操作DOM執行click事件。

擷取傳參

将參數名稱傳入此方法即可

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = decodeURIComponent(vars[i].split("=")[1]).split('&');
        for (var i in pair) {
            if (pair[i].indexOf(variable) != -1) {
                return pair[i].split('=')[1];
            }
        }
        console.log(pair);
    }
    return (false);
}
           

重新整理父頁面資料

比如在dialog中操作修改了父頁面的資料,那如何自動重新整理呢?直接調用Xrm.Page.data.refresh();是不行的,既然在dialog中調用不到,那可不可以操作parent的方法呢,答案是可以的。

首先在父級頁面加入一個方法,方式可以自定義一個JS,在窗體庫中添加這個JS。

CRM V9.1——NavigaiteTo

這時,在dialog中直接調用即可。

擷取父頁面formContext

因data隻能傳string類型,把上下文傳到Dialog肯定是不行的。不過也是可以通過調用父級頁面方法來擷取。

Contoso.CustomerPlan = (function () {
    return {
        OnLoad: function (ExecutionContext) {
            var formContext = ExecutionContext.getFormContext();
            Contoso.CustomerPlan.formContext = formContext;
        }
    }
})();
 
parent.getContext = function () {
    return Contoso.CustomerPlan.formContext;
}
           

接收:直接在html頁面調用此方法就行。

完整示例

HTML

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>
    <script type="text/javascript">
        function ClickFun() {
            debugger;
            //實體ID
            var entityid = getQueryVariable("entityid");
            //意見
            var opinion = document.getElementById('value').value;

            var entity = {};
            entity.tec_approveopinion = opinion;

            var req = new XMLHttpRequest();
            req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/tec_tasks(" + entityid + ")", false);
            req.setRequestHeader("OData-MaxVersion", "4.0");
            req.setRequestHeader("OData-Version", "4.0");
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.onreadystatechange = function () {
                if (this.readyState === 4) {
                    req.onreadystatechange = null;
                    if (this.status === 204) {
                        Xrm.Utility.alertDialog("審批成功", function refershFun() {
                            debugger;
                            parent.shuaxin();
                            //關閉層
                            parent.$("button[data-id='dialogCloseIconButton']", parent.document).click();
                            //parent.Xrm.Page.data.refresh();
                        });
                    } else {
                        Xrm.Utility.alertDialog(this.statusText);
                    }
                }
            };
            req.send(JSON.stringify(entity));
        }
        //擷取傳參
        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = decodeURIComponent(vars[i].split("=")[1]).split('&');
                for (var i in pair) {
                    if (pair[i].indexOf(variable) != -1) {
                        return pair[i].split('=')[1];
                    }
                }
                console.log(pair);
            }
            return (false);
        }
    </script>
</head>
<body style="margin-left:100px">
    <h3>審批意見</h3>
    <input id="value" type="text" name="name" value="" placeholder="請輸入審批意見" /><br /><br />
    <button onclick="ClickFun()">審批</button>
</body>

</html>
           

Ribbon

function approveFun()
{
    var pageInput = {
        pageType: "webresource",
        webresourceName: "tec_TaskApprove.html",
        data: "typename=" + Xrm.Page.data.entity.getEntityName() + "&entityid=" + Xrm.Page.data.entity.getId().replace("{", "").replace("}","")
    };
    var navigationOptions = {
        target: 2,
        width: 400, // value specified in pixel
        height: 400, // value specified in pixel
        position: 1
    };
    Xrm.Navigation.navigateTo(pageInput, navigationOptions).then(
        function success() {
            // Run code on success
        },
        function error() {
            // Handle errors
        }
    );
}

function approveButtonDisplay()
{
    return true;
}
           

窗體JS

parent.shuaxin = function () { Xrm.Page.data.refresh(); };


Contoso.CustomerPlan = (function () {
    return {
        OnLoad: function (ExecutionContext) {
            var formContext = ExecutionContext.getFormContext();
            Contoso.CustomerPlan.formContext = formContext;
        }
    }
})();
 
parent.getContext = function () {
    return Contoso.CustomerPlan.formContext;
}
           

繼續閱讀