Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Suggested answer

How to export permissions from AX

Posted on by 50

Hello, 


I need to export permissions (Table and security keys) from AX2009 in an Excel file or csv file. 

How can I manage this ? 

Thanks a lot. 

  • mimi51340 Profile Picture
    mimi51340 50 on at
    RE: How to export permissions from AX

    Hello,

    no I don't want to export a report. In fact the first part of code is extracted from an existing report that contains the data I want to get into my csv file.

    The second part of code is the code used to generate the csv file from a query.

    But my question is: how to format in the second part of code (where you put your example: line = [inventTable.ItemId,inventTable.ItemName];) to be able to handle the extraction as it was done in the report (first part of code).

    Is it clearer?

    Thanks for your help

  • GanapathyS Profile Picture
    GanapathyS 75 on at
    RE: How to export permissions from AX

    Hello ,

    Sorry , I haven't worked on exporting a report from AX directly to CSV file, But why are you trying exporting a report where as you already know the table in which the data is stored and also have the code to export them from table.

    public static void main(Args _args)
    {
        Commaio file;
        container line;
        InventTable inventTable;
        #define.filename("C:\\Items.csv")
        #File
    
        ;
        file = new Commaio(#filename , #io_write); or  file = new Commaio(#filename , 'W');
        //file.outFieldDelimiter(';');
        if( !file || file.status() != IO_Status::Ok)
        {
            throw error("File Cannot be opened");
        }
        while select inventTable
        {
            line = [inventTable.ItemId,inventTable.ItemName];
            file.writeExp(line);
        }
    }

    Thanks,

  • mimi51340 Profile Picture
    mimi51340 50 on at
    RE: How to export permissions from AX

    Hello, 

    It does not work. I get an error: 

    pastedimage1646646476237v1.png

    But I found a report in AX that does the same thing I would like to get. But I would like to output it as a csv. 

    Currently this code is used to extract the data. 

    static container extractData(FormTreeControl tree, boolean isMenu)
    {
        List                    textlist    = new List(Types::String);
        List                    accesslist  = new List(Types::Enum);
        ImageListAppl_Security  imagelist   = new ImageListAppl_Security();
        str                     text;
        SysAccessRights         access;
    #ResAppl
    
    void doIt(int idx, int layer)
    {
        FormTreeItem    item         = tree.getItem(idx);
        TmpSecurityTree securityItem;
        int             image;
    
        while (item)
        {
            securityItem = item.data().lookup(0);
            text = strrep(' ', layer*7) item.text();
            image = item.overlayImage();
            switch (image)
            {
                case imagelist.image(#ImageNoAccess):
                    access = SysAccessRights::NoAccess;
                    break;
                case imagelist.image(#ImageView):
                    access = SysAccessRights::View;
                    break;
                case imagelist.image(#ImageEdit):
                    access = SysAccessRights::Edit;
                    break;
                case imagelist.image(#ImageAdd):
                    access = SysAccessRights::Add;
                    break;
                case imagelist.image(#ImageFullControl):
                    access = SysAccessRights::FullAccess;
                    break;
                default:
                    if (isMenu)
                    {
                        if (item.stateBold())
                            access = SysAccessRights::FullAccess;
                        else
                            access = SysAccessRights::NoAccess;
                    }
                    else
                    {
                        access = SysAccessRights::FullAccess;
                    }
                    break;
    
            }
            textlist.addEnd(text);
            accesslist.addEnd(access);
    
            if ((securityItem.type == SysSecurityType::SecurityKey && item.stateImage() == imagelist.image(#ImageCheckSome)) ||
                access != SysAccessRights::NoAccess)
            {
                doIt(tree.getChild(item.idx()), layer 1);
            }
            item = tree.getItem(tree.getNextSibling(item.idx()));
        }
    }
        doit(tree.getRoot(), 0);
    
        return [textlist.pack(), accesslist.pack()];
    }
    

    I am modifying the code to get something like this : 

    static container extractData(FormTreeControl tree, boolean isMenu)
    {
        SysOperationProgress progression = new SysOperationProgress();
        List                    textlist    = new List(Types::String);
        List                    accesslist  = new List(Types::Enum);
        ImageListAppl_Security  imagelist   = new ImageListAppl_Security();
        str                     text;
        SysAccessRights         access;
    #ResAppl
    
    ;
    
            CodeAccessPermission::revertAssert();
            permission= new FileIoPermission(_fileName,#io_write);
            permission.assert();
    
            commaIO = new AsciiIO(_fileName, #io_write);
    
            if (!commaIO)
            {
               throw error("@SYS104602");
            }
    
            if(commaIo)
            {
                switch(delimiter)
                {
                    case FileDelimiter::Comma :
                       commaIo.outFieldDelimiter(#delimiterComma);
                       break;
                    case FileDelimiter::SemiColumn :
                       commaIo.outFieldDelimiter(#delimiterSemiColumn);
                       break;
                    case FileDelimiter::Space :
                        commaIo.outFieldDelimiter(#delimiterSpace);
                        break;
                    case FileDelimiter::Tabulation :
                        commaIo.outFieldDelimiter(#delimiterTab);
                        break;
                    case Filedelimiter::Pipe :
                        commaIo.outFieldDelimiter(#delimiterPipe);
                        break;
                 }
            }
    
        if(checkWriteHeader)
        {
            this.WriteHeader();
        }
    
        while (queryRun.next())
        {
            /*line = [
                        enum2Str(AccessRecordType),             // Type d'objet
                        accessObjectName,                       // Le nom de l'objet
                        accessFieldName,                        // Le nom du champs si l'objet est de type Table, sinon vide
                        userInfo.name,                          // Le nom utilisateur
                        enum2str(accessType)                    // Le type d'accès
                       ];*/
                       
                       
            //On écrit la ligne dans le fichier
            commaIO.writeExp(line);
            //On incrément le nombre de lignes écrites
            i2  ;
    
            progression.setCaption(this.caption());
            progression.setAnimation(#AviUpdate);
            progression.setText(strfmt("@SYS73233"   " : %1", i2));
        }

    But the question is, how to replace the "line" section with the result of the "doIt" method ? 

    Thanks a lot. 

  • GanapathyS Profile Picture
    GanapathyS 75 on at
    RE: How to export permissions from AX

    Hello mimi51340,

    They are just font attributes you can comment them too it wont affect the logic.

    Thanks.

  • mimi51340 Profile Picture
    mimi51340 50 on at
    RE: How to export permissions from AX

    Hello,

    Now I got an error on the line font.bold(true).

    Object not initialized.

    Thanks

  • GanapathyS Profile Picture
    GanapathyS 75 on at
    RE: How to export permissions from AX

    Hello mimi51340,

    Just comment the lines which has font() function in it.

    Thanks

  • mimi51340 Profile Picture
    mimi51340 50 on at
    RE: How to export permissions from AX

    Hello,

    The SysExcelCell class does not contain the font() function.

    There for example :  font = cell.font();

    Thank you for you help.

  • Suggested answer
    GanapathyS Profile Picture
    GanapathyS 75 on at
    RE: How to export permissions from AX

    static void GS_Export(Args _args)
    {
    SysExcelApplication     application;
    SysExcelWorkbooks       workbooks;
    SysExcelWorkbook        workbook;
    SysExcelWorksheets      worksheets;
    SysExcelWorksheet       worksheet;
    SysExcelCells           cells;
    SysExcelCell            cell;
    SysExcelFont            font;
    int                     row;
    AccessRightsList yourTable;
    
    // intializing classes to export excel
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    workbook = workbooks.add();
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();
    cells.range('A:A').numberFormat('@');
    
    // Setting Header values
    cell = cells.item(1, 1);
    cell.value("HeaderValue1");
    font = cell.font();
    font.bold(true);
    
    cell = cells.item(1, 2);
    cell.value("HeaderValue2");
    font = cell.font();
    font.bold(true);
    
    cell = cells.item(1, 3);
    cell.value("HeaderValue3");
    font = cell.font();
    font.bold(true);
    
    cell = cells.item(1, 4);
    cell.value("HeaderValue4");
    font = cell.font();
    font.bold(true);
    row = 1;
    
    // inserting data row wise
    while select yourTable
    {
    row  ;
    cell = cells.item(row, 1);
    cell.value(yourTable.AccessType);
    cell = cells.item(row, 2);
    cell.value(yourTable.AccessTypeFKeyUse);
    cell = cells.item(row, 3);
    cell.value(yourTable.createdBY);
    cell = cells.item(row, 4);
    cell.value(yourTable.createdDateTime);
    }
    
    application.visible(true);
    
    }

    Hello mimi51340,

    You haven't changed the table name and fields with your required table to export, Try this one.

    Thanks

  • mimi51340 Profile Picture
    mimi51340 50 on at
    RE: How to export permissions from AX

    Hello, 

    With your code I get an errror : The SecurityRole variable has not been declared.

    static void GS_Export(Args _args)
    
    {
    
        SysExcelApplication     application;
    
        SysExcelWorkbooks       workbooks;
    
        SysExcelWorkbook        workbook;
    
        SysExcelWorksheets      worksheets;
    
        SysExcelWorksheet       worksheet;
    
        SysExcelCells           cells;
    
        SysExcelCell            cell;
    
        SysExcelFont            font;
    
        int                     row;
    
        SecurityRole            AccessRightsList;
        ;
    
        // intializing classes to export excel
    
        application = SysExcelApplication::construct();
    
        workbooks = application.workbooks();
    
        workbook = workbooks.add();
    
        worksheets = workbook.worksheets();
    
        worksheet = worksheets.itemFromNum(1);
    
        cells = worksheet.cells();
    
        cells.range('A:A').numberFormat('@');
    
        // Setting Header value
    
    
        cell = cells.item(1, 1);
    
        cell.value("HeaderValue1");
    
        font = cell.font();
    
        font.bold(true);
    
        cell = cells.item(1, 2);
    
        cell.value("HeaderValue2");
    
        font = cell.font();
    
        font.bold(true);
    
        cell = cells.item(1, 3);
    
        cell.value("HeaderValue3");
    
        font = cell.font();
    
        font.bold(true);
    
        cell = cells.item(1, 4);
    
        cell.value("HeaderValue4");
    
        font = cell.font();
    
        font.bold(true);
    
        row = 1;
    
        // inserting data row wise
    
        while select AccessRightsList
    
        {
    
            row  ;
    
            cell = cells.item(row, 1);
    
            cell.value(AccessRightsList.AllowCurrentRecords);
    
            cell = cells.item(row, 2);
    
            cell.value(AccessRightsList.AllowFutureRecords);
    
            cell = cells.item(row, 3);
    
            cell.value(AccessRightsList.AllowPastRecords);
    
            cell = cells.item(row, 4);
    
            cell.value(AccessRightsList.AotName);
    
        }
    
        application.visible(true);
    
    }

    Thanks. 

  • Suggested answer
    GanapathyS Profile Picture
    GanapathyS 75 on at
    RE: How to export permissions from AX

    Hello mimi51340,

    Try running this code on your machine by changing only the table name and fields in it, this did exported the system documentation table to an excel for me.

    static void GS_Export(Args _args)

    {

    SysExcelApplication     application;

    SysExcelWorkbooks       workbooks;

    SysExcelWorkbook        workbook;

    SysExcelWorksheets      worksheets;

    SysExcelWorksheet       worksheet;

    SysExcelCells           cells;

    SysExcelCell            cell;

    SysExcelFont            font;

    int                     row;

    SecurityRole yourTable;

    // intializing classes to export excel

    application = SysExcelApplication::construct();

    workbooks = application.workbooks();

    workbook = workbooks.add();

    worksheets = workbook.worksheets();

    worksheet = worksheets.itemFromNum(1);

    cells = worksheet.cells();

    cells.range('A:A').numberFormat('@');

    // Setting Header values

    cell = cells.item(1, 1);

    cell.value("HeaderValue1");

    font = cell.font();

    font.bold(true);

    cell = cells.item(1, 2);

    cell.value("HeaderValue2");

    font = cell.font();

    font.bold(true);

    cell = cells.item(1, 3);

    cell.value("HeaderValue3");

    font = cell.font();

    font.bold(true);

    cell = cells.item(1, 4);

    cell.value("HeaderValue4");

    font = cell.font();

    font.bold(true);    

    row = 1;

    // inserting data row wise

    while select yourTable

    {

    row++;

    cell = cells.item(row, 1);

    cell.value(yourTable.AllowCurrentRecords);

    cell = cells.item(row, 2);

    cell.value(yourTable.AllowFutureRecords);

    cell = cells.item(row, 3);

    cell.value(yourTable.AllowPastRecords);

    cell = cells.item(row, 4);

    cell.value(yourTable.AotName);    

    }

    application.visible(true);

    }

    Thanks and regards

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans