Thursday, July 23, 2015

How to read active directory users through x++



Dynamics Ax relies on Active Directory for user authentication.You can use AD for what it is designed for: a central storage location for application data.

But how does one get to read information from the AD?  xAxaptaUserManager and  xAxaptaUserDetails class that helps us to get the AD inforation along with SID In following code snippet, I'll show you how to collect a list of all users from a specific domain, with some basic information about those users.

static void getADUsersInfo(Args _args)

{
    str                 computer = new xSession().clientComputerName();
    xAxaptaUserManager  mgr = new xAxaptaUserManager();
    xAxaptaUserDetails  usr;
    container           domainains;
    int                 d, u;
    str                 domain, login, name, sid, email;
    ;

    // iterate AD domainains

    domainains = mgr.enumeratedomains(computer);
    for (d = 1; d <= conlen(domainains); d++)
    {
        domain = conpeek(domainains, d);
        setprefix(domain);

        // iterate AD domainain users

        usr = mgr.enumerateDomainUsers(domain);
        for (u = 0; u < usr.getUserCount(); u++)
        {
            if (usr.isUserEnabled(u) && !usr.isUserExternal(u))
            {
                // get information from AD
                login = usr.getUserLogin(u);
                name = usr.getUserName(u);
                sid = usr.getUserSid(u);
                email = usr.getUserMail(u);

                // stuff happens here, you can compare AD data with AX User info


                info(strfmt("%1 - %2 - %3 - %4 - %5", domain, login, name, email, sid));

            }
        }
    }
}

Another example of getting SID through X++ using AxaptaUserManager class – getuserId()

Sometimes knowing SID of system is very much helpful specially when the user in the database has been deleted or database is migrated/upgraded incorrectly. Ofcourse, we can get the SID’s easily from the system by opening registry editor and selecting HKEY_USERS in left pane to expand it, in left pane itself you will find the SID of users.But, below is the simple code to get the SID’s of windows users using X++ Code.

static void GetUserSID(Args _args)
{
str windowsUser = ‘companyDomain\\sreenath.reddy'; // DomainName\\userId
#Aif
#File
networkALias alias;
networkDomain domain;
int pos, len;
sid userSid;
Microsoft.Dynamics.IntegrationFramework.Util util;
;
//Split the windowsUser into domain and alias
len = strlen(windowsUser);
pos = strfind(windowsUser, #FilePathDelimiter, 1,len);
domain = substr(windowsUser, 1, pos-1);
alias = substr(windowsUser, pos+1, len – pos);

new InteropPermission(InteropKind::ClrInterop).assert();
// BP Deviation Documented
util = new Microsoft.Dynamics.IntegrationFramework.Util();

//Get the Windows SID for this user
// BP Deviation Documented
userSid = util.GetUserSid(domain, alias);
CodeAccessPermission::revertAssert();

info(userSid);
}

Friday, July 4, 2014

Making an existing table temporary

From a developer's perspective, temporary tables store data in the same way as normal physical tables, except that the data is automatically dropped when no longer required.
They are useful in two common situations
  1. As the datasource for a form or report, where the original data is too complex to be easily queried.
  2. As temporary storage during complicated processing, to hold the results midway through the process.
CustTable  CustTableTmp;
;
CustTableTmp.setTmp();
while select CustTable
{
    CustTableTmp.data(CustTable.data());
    CustTableTmp.doInsert();
}

Select CustTableTmp;

Temporary tables

Displaying a Message Box

Displaying a Message Box


You can display a message in a modal dialog box by using the Box class.
Some methods in the Box class display a dialog box that contains multiple buttons. These methods have a parameter for choosing which button has focus when the dialog box is first displayed. This parameter is of type DialogButtonenum. Most of these multiple-button methods return a value of type DialogButton enum. This returned value enables your program to branch based on which button the user clicked.
NoteNote
The Box application class calls the DialogBox system class. Don't call it directly—always call the Box class instead.

The following are guidelines to help you create an effective message box:
  • Choose a Box method that matches your purpose.
  • Write the information text to match the buttons in the box.
  • Choose the button best suited for having initial focus.
  • Use the returned DialogButton value to direct the branching in your code.
The following table describes the Box class methods and their associated DialogBoxType system enum values.
Box class static method nameAssociatedDialogBoxTypeenum valueDescription
info
InfoBox
The OK button is the only one that the info method displays in the dialog box.
Use this method to display general information messages rather than for warning or error messages.
infoOnce
InfoBox
The infoOnce method is similar to the info method but with the following differences:
  • Your code cannot set the text in the title bar.
  • An additional header section is displayed under the title bar, and you can set the text that displays in the header.
  • More info button is present. When clicked, it opens a Web browser to the URL string that you supply through a parameter.
The infoOnce method returns void, even though it has two buttons.
infoOnceEx
InfoBox
The infoOnceEx method is similar to the infoOnce method but with the following differences:
  • The infoOnceEx method has a parameter that can be passed in to set the text in the title bar.
  • The infoOnceEx method has a Boolean parameter for choosing whether to have the caller thread continue processing while the user reads the message in the dialog box.
okCancel
OkCancelBox
The OK and Cancel buttons are displayed in the dialog box. Call this method when the user must confirm or reject an action.
stop
StopBox
The OK button is the only one displayed in the dialog box.
Use this method to display a message when the user should stop attempting their action or when a process has stopped running.
warning
WarnBox
The OK button is the only one displayed in the dialog box. Use this dialog box for a warning message.
yesAllNoAllCancel
YesToAllNoToAllBox
The buttons displayed in this dialog box are YesYes to allNoNo to all, and Cancel.
yesNo
YesNoBox
The buttons displayed in this dialog box are Yes and No. Call this method when you need the user to make a choice.
yesNoNoAllCancel
NoToAllBox
The buttons displayed in this dialog box are YesNoNo to all, andCancel.
yesNoAxaptaForm
YesNoBox
The yesNoAxaptaForm method is similar to the yesNo method but with the following differences:
  • The yesNoAxaptaForm dialog box has a different image.
  • The yesNoAxaptaForm dialog box is wider.
yesNoCancel
YesNoCancelBox
The buttons displayed in this dialog box are YesNo, and Cancel. Call this method when the yesNo method is insufficient.
yesNoOnce
YesNoBox
The yesNoOnce method is similar to the yesNo method but with the following differences:
  • The yesNoOnce dialog box has a different image.
  • The yesNoOnce dialog box has a check box that the user can select to suppress any further occurrence of an associated message.
yesYesAllNoCancel
YesToAllBox
The YesYes to allNo, and Cancel buttons are displayed.

The following example displays a message box that contains buttons labeled YesNo, and Cancel. The No button has initial focus. When a button is clicked, a message is displayed in the Print Window to indicate which button was clicked. Run this example as a job in the Application Object Tree (AOT).
static void JobBoxDemo(Args _args)
{
    DialogButton diagBut;
    str strMessage = "The No button should have initial focus.";
    str strTitle = "Title";
    ;
    diagBut = Box::yesNoCancel(
        strMessage,
        DialogButton::No, // Initial focus is on the No button.
        strTitle);
    if (diagBut == DialogButton::No)
        {
            print "The No button was clicked.";
        }
    else
        {
            print "The button that was clicked was: ", diagBut;
        }
    pause;
}