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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Creating User using X++

(0) ShareShare
ReportReport
Posted on by 148
Hi,
 
I am trying to create users using the following code. The issue that I am facing is that when I try to login to the system it gives me an error that I cannot log in with my credentials. But when I go to the user form and click edit and without changing anything press the save button then the user works fine.
 
Could you please help me find out what am I missing in the below Code?
 
UserInfo userInfo;xAxaptaUserManager Axmanage;xAxaptaUserDetails Axdetails;Boolean ret = false;Axmanage = new xAxaptaUserManager();userInfo.initValue();userInfo.accountType = UserAccountType::ClaimsUser;userInfo.networkAlias = networkAlias;userInfo.networkDomain = networkDomain ;userInfo.id = userId;userInfo.name = userName;userInfo.company = defaultCompany;userInfo.enable = NoYes::Yes;userInfo.helplanguage = 'en-Us';userInfo.language = 'en-Us';Axdetails = Axmanage.getSIDFromName(userInfo.id, userInfo.NetworkDomain, UserAccountType::ClaimsUser);userInfo.sid = Axdetails.getUserSid(0);userInfo.ObjectId = SysSecurity::getUserObjectIdForPrincipalName(userInfo.NetworkAlias);userInfo.insert();
 
I have the same question (0)
  • Suggested answer
    Layan Jwei Profile Picture
    8,118 Super User 2025 Season 2 on at
    Hi Shahzaib,

    can you please repost your code as it's not well formatted  -- there is a bug in the dynamics community where code written in the question itself is not well formatted. However, in comments section it appears properly.

    But a quick guess would be, did you check if the SID field was empty before you edited the user via the form and if it got filled after you edited?
     
    In general, you can check all fields from SQL after inserting, then edit the user from the form and check the values of the fields again and see the difference. -- after that you should know what is missing

    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future 
  • ShahzaibAhmed52 Profile Picture
    148 on at
    Here is the formatted code. 
    Yes SID was created before I manually edited the user.
     
    UserInfo userInfo;
    xAxaptaUserManager Axmanage;
    xAxaptaUserDetails Axdetails;
    Boolean ret = false;
    Axmanage = new xAxaptaUserManager();
    
    userInfo.initValue();
    userInfo.accountType = UserAccountType::ClaimsUser;
    userInfo.networkAlias = networkAlias;
    userInfo.networkDomain = networkDomain ;
    userInfo.id = userId;
    userInfo.name = userName;
    userInfo.company = defaultCompany;userInfo.enable = NoYes::Yes;
    userInfo.helplanguage = 'en-Us';
    userInfo.language = 'en-Us';
    
    Axdetails = Axmanage.getSIDFromName(userInfo.id, userInfo.NetworkDomain, UserAccountType::ClaimsUser);
    userInfo.sid = Axdetails.getUserSid(0);
    userInfo.ObjectId = SysSecurity::getUserObjectIdForPrincipalName(userInfo.NetworkAlias);
    
    userInfo.insert();
     
  • Verified answer
    Layan Jwei Profile Picture
    8,118 Super User 2025 Season 2 on at
    Hi Shahzaib,
     
    Did you go through my suggestion? which is to look at "user info" field values when inserting by code, then see what gets updated when you edit the form?

    Looking at the code you provided, I can see that you are not filling "identityProvider" field -- try filling that then let me know what happens, if it didn't fix it, then please go through my suggestion.

    Thanks,
    Layan Jweihan
    Please mark this answer as "Verified" if it solved your issue. In order to help others who will face a similar issue in the future 
  • ShahzaibAhmed52 Profile Picture
    148 on at
    Hi Layan,
     
    I check the difference before and after manually editing the user from form. 
     
    I found two differences.
     
    1. "identityProvider" was not populated. (Which I fixed)
    2. "SID" is updating 

    What may be the reason for "SID" beign chaged after I manually edit the record?
    Could you also suggest a fix on this?
    I searched the whole internet but the solutions I found didn't worked.
     
  • Verified answer
    ShahzaibAhmed52 Profile Picture
    148 on at
    Hi Layan,
     
    Thank you for your guidance.
    My issue is resolved the final version of the code that worked for me is below, I hope it will be helpful for others as well.
     
    I was hard coding the "networkDomain" and "identityProvider". when I used "EnvironmentFactory" class to generate "networkDomain", it worked.
     
    using Microsoft.Dynamics.ApplicationPlatform.Environment;
    using Microsoft.Dynamics.ApplicationPlatform.XppServices.Instrumentation;
    using Microsoft.Dynamics.AX.Security.AuthenticationCommon;
    using Microsoft.Dynamics.Ax.Xpp.AxShared.Instrumentation;
    using XppLogger = Microsoft.Dynamics.ServiceFramework.Instrumentation.Xpp;
    using Microsoft.Extensions.Logging;
    
    class userCreationClass
    {
    	public str createUser()
        {
            UserInfo userInfo;
            xAxaptaUserManager Axmanage;
            xAxaptaUserDetails Axdetails;
    
            container    packedList;
            ListIterator iterator;
            boolean      ret = false;
    
            Axmanage = new xAxaptaUserManager();
    
            list split = strSplit(worker.email(), "@");
            iterator = new ListIterator(split);
            while(iterator.more())
            {
                packedList += iterator.value();
                iterator.next();
            }
    
            userInfo.initValue();
            userInfo.accountType = UserAccountType::ClaimsUser;
            userInfo.networkAlias = worker.email();
            userInfo.networkDomain = EnvironmentFactory::GetApplicationEnvironment().get_Provisioning().get_AdminIdentityProvider();
            userInfo.IdentityProvider = Microsoft.Dynamics.AX.Security.AuthenticationCommon.AadHelper::GetCanonicalIdentityProvider(UserInfo.networkDomain);;
            userInfo.id = conPeek(packedList, 1);
            userInfo.name = worker.name();
            userInfo.company = defaultCompany;
            userInfo.enable = NoYes::Yes;
            userInfo.helplanguage = 'en-Us';
            userInfo.language = 'en-Us';
    
            Axdetails = Axmanage.getSIDFromName(UserInfo.networkAlias, UserInfo.IdentityProvider, UserInfo.accountType);
            userInfo.sid = Axdetails.getUserSid(0);
    
            userInfo.ObjectId = SysSecurity::getUserObjectIdForPrincipalName(userInfo.NetworkAlias);
    
            userInfo.insert();
    
            return userInfo.id;
        }
    }
     
  • Layan Jwei Profile Picture
    8,118 Super User 2025 Season 2 on at
    Hi Shahzaib,
     
    I'm glad it worked. In this case, you can mark the two answers as verified. (Your answer and my suggestion to fill identityProvider) so that others know what was helpful.
     
    Under each answer, you can find a tick box that says "Does this answer your question" -- you can click it for the two answers.
     
    **Btw listEnumerator is better to use than listIterator.
     
    Thanks,
    Layan Jweihan

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 451 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 428 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans