You are here

You are here

Create User

One of the most common business functions is the need to create or update a user.  The following code shows how a user can be programmatically created in the Address Book.

Vertical Tabs

c#
SCMUserAccount scmUserAccount = new SCMUserAccount();
scmUserAccount.Email = "jdoe@springcm.com";
scmUserAccount.Role = SCMRole.FullSubscriber;
scmUserAccount.Status = SCMUserStatus.Active;
scmUserAccount.ObjectType = SCMObjectType.User;
scmUserAccount.FirstName = "John";
scmUserAccount.LastName = "Doe";
scmUserAccount.Country = "UNITED STATES";
scmUserAccount.ProvinceState = "ILLINOIS";
scmUserAccount.PostalCode = "60654";
scmUserAccount.Address1 = "350 North Orleans Street, Suite 950";
scmUserAccount.City = "Chicago";
//ManagedBy can be set, but you must find the Id to set it, see examples with ContactFind
//Persona can be set, but you must find the ID to set it, see examples with GroupFind
 
SaveResult saveResult = springCMService.Save(token, new[] { scmUserAccount }, true);
 
if (!saveResult.Success)
{
    foreach (Error e in saveResult.Errors)
    {
        Console.WriteLine( e.Message);
       //Handle error
    }
}
Console.WriteLine("User created with id: " + saveResult.SavedObjects[0].Id);
java
SCMUserAccountscmUserAccount = new SCMUserAccount();
scmUserAccount.setEmail("jdoe@springcm.com");
scmUserAccount.setRole(SCMRole.FullSubscriber);
scmUserAccount.setStatus(SCMUserStatus.Active);
scmUserAccount.setFirstName("John");
scmUserAccount.setLastName("Doe");
scmUserAccount.setCountry("UNITED STATES");
scmUserAccount.setProvinceState("ILLINOIS");
scmUserAccount.setPostalCode("60654");
scmUserAccount.setAddress1("350 North Orleans Street, Suite 950");
scmUserAccount.setCity("Chicago");
//ManagedBy can be set, but you must find the Id to set it, see examples with ContactFind
//Persona can be set, but you must find the ID to set it, see examples with GroupFind
 
SaveResultsaveResult = springCMService.Save(token, new SCMBaseObject[]{ scmUserAccount }, true);
 
if (!saveResult.isSuccess())
{
    Error[] errors = saveResult.getErrors();
    for (int i = 0; i<errors.length; i++)
    { 
        System.out.println(errors[i].getMessage());
    }        
}
System.out.println("User created with id: " + saveResult.getSavedObjects()[0].getId());
php
<?php
 
// User creation uses the Save method, which allows for creation or alteration of any
// object within SpringCM.
// Since this method takes a SCMBaseObject array, expecting that the actual objects
// passed will inherit SCMBaseObject,
// we have to define PHP classes for the base and derived objects and map them to types
// defined in the WSDL.
 
// create the mapping
$classMap = array('SCMBaseObject'=> 'BaseObject', 'SCMUserAccount'=>'UserAccount'); 
 
// pass the class mapping into the SoapClient
$springCM = new SoapClient('https://soapna11.springcm.com/atlas/webservices/v201305/springcmservice.asmx?wsdl',
	array('classmap'=>$classMap)); 
 
$authResult = $springCM->AuthenticateNonDefault(array('userName' => '<your username>',
	'password'  => '<your password>',
	'AccountId' => '<your account id>',
	'apiKey'    => '<your api key>' ));
 
$token = $authResult->AuthenticateNonDefaultResult;
 
// Create the base class...
class BaseObject
{
	public $Id;
	public $Name;
	public $ObjectType;
}
 
// ...and the derived class.
class UserAccount extends BaseObject
{
	public $FirstName;
	public $LastName;
	public $Email;
	public $Role;
	public $Status;
}
 
// Populate the object to be saved, which is of the derived type
$scmUserAccount = new UserAccount();
$scmUserAccount->FirstName = 'Test';
$scmUserAccount->LastName = 'ScmUser';
$scmUserAccount->Email = 'test.scmuser@example.com';
$scmUserAccount->Role = 'FullSubscriber';
// Status must be provided, but can only be set as inactive via the API.
$scmUserAccount->Status = 'Inactive'; 
$scmSaveResult = $springCM->Save( array('token' => $token, 
                                       'ToSave' => array($scmUserAccount), 
                                       'CreateIfNew' => true) );
?>