Skip to main content
Dynamics 365 Community / Forums / Finance forum / Creating User using X++
Finance forum
Answered

Creating User using X++

editSubscribe (0) ShareShare
ReportReport
Posted on by 92
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();
 
  • Layan Jwei Profile Picture
    Layan Jwei 2,785 Super User on at
    Creating User using X++
    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
  • Verified answer
    ShahzaibAhmed52 Profile Picture
    ShahzaibAhmed52 92 on at
    Creating User using X++
    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;
        }
    }
     
  • ShahzaibAhmed52 Profile Picture
    ShahzaibAhmed52 92 on at
    Creating User using X++
    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
    Layan Jwei Profile Picture
    Layan Jwei 2,785 Super User on at
    Creating User using X++
    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
    ShahzaibAhmed52 92 on at
    Creating User using X++
    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();
     
  • Suggested answer
    Layan Jwei Profile Picture
    Layan Jwei 2,785 Super User on at
    Creating User using X++
    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 

Helpful resources

Quick Links

What Motivates a Super User?

We know many of you visit the Dynamics 365 Community and Power Platform…

Demystifying Copilot with Georg Glantschnig…

Industry experts answer burning questions directly from our amazing Community…

Setting Up Knowledge Sources for Copilot…

Look at how configuring a comprehensive knowledge base is crucial…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,045 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,570 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Product updates

Dynamics 365 release plans