Monday, November 24, 2014

Add Action pane AxActionPaneControl to EP form

To add Action pane with default Save and Close buttons, here are the steps:

Create two web menus in AX
Add following code on ASP.NET script:
<%@ Register Assembly="Microsoft.Dynamics.Framework.Portal, Version=6.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
 Namespace="Microsoft.Dynamics.Framework.Portal.UI.WebControls" TagPrefix="dynamics" %>
<%@ Register Src="AxBaseUserControl.ascx" TagName="AxBaseUserControl" TagPrefix="Ax" %>
<%@ Register Assembly="Microsoft.Dynamics.Framework.Portal.SharePoint, Version=6.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="Microsoft.Dynamics.Framework.Portal.SharePoint.UI.WebControls" TagPrefix="dynamics" %>

dynamics:AxDataSource ID="youDS" runat="server" DataSetName="youDS"
    ProviderView="yourTable">
dynamics:AxDataSource

dynamics:AxActionPaneControl ID="AxActionPaneControlTop" runat="server"
DataMember="yourDS_Current" DataSourceID="yourDS"
WebMenuName="axToolBarInfo" EnableMenuItemHelpText="True" EnableTheming="True"
dynamics:AxActionPaneControl ID="AxActionPaneControlEdit" runat="server" EnableTheming="True"
DataMember="youDS_Current" DataSourceID="youDS" EnableMenuItemHelpText="True" WebMenuName="axToolBarCreate"

Now write following code in C# code behind:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Dynamics.Framework.Portal.UI;
using Microsoft.Dynamics.Framework.Portal.UI.WebControls;
using Microsoft.Dynamics.AX.Framework.Services.Client;
using Microsoft.Dynamics.AX.Framework.Portal.Data;
using Proxy = Microsoft.Dynamics.Framework.BusinessConnector.Proxy;
using ApplicationProxy = Microsoft.Dynamics.Portal.Application.Proxy;
using Microsoft.Dynamics.Framework.BusinessConnector.Adapter;
using Microsoft.Dynamics.Framework.Portal;
using Microsoft.Dynamics.Portal.Application.Proxy;
using Microsoft.Dynamics.Framework.BusinessConnector.Session;
using Microsoft.Dynamics.Framework.Portal.UI.WebControls.WebParts;

public partial class Form1: System.Web.UI.UserControl
{
    protected const string axToolbar_Info = "axtoolbarinfo";
    protected const string axtoolbar_Create = "axtoolbarcreate";
    private const string MENU_ITEM_SaveAndClose = "epsaveandclose";
    private const string MENU_ITEM_Close = "epclose";

    protected void Page_Load(object sender, EventArgs e)
    {
        this.SetupMode();
    }

    void Page_Init(object sender, EventArgs e)
    {
        this.AxActionPaneControlTop.SetMenuItemProperties += new EventHandler(AxActionPaneControlTop_SetMenuItemProperties);
        this.AxActionPaneControlTop.ActionMenuItemClicking += new EventHandler(AxActionPaneControlTop_ActionMenuItemClicking);
        this.AxActionPaneControlTop.ActionMenuItemClicked += new EventHandler(AxActionPaneControl_ActionMenuItemClicked);

        this.AxActionPaneControlEdit.SetMenuItemProperties += new EventHandler(AxActionPaneControlTop_SetMenuItemProperties);
        this.AxActionPaneControlEdit.ActionMenuItemClicking += new EventHandler(AxActionPaneControlTop_ActionMenuItemClicking);
        this.AxActionPaneControlEdit.ActionMenuItemClicked += new EventHandler(AxActionPaneControl_ActionMenuItemClicked);
    }
    ApplicationProxy.EPFormAction FormMode
    {
        get
        {
            return (ApplicationProxy.EPFormAction)Convert.ToInt16(
            this.Page.Request.QueryString.Get("mode")); // This mode is the param set in web menu item url, i.e. mode=1
        }
    }

    private ISession AxSession
    {
        get
        {
            AxBaseWebPart webpart = AxBaseWebPart.GetWebpart(this);
            return webpart == null ? null : webpart.Session;
        }
    }

    private void SetupMode()
    {
        //Proxy.Info objInfoLog = new Proxy.Info(this.AxSession.AxaptaAdapter);
        //objInfoLog.add(Proxy.Exception.Warning, Convert.ToString(this.FormMode));
        switch (this.FormMode)
        {
            case ApplicationProxy.EPFormAction.EditMode:

                this.AXForm.DefaultMode = DetailsViewMode.Edit;
                this.AXForm.AutoGenerateEditButton = true;
                this.AXForm.AutoGenerateInsertButton = false;
             
                break;

            case ApplicationProxy.EPFormAction.CreateMode:
                this.AXForm.DefaultMode = DetailsViewMode.Insert;
                this.AXForm.AutoGenerateEditButton = false;
                //this.AXForm.AutoGenerateInsertButton = true;
                this.AxActionPaneControlTop.Visible = false;
                this.AxActionPaneControlEdit.Visible = true;
               
                break;

            default:
                this.AXForm.DefaultMode = DetailsViewMode.ReadOnly;
                this.AXForm.AutoGenerateEditButton = false;
                this.AXForm.AutoGenerateInsertButton = false;
                this.AxActionPaneControlTop.Visible = true;
                this.AxActionPaneControlEdit.Visible = false;
                break;
        }
    }
 
    void AxActionPaneControlTop_ActionMenuItemClicking(object sender, ActionMenuItemClickingEventArgs e)
    {
        e.RunMenuItem = false;
    }

    void AxActionPaneControl_ActionMenuItemClicked(object sender, ActionMenuItemEventArgs e)
    {

        switch (e.MenuItem.MenuItemAOTName.ToLower())
        {
            case MENU_ITEM_SaveAndClose:
                this.AXForm.InsertItem(true);
                DialogHelper.Close(CloseDialogBehavior.RefreshDataSource);
                break;
            case MENU_ITEM_Close:
                this.AXForm.DoCancel();
                DialogHelper.Close(CloseDialogBehavior.CloseOnly);
                break;
        }
    }

    void AxActionPaneControlTop_SetMenuItemProperties(object sender, SetMenuItemPropertiesEventArgs e)
    {
        string menuItemName = e.MenuItem.MenuItemAOTName.ToLower(System.Globalization.CultureInfo.InvariantCulture);

        switch (menuItemName)
        {
            case MENU_ITEM_Close:
                // Close should not trigger validation
                ((AxActionMenuItem)e.MenuItem).CausesValidation = false;
                break;
            //case MENUITEMNAME_SaveAndClose:
        }
    }
     protected override void OnInit(EventArgs e)
    {
        this.SetupMode();
        base.OnInit(e);
    }
}



No comments: