Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create Enterprise Custom Fields in SharePoint 2013 using CSOM?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 721
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: