Lookup tables is used in Enterprise Custom Fields. We can create Lookup tables programmatically either from CSOM or PSI. Here below is the example of creating Lookup tables using CSOM.
Microsoft.ProjectServer.Client.LookupTableCreationInformation lookupTableInfo = new Microsoft.ProjectServer.Client.LookupTableCreationInformation();
try
{
lookupTableInfo.Id = new Guid();
lookupTableInfo.Name = "Applications";
lookupTableInfo.SortOrder = Microsoft.ProjectServer.Client.LookupTableSortOrder.UserDefined;
#region Creating Lookup Mask
List<Microsoft.ProjectServer.Client.LookupMask> lstMask = new List<Microsoft.ProjectServer.Client.LookupMask>();
Microsoft.ProjectServer.Client.LookupMask maskInfo = new Microsoft.ProjectServer.Client.LookupMask();
maskInfo.Length =0;
maskInfo.Separator = ".";
maskInfo.MaskType = Microsoft.ProjectServer.Client.LookupTableMaskSequence.CHARACTERS;
lstMask.Add(maskInfo);// you can create more than one mask
lookupTableInfo.Masks = lstMask;
#endregion
char separator = lstMask[0].Separator.ToCharArray()[0];
#region Creating LookupTable Entry
Dictionary<string, Guid> AvailableLookupEntry = new Dictionary<string, Guid>();
List<Microsoft.ProjectServer.Client.LookupEntryCreationInformation> lstEntry = new List<Microsoft.ProjectServer.Client.LookupEntryCreationInformation>();
string[] multiLevel = "Your Full Value".Split(separator);
int length = multiLevel.Length;
AvailableLookupEntry.Add( "Your Full Value", new Guid());
string fullValue = multiLevel[length - 1];
StringBuilder sb = new StringBuilder();
if (length > 1)
{
for (int i = 0; i < (length - 1); i++)
{
sb.AppendFormat("{0}{1}", multiLevel[i], separator);
}
sb.Length--;
}
Microsoft.ProjectServer.Client.LookupEntryCreationInformation lookupEntryInfo = new Microsoft.ProjectServer.Client.LookupEntryCreationInformation();
lookupEntryInfo.Id = new Guid();
lookupEntryInfo.Description = "Lookup Entry Description";
lookupEntryInfo.SortIndex = Convert.ToDecimal("1.0000000000");
if (AvailableLookupEntry.ContainsKey(sb.ToString()))
lookupEntryInfo.ParentId = AvailableLookupEntry[sb.ToString()];
lookupEntryInfo.Value = new Microsoft.ProjectServer.Client.LookupEntryValue();
lookupEntryInfo.Value.TextValue = fullValue;
lstEntry.Add(lookupEntryInfo);
lookupTableInfo.Entries = lstEntry;
#endregion
Microsoft.ProjectServer.Client.LookupTable newLookupTable = projectContext.LookupTables.Add(lookupTableInfo);
projectContext.LookupTables.Update();
projectContext.ExecuteQuery();
}
catch (Exception ex)
{
//log exception
}
Hope this code will help you. Thanks
0 Comment(s)