How to Create User and Assign a Role using X++

Hi Friends,

Recently as per the requirement i have to implement functionality of creation of new active directory user in ax and assign one security role from the third party .net application using AIF integration.

Here is the solution…

For that we have to create a new table and necessary fields which we are going to pass through .net methods.

Then in the Insert method of the table write the below code snippet of x++,

public void insert()

{

SecurityRole role;

SecurityUserRole userRole;

UserInfo userInfo;

xAxaptaUserManager Axmanage;

xAxaptaUserDetails Axdetails;

;

super();

Axmanage = new xAxaptaUserManager();

userInfo.accountType = UserAccountType::ADUser;

userInfo.networkAlias = this.NetworkAlias;

userInfo.networkDomain = this.NetworkDomain;

userInfo.id = this.UserId;

userInfo.name = this.UserName;

userInfo.company = this.CompanyId;

userInfo.enable = this.Enabled;

// To get SID of user, without this user will not be able to login to ax, just user will be shown into user form

Axdetails = Axmanage.getSIDFromName(this.UserId,this.NetworkDomain,UserAccountType::ADUser);

userInfo.sid = Axdetails.getUserSid(0);

userInfo.insert();

select role where role.Name == this.Role;

select * from userRole

where userRole.SecurityRole == role.RecId &&

userRole.User == this.UserId;

userRole.User = this.UserId;

userRole.SecurityRole = role.RecId;

userRole.AssignmentMode = RoleAssignmentMode::Manual;

userRole.AssignmentStatus = RoleAssignmentStatus::Enabled;

SecuritySegregationOfDuties::assignUserToRole(userRole, null);

info(strFmt(“Role %1 added to the user %2 successfully.”, role.Name, this.UserId));

}

Then create one Axd query for the same table and generate AIF document service using AIF service wizard and deploy the service into one Service group and consume it from .net application.

Please let me know if you have any query regarding this.

I hope this will be useful for AIF developers.

Kind Regards,

Vijay Solanki.

1 Like

This is the code snippet of C# for the UserDetailsService which we have created using AIF document service. You can change the name as per your requirement.

public string Create(string NetworkAlias, string Domain, string UserId, string Name, string Company, string Role, string DataAreaId, string LanguageId)

{

try

{

UserDetailsService.UserDetailsServiceClient UserDetailsClient = new UserDetailsService.UserDetailsServiceClient();

UserDetailsService.AxdUserDetails UserDetails = new UserDetailsService.AxdUserDetails();

UserDetailsService.CallContext UserDetailsCtx = new UserDetailsService.CallContext();

UserDetailsService.EntityKey[] UserDetailsKeys;

UserDetailsService.EntityKey UserDetailsKey = new UserDetailsService.EntityKey();

UserDetailsService.KeyField UserDetailsFld = new UserDetailsService.KeyField();

UserDetailsService.AxdEntity_UserDetails UserDetailsTable = new UserDetailsService.AxdEntity_UserDetails();

UserDetailsTable.AccountType = “Active Directory user”;

UserDetailsTable.NetworkAlias = NetworkAlias;

UserDetailsTable.NetworkDomain = Domain;

UserDetailsTable.UserId = UserId;

UserDetailsTable.UserName = Name;

UserDetailsTable.CompanyId = Company;

UserDetailsTable.EnabledSpecified = true;

UserDetailsTable.Enabled = UserDetailsService.AxdExtType_NoYesId.Yes;

UserDetailsTable.Role = Role;

UserDetails.UserDetails = new UserDetailsService.AxdEntity_UserDetails[] { UserDetailsTable };

UserDetailsCtx.Company = DataAreaId;

UserDetailsCtx.Language = LanguageId;

UserDetailsKeys = UserDetailsClient.create(UserDetailsCtx, UserDetails);

UserDetailsKey = UserDetailsKeys[0];

UserDetailsFld = UserDetailsKey.KeyData[0];

return UserDetailsFld.Value;

}

catch (Exception ex)

{

throw ex;

}

}

}

Hi Vijay,

Thanks in advance. I am going to use this code.

Regards,

Janak