You are here

You are here

Find or Create Group and Add Users

This example shows how to work with groups.  It will check if a group exists.  If not it will create it and then adds a user to it.  

Vertical Tabs

c#
var groupResult = _springcm.GroupFind(token,GROUP_NAME, false);
SCMGroup group;
bool userInGroup = false;
 
if (groupResult.Length > 0)
{
    group = groupResult[0];
    if (group.Members.Length > 0)
    {
        foreach(SCMContactInfo member in group.Members)
        {
            if (member.Email == USER_ID)
            {
                userInGroup = true;
                break;
            }
        }
    }
}
else
{
    group = new SCMGroup
                {
                    GroupType = SCMGroupType.Security,
                    IsPrivate = false,
                    Name = GROUP_NAME,
                    ObjectType = SCMObjectType.Group
                };
}
 
if (!userInGroup)
{
    var scmUser = _springcm.ContactFind(token, USER_ID, false)[0];
 
    var membersList = new List<SCMContactInfo>(group.Members) {scmUser};
    group.Members = membersList.ToArray();
 
    _springcm.Save(token, new SCMBaseObject[]{group}, true);
}
java
public static void FindGroupAndAddMembers(SpringCMServiceSoapProxy springService,String token)throws RemoteException{
{
     SCMContactInfo[] users = springService.contactFind(token, USER_ID, false);
 
     //Search for the groups
     SCMGroup[] groups = springService.groupFind(token, GROUP_NAME, false);
 
     if(users.length>0)
     {
          //Loop through the groups
	   for (int i = 0; i < groups.length; i++)
          {
	        //Print the group name
	        SCMGroup group = groups[i];
		 System.out.println(group.getName());
 
		 List<SCMContactInfo> memberList = 
                     new  ArrayList<SCMContactInfo>(Arrays.asList(group.getMembers()));
		 memberList.add(users[0]);
		 group.setMembers(memberList.toArray(new SCMContactInfo[memberList.size()]));
 
		 //Save the new group and check for errors
		 SaveResult saveResult = springService.save(token, new SCMGroup[] { group }, true);
 
		 if (!saveResult.isSuccess())
		 {
		        Error[] errors = saveResult.getErrors();
		        for (int e = 0; e < errors.length; e++)
		        { 
		            System.out.println(errors[e].getMessage());
		        }
		  }
          }
     }
}}
php
 <?php
// Group creation and management 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.
$classMap = array('SCMBaseObject'=> 'BaseObject', 
	           'SCMGroup'=>'Group',
	           'SCMContactInfo'=>'ContactInfo',
	           'SCMUserAccount'=>'UserAccount'); // create the mapping
 
class BaseObject {
	public $Id;
	public $Name;
	public $ObjectType;
}
 
class Group extends BaseObject {
	public $GroupType;
	public $Members;
	public $IsPrivate = false;
}
 
class UserAccount extends BaseObject {
	public $FirstName;
	public $LastName;
	public $Email;
	public $Role;
	public $Status;
}
 
// Authenticate
$springCM = new SoapClient('https://soapna11.springcm.com/atlas/webservices/v201305/springcmservice.asmx?wsdl'
	, array('classmap'=>$classMap)); // pass the class mapping into the SoapClient
 
$authResult = $springCM->AuthenticateNonDefault( array(
	'userName'=>'<your id>',
	'password'=>'<your password>',
	'AccountId'=>'<your account id>',
	'apiKey'=>'<your api key>'
));
// Get the inner object from the SOAP response
$token = $authResult->AuthenticateNonDefaultResult;
$groupName = '<name of the group to update>';
$userEmail = '<email of the user to add>';
 
// Find the group. This will find any group with a name containing the search string.
$groupResult = $springCM->GroupFind( array(
	'token'=>$token,
	'name'=>$groupName,
	'IncludeDeleted'=>false
));
$groupResult = $groupResult->GroupFindResult;
 
// Determine whether to add the user to an existing group, create a new one, or do
// nothing (if the user is already in the group)
$userInGroup = false;
if (property_exists($groupResult, 'SCMGroup'))
{
	$groupObject = $groupResult->SCMGroup;
	// If the group has no members, create an array to hold 
       // the new member we'll add later
	if (!property_exists($groupObject->Members, 'SCMContactInfo'))
	{
		$groupObject->Members->SCMContactInfo = array();
	}
	// If the group has exactly one member, the Members object does not contain an
       // array but a single SCMUserAccount
	// But when we call back to SpringCM, we have to pass an array
	if (sizeof($groupObject->Members->SCMContactInfo) == 1)
	{
		$groupObject->Members->SCMContactInfo = array($groupObject->Members->SCMContactInfo);
	}
	// If the group has members, check to see if the member to be added is already
       // there and set a flag if so
	if (sizeof($groupObject->Members->SCMContactInfo) > 0)
	{
		foreach ($groupObject->Members->SCMContactInfo as $userAccount)
		{
			if ($userAccount->Email == $userEmail)
			{
				$userInGroup = true;
				break;
			}
		}
		unset($userAccount);
	}
}
else
{
	// No group found by the name requested; add new group
	$groupObject = new Group();
	$groupObject->Name = $groupName;
	$groupObject->ObjectType = 'Group';
       // Could also be 'Distribution' if you want a distribution group
	$groupObject->GroupType = 'Security';
	$groupObject->Members = new stdClass();
	$groupObject->Members->SCMContactInfo = array();
}
 
if (!$userInGroup)
{
	// Get additional information about the user to be added
	$userObject = $springCM->ContactFind( array(
		'token'=>$token,
		'SearchText'=>$userEmail,
		'IncludeDeleted'=>false
	));
	// Have to copy the info from ContactFind into 
       // a new object of the type defined above.
	$userObject = $userObject->ContactFindResult->SCMContactInfo;
	$userToAdd = new UserAccount();
	foreach($userObject as $property => $value)
	{
		$userToAdd->$property = $value;
	}
	// Add the new member to the array
	$groupObject->Members->SCMContactInfo[] = $userToAdd;
 
	// Call back to SpringCM with the updated SCMGroup
	$springCM->Save( array(
		'token'=>$token,
		'ToSave'=>array($groupObject),
		'CreateIfNew'=>true
	));
}
?>