Skip to main content
13-Aquamarine
May 28, 2021
Solved

How to create a user with default Name, email, telephone etc programatically?

  • May 28, 2021
  • 2 replies
  • 2269 views

To create a user programatically there is this script:

 

var userParams = {
   password: somePassword,
   name: someUserName,
  description:someDescription
};
Resources["EntityServices"].CreateUser(userParams);

 

 

How to include user fields like Name, email and telephone programmatically?

 

Best answer by yhan

Seems that parameters not defined in the system UserOptions datashape can not be set as an expected input parameter of Create User services (see article CS245936).

But you can use the below workaround instead:

var params = {
 infoTableName : "InfoTable",
 dataShapeName : "UserOptions"
};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(UserOptions)
var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var row = new Object ();
row.userName="testUser2";
var password = Resources["EncryptionServices"].EncryptPropertyValue({data: "Login@thingworx"});
row.password = password; //password
result.AddRow(row);
Resources["EntityServices"].CreateUserWithOptions({
 userOptions: result /* INFOTABLE */
});
Users["testUser2"].emailAddress="abc123@gmail.com";

 That is after the creation of a user, we can set its user extension parameters directly in code.

2 replies

17-Peridot
May 28, 2021

You can use CreateUserWithOptions service instead. Please refer to the community post and article CS329993

13-Aquamarine
May 28, 2021

I am doing something wrong

I created a UserDataShape (copying some user fields from the composer) like this:

EM_9923519_0-1622229346301.png

And the script is:

 

 

var params = {
   infoTableName: "InfoTable",
   dataShapeName: "UserDataShape"
};

var infotable = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);
var newEntry = new Object();
newEntry.userName = 'usrtestscript4';
newEntry.Name = 'testscript4';
newEntry.Description = 'test 4 descr';
var password = Resources["EncryptionServices"].EncryptPropertyValue({data: "Login@12345"});
newEntry.Password = password;
newEntry.fullName = 'full name test 4 ';
newEntry.emailAddress= 'testscript4@site.com';
newEntry.mobilePhone = '12344567';
infotable.AddRow(newEntry);
// no return
Resources["EntityServices"].CreateUserWithOptions({
    userOptions: infotable
});

 

User is cretaed but emailAddress, mobilePhone  etc fields are blank

EM_9923519_1-1622229560733.png

 

What am I missing?

 

 

yhan17-PeridotAnswer
17-Peridot
May 29, 2021

Seems that parameters not defined in the system UserOptions datashape can not be set as an expected input parameter of Create User services (see article CS245936).

But you can use the below workaround instead:

var params = {
 infoTableName : "InfoTable",
 dataShapeName : "UserOptions"
};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(UserOptions)
var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

var row = new Object ();
row.userName="testUser2";
var password = Resources["EncryptionServices"].EncryptPropertyValue({data: "Login@thingworx"});
row.password = password; //password
result.AddRow(row);
Resources["EntityServices"].CreateUserWithOptions({
 userOptions: result /* INFOTABLE */
});
Users["testUser2"].emailAddress="abc123@gmail.com";

 That is after the creation of a user, we can set its user extension parameters directly in code.

5-Regular Member
May 28, 2021

@EM_9923519 

 

If you think previous response on this thread was helpful, please mark that accepted solution for the benefit of other community users.

 

Regards,

Mohit