How to do it...

  1. Open your AL project in Visual Studio Code.
  2. For the purposes of this recipe, our license will be a simple 10-character string. We need a function that will accept this key, as entered by the user, and put it into our isolated storage so that it is hidden from all other Business Central applications.

In Explorer, select the Television Show Setup Card.al file, and in Editor, add the following function after the OnOpenPage trigger. This function will accept a license string and put it in the isolated storage:

local procedure SetLicenseInStorage(LicenseText: Text[10])
var
SuccessMsg: Label 'License successfully stored';
FailureErr: Label 'License could not be stored';
MustNotBeBlankErr: Label 'You must provide a valid
license';
begin
if LicenseText <> '' then begin
DeleteExistingLicense();
if IsolatedStorage.Set(GetLicenseStorageKey(),
LicenseText, DataScope::Module) then
Message(SuccessMsg)
else
Error(FailureErr);
end else
Error(MustNotBeBlankErr);
end;
  1. Next, we need a function that will retrieve the license from the isolated storage. Add the following function to the page:
local procedure GetLicenseFromStorage()
var
SuccessMsg: Label 'License retrieved: %1';
FailureErr: Label 'License could not be retrieved';
LicenseText: Text;
begin
if IsolatedStorage.Get(GetLicenseStorageKey(),
DataScope::Module, LicenseText) then
Message(SuccessMsg, LicenseText)
else
Error(FailureErr);
end;
  1. Now, add a function to delete the existing license that we have stored:
local procedure DeleteExistingLicense()
var
PromptToDeleteMsg: Label 'There is an existing license stored.
Are you sure you want to replace it?';
begin
if IsolatedStorage.Contains(GetLicenseStorageKey(),
DataScope::Module) then
if Confirm(PromptToDeleteMsg, false) then
IsolatedStorage.Delete(GetLicenseStorageKey(),
DataScope::Module);
end;
  1. Add a function to return the unique identifier that will be used to find and set the license value in the isolated storage:
local procedure GetLicenseStorageKey(): Text
var
LicenseStorageKey: Label 'LIC';
begin
exit(LicenseStorageKey);
end;
Each value that you store in isolated storage needs to have a unique identifier associated with it.
  1. Now, let's add a couple of actions to the page to test our functions. Add the following code to the page object, after the layout section:
actions
{
area(Processing)
{
action(SetLicense)
{
ApplicationArea = All;

trigger OnAction()
var
LicenseInputDialog: page "License Input Dialog";
begin
if LicenseInputDialog.RunModal() = Action::OK then
SetLicenseInStorage(
LicenseInputDialog.GetLicenseText());
end;
}
action(GetLicense)
{
ApplicationArea = All;

trigger OnAction()
begin
GetLicenseFromStorage();
end;
}
}
}
  1. Testing time! Press F5 to build and publish your application:
    1. Using the  icon, search for the Television Shows Setup Card and open it.
    2. Select Actions | SetLicense.
    3. Enter any text string into the dialog box.
    4. Press OK.

You should receive the following message:

Now, let's make sure our license was stored correctly. On the Television Show Setup Card, choose Actions | GetLicense.

You should see a message such as this, showing the license text you entered previously:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset