关于如何从AL中取得Business Central的用户许可证类型

中国語

各位读者好。
在做定制开发时,可能会碰到要判断用户许可证类型的情况,所以今天我想谈谈如何从AL中获取用户的许可证类型,例如Dynamics 365 Business Central Premium、Dynamics 365 Business Central Essential、Internal Administrator、Dynamics 365 Business Central Team Member等。

首先,让我们看一下这些许可证类型的存储位置。
在Users页面和User Card页面,你可以很容易的找到User Plans FactBox(9826,ListPart)中显示的许可证类型,源表为User Plan(9005)。

但是请注意这个表无法直接打开。

You do not have the following permissions on TableData User Plan: Read.

如果您查看标准源代码,您会发现该表的 Access 属性是 Internal。
Access = Internal;

ValueDescription
Public该对象可以被同一模块和引用它的其他模块中的任何其他代码访问。
Internal该对象只能由同一模块中的代码访问,而不能从另一个模块访问。

所以你也不能直接在你的扩展中使用这个表。

‘User Plan’ is inaccessible due to its protection level

可能是出于某些安全原因,Microsoft 不允许用户直接访问 User Plan 表,但微软为了解决这个问题,准备了另一种访问 User Plan 表中数据的方法。

query 774 “Users in Plans”:

您可以通过将 ?query=774 参数添加到 URL 来强制 Business Central 运行该查询,例如在以下示例中:
https://businesscentral.dynamics.com/d8f36038-1f93-4543-affc-5dc92b6ee871/Sandbox?query=774

打开后,你可以看到用户对应的许可证名称。

让我们再来做一个简单的测试。

源代码:

pageextension 50123 MyExtension extends "Customer List"
{
    trigger OnOpenPage()
    var
        UsersInPlans: Query "Users in Plans";
    begin
        UsersInPlans.SetFilter(UsersInPlans.User_Name, UserId);
        UsersInPlans.Open();
        while UsersInPlans.Read() do begin
            Message(UsersInPlans.Plan_Name);
        end;
    end;
}

测试视频: 从 AL 获取当前用户的许可证类型。

接下来,让我们尝试在新页面上显示用户计划列表。

测试视频:

源代码:

table 50123 UserPlan
{
    TableType = Temporary;
    DataClassification = CustomerContent;

    fields
    {
        field(1; "User Security ID"; Guid)
        {
            Caption = 'User Security ID';
            DataClassification = CustomerContent;
        }
        field(2; "Plan ID"; Guid)
        {
            Caption = 'Plan ID';
            DataClassification = CustomerContent;
        }
        field(10; "User Name"; Code[50])
        {
            Caption = 'User Name';
            DataClassification = CustomerContent;
        }
        field(11; "Plan Name"; Text[50])
        {
            Caption = 'Plan Name';
            DataClassification = CustomerContent;
        }
        field(12; State; Option)
        {
            Caption = 'User State';
            OptionCaption = 'Enabled,Disabled';
            OptionMembers = Enabled,Disabled;
            DataClassification = CustomerContent;
        }
    }

    keys
    {
        key(Key1; "Plan ID", "User Security ID")
        {
            Clustered = true;
        }
    }
}

page 50123 UserPlans
{
    Caption = 'User Plans';
    PageType = List;
    UsageCategory = Lists;
    ApplicationArea = All;
    SourceTable = UserPlan;

    layout
    {
        area(Content)
        {
            repeater(Group)
            {
                field("User Security ID"; Rec."User Security ID")
                {
                    ToolTip = 'Specifies the value of the User Security ID field';
                    ApplicationArea = All;
                }
                field("User Name"; Rec."User Name")
                {
                    ToolTip = 'Specifies the value of the User Name field';
                    ApplicationArea = All;
                }
                field("Plan ID"; Rec."Plan ID")
                {
                    ToolTip = 'Specifies the value of the Plan ID field';
                    ApplicationArea = All;
                }
                field("Plan Name"; Rec."Plan Name")
                {
                    ToolTip = 'Specifies the value of the Plan Name field';
                    ApplicationArea = All;
                }
                field(State; Rec.State)
                {
                    ToolTip = 'Specifies the value of the User State field';
                    ApplicationArea = All;
                }

            }
        }
    }
    trigger OnOpenPage()
    var
        UsersInPlans: Query "Users in Plans";
    begin
        if UsersInPlans.Open() then begin
            while UsersInPlans.Read() do begin
                Rec.Init();
                Rec."User Security ID" := UsersInPlans.User_Security_ID;
                Rec."User Name" := UsersInPlans.User_Name;
                Rec."Plan ID" := UsersInPlans.Plan_ID;
                Rec."Plan Name" := UsersInPlans.Plan_Name;
                Rec.State := UsersInPlans.User_State;
                Rec.Insert();
            end;
            UsersInPlans.Close();
            Rec.FindFirst();
        end;
    end;
}

以上

ZHU

コメント

Copied title and URL