C# Example: Create a User
The following example shows how to create a CSM User account using C#.
Run Swagger Code Generation before attempting to modify the code so that you do not receive reference errors.
Copy
using System;
using System.Collections.Generic;
using System.Linq;
using IO.Swagger.Api;
using IO.Swagger.Model;
namespace Trebuchet.WebApi.IntegrationTests.ExamplesForCustomers.Create_an_Object
{
public class CreateUser
{
public void CreateNewUser()
{
//Get an access token using CSM credentials
var serviceApi = new ServiceApi("http://your server/CherwellApi/");
var tokenResponse = serviceApi.ServiceToken("password","your client id", null, "CSDAdmin", "CSDAdmin", null,"Internal");
//Create a new Business Object api object and add the default header
var businessObjectApi = new BusinessObjectApi("http://your server/CherwellApi/");
businessObjectApi.Configuration.AddDefaultHeader("Authorization","Bearer " + tokenResponse.AccessToken);
//Create a new Security api object and add the default header
var securityApi = new SecurityApi("http://your server/CherwellApi/");
securityApi.Configuration.AddDefaultHeader("Authorization","Bearer " + tokenResponse.AccessToken);
//Get the Business Object summary for user info
var businessObjectSummaryUserInfo = businessObjectApi.BusinessObjectGetBusinessObjectSummaryByNameV1("UserInfo");
//Get the Security Group information
var securityGroupResponse = securityApi.SecurityGetSecurityGroupsV1();
//Get the template for userinfo. Use this to get field ids
var templateRequest = new ApiTrebuchetWebApiDataContractsBusinessObjectTemplateRequest
{
BusObId = businessObjectSummaryUserInfo[0].BusObId,
IncludeAll = true
};
var templateResponse = businessObjectApi.BusinessObjectGetBusinessObjectTemplateV1(templateRequest);
//Create the user save request
var userSaveRequest = new ApiTrebuchetWebApiDataContractsUsersUserSaveRequest
{
AccountLocked = false,
BusObId = businessObjectSummaryUserInfo[0].BusObId,
DisplayName = "Test User",
LdapRequired = false,
LoginId = "Test",
NextPasswordResetDate = null,
Password = "P@ssword",
PasswordNeverExpires = true,
SecurityGroupId = securityGroupResponse.SecurityGroups.First(f => f.GroupName == "Admin").GroupId,
UserCannotChangePassword = false,
UserMustChangePasswordAtNextLogin = false,
UserInfoFields = new List<ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem>
{
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "FullName",
Value = "Test User",
FieldId = templateResponse.Fields.First(f => f.Name == "FullName").FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "EmployeeID",
Value = "123456",
FieldId = templateResponse.Fields.First(f => f.Name == "EmployeeID").FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "Comments",
Value = "Created by api",
FieldId = templateResponse.Fields.First(f => f.Name == "Comments").FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "Department",
Value = "IT",
FieldId = templateResponse.Fields.First(f => f.Name == "Department").FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "Office",
Value = "Colorado Springs",
FieldId = templateResponse.Fields.First(f => f.Name == "Office").FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "Phone",
Value = "719-777-7777",
FieldId = templateResponse.Fields.First(f => f.Name == "Phone").FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "CellPhone",
Value = "719-777-7778",
FieldId = templateResponse.Fields.First(f => f.Name == "CellPhone").FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "FirstName",
Value = "Test",
FieldId = templateResponse.Fields.First(f => f.Name == "FirstName").FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "MiddleInitial",
Value = "C",
FieldId =
templateResponse.Fields.First(f => f.Name == "MiddleInitial").FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "LastName",
Value = "User",
FieldId = templateResponse.Fields.First(f => f.Name == "LastName").FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "HomePhone",
Value = "719-777-7779",
FieldId = templateResponse.Fields.First(f => f.Name == "HomePhone").FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "Address",
Value = "1234 Cherwell Ave",
FieldId = templateResponse.Fields.First(f => f.Name == "Address").FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "City",
Value = "Colorado Springs",
FieldId = templateResponse.Fields.First(f => f.Name == "City").FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "ProvinceStateName",
Value = "CO",
FieldId =
templateResponse.Fields.First(f => f.Name == "ProvinceStateName")
.FieldId
},
new ApiTrebuchetWebApiDataContractsBusinessObjectFieldTemplateItem
{
Dirty = true,
Name = "PostalCodeZip",
Value = "80132",
FieldId =
templateResponse.Fields.First(f => f.Name == "PostalCodeZip").FieldId
}
}
};
//Create the user
var usersApi = new UsersApi("http://your server/CherwellApi/");
usersApi.Configuration.AddDefaultHeader("Authorization","Bearer " + tokenResponse.AccessToken);
try
{
usersApi.UsersSaveUserV1(userSaveRequest);
}
catch (Exception ex)
{
Assert.Fail(ex.Message);
}
}
}
}