web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / i-am-ax-pankaj / DS_ExportAllUsersandSecurit...

DS_ExportAllUsersandSecurityRoles AX 2012 Security roles export to Excel

I AM AX PANKAJ Profile Picture I AM AX PANKAJ 419

void DS_ExportAllUsersandSecurityRoles()
{
UserId usrid,prevusrid;
str 5000 rolename;
container c;
CompanyInfo companyInfo;
SecurityUserRole SecurityUserRole;
SecurityRole SecurityRole;
OMUserRoleOrganization oMUserRoleOrganization;
SysUserInfo sysuserinfo;
SysExcelApplication xlsApplication;
SysExcelWorkBooks xlsWorkBookCollection;
SysExcelWorkBook xlsWorkBook;
SysExcelWorkSheets xlsWorkSheetCollection;
SysExcelWorkSheet xlsWorkSheet;
SysExcelRange xlsRange;
int row = 1;
str fileName;
str 1000 Rname;
LabelId Rlabel;
;
//Filename
fileName = "D:\\AllUsersSecirityRolesnew.xlsx";
//Initialize Excel instance
xlsApplication = SysExcelApplication::construct();
//Open Excel document
//xlsApplication.visible(true);
//Create Excel WorkBook and WorkSheet
xlsWorkBookCollection = xlsApplication.workbooks();
xlsWorkBook = xlsWorkBookCollection.add();
xlsWorkSheetCollection = xlsWorkBook.worksheets();
xlsWorkSheet = xlsWorkSheetCollection.itemFromNum(1);
//Excel columns captions
xlsWorkSheet.cells().item(row,1).value("UserId");
xlsWorkSheet.cells().item(row,2).value("Security Role");
row++;
xlsWorkSheet.cells().item(row,1).value(' ');
xlsWorkSheet.cells().item(row,2).value(' ');
row++;

startLengthyOperation();

while select sysuserinfo
{
usrId = sysuserinfo.id;

//new code
while select * from SecurityUserRole
where SecurityUserRole.User == usrId
join SecurityRole
where SecurityRole.RecId == SecurityUserRole.SecurityRole
{
if(SecurityUserRole)
{
if(rolename == '')
{
Rlabel = SecurityRole.Name;
Rname = SysLabel::labelId2String2(Rlabel,'en-us');
rolename = Rname;
}
else
{
Rlabel = SecurityRole.Name;
Rname = SysLabel::labelId2String2(Rlabel,'en-us');
rolename = rolename + ', ' + Rname;
}
}
prevusrid=usrid;
}

if(!SecurityUserRole && prevusrid!='')
{
xlsWorkSheet.cells().item(row,1).value(usrId);
xlsWorkSheet.cells().item(row,2).value(rolename);
row++;
usrId = '';
}

SecurityUserRole.clear();
prevusrid = '';
rolename = '';
}// main while

//Check whether the document already exists
if(WinApi::fileExists(fileName))
WinApi::deleteFile(fileName);
//Save Excel document
xlsWorkbook.saveAs(fileName);
//Open Excel document
xlsApplication.visible(true);
//Close Excel
xlsApplication.quit();
xlsApplication.finalize();

info("Done");
}

Comments

*This post is locked for comments