Enterprise Custom Fields includes custom formula and lookup tables. We can create CustomField programmatically either from CSOM or PSI. Here below is the example of creating CustomField using CSOM.
Microsoft.ProjectServer.Client.CustomFieldCreationInformation customFieldInfo = null;
try
{
projectContext.Load(projectContext.LookupTables,
lts => lts.Include(
lt => lt.AppAlternateId,
lt => lt.Id,
lt => lt.Name,
lt => lt.FieldType,
lt => lt.SortOrder,
lt => lt.Masks,
lt => lt.Entries
));
projectContext.ExecuteQuery();
Dictionary<Guid, Microsoft.ProjectServer.Client.LookupTable> availableLookupTables = new Dictionary<Guid, Microsoft.ProjectServer.Client.LookupTable>();
foreach (Microsoft.ProjectServer.Client.LookupTable lt in projectContext.LookupTables)
{
availableLookupTables.Add(lt.Id, lt);
}
#region CustomField Creation
customFieldInfo = new Microsoft.ProjectServer.Client.CustomFieldCreationInformation();
customFieldInfo.Description = "Test Description";
customFieldInfo.EntityType = projectContext.EntityTypes.AssignmentEntity;
customFieldInfo.FieldType = Microsoft.ProjectServer.Client.CustomFieldType.DATE;
customFieldInfo.Formula = "Your custom field formula";
customFieldInfo.Id = new Guid();
customFieldInfo.IsEditableInVisibility = false;
customFieldInfo.IsMultilineText = false;
customFieldInfo.IsRequired =false;
customFieldInfo.IsWorkflowControlled =false;
customFieldInfo.LookupAllowMultiSelect = false;
customFieldInfo.LookupDefaultValue = new Guid();
Guid lookupTableId = new Guid("// Available look up Id");
if (availableLookupTables.ContainsKey(lookupTableId))
customFieldInfo.LookupTable = availableLookupTables[lookupTableId];
customFieldInfo.Name ="Custom field Name";
Microsoft.ProjectServer.Client.CustomField newCustomField = projectContext.CustomFields.Add(customFieldInfo);
newCustomField.RollsDownToAssignments = false;
newCustomField.RollupType = Microsoft.ProjectServer.Client.CustomFieldRollupType.Formula;
projectContext.CustomFields.Update();
projectContext.ExecuteQuery();
#endregion
}
catch (Exception ex)
{
//log exception
}
Hope this code will help you.Thanks
0 Comment(s)