Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X
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?
Solved! Go to Solution.
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.
You can use CreateUserWithOptions service instead. Please refer to the community post and article CS329993
I am doing something wrong
I created a UserDataShape (copying some user fields from the composer) like this:
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
What am I missing?
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.
I ended using my code with assigning each property as you described
If you think previous response on this thread was helpful, please mark that accepted solution for the benefit of other community users.
Regards,
Mohit