Add / Modify Lookup Table Entries using Project Server CSOM

Too bad there doesn’t seem to be actual documentation for this.

private static void CreateLookup(Guid LookupTableGuid)
        {
            LookupTableCreationInformation NewLookupTable = new LookupTableCreationInformation();
            NewLookupTable.Id = LookupTableGuid;
            NewLookupTable.Name = "NewLookupTable";
            NewLookupTable.SortOrder = LookupTableSortOrder.Ascending;

            LookupMask mask = new LookupMask();
            mask.Length = 2;
            mask.MaskType = LookupTableMaskSequence.CHARACTERS;
            mask.Separator = ".";

            LookupMask mask2 = new LookupMask();
            mask2.Length = 3;
            mask2.MaskType = LookupTableMaskSequence.CHARACTERS;
            mask2.Separator = ".";

            LookupEntryCreationInformation FirstLookupEntry = new LookupEntryCreationInformation();
            FirstLookupEntry.Description = "First Description";
            var id = Guid.NewGuid();
            FirstLookupEntry.Id = id;
            FirstLookupEntry.Value = new LookupEntryValue();
            FirstLookupEntry.Value.TextValue = "aa";

            LookupEntryCreationInformation SecondLookupEntry = new LookupEntryCreationInformation();
            SecondLookupEntry.Value = new LookupEntryValue();
            SecondLookupEntry.Value.TextValue = "bbb";
            //set the parent id = the id of the previous item
            SecondLookupEntry.ParentId = id;
            SecondLookupEntry.Description = "Second Description";
            SecondLookupEntry.Id = Guid.NewGuid();

            List<LookupEntryCreationInformation> ListOfEntries = new List<LookupEntryCreationInformation>();
            List<LookupMask> ListOfMasks = new List<LookupMask>();

            ListOfMasks.Add(mask);
            ListOfMasks.Add(mask2);

            ListOfEntries.Add(FirstLookupEntry);
            ListOfEntries.Add(SecondLookupEntry);

            NewLookupTable.Masks = ListOfMasks;
            NewLookupTable.Entries = ListOfEntries;

            projContext.LookupTables.Add(NewLookupTable);
            projContext.LookupTables.Update();

            projContext.ExecuteQuery();
        }



        private static void AddEntriesToExistingLookup(Guid LookupTableGuid)
        {
            LookupTable ExistingLookupTable = projContext.LookupTables.GetByGuid(LookupTableGuid);
            projContext.ExecuteQuery();

            LookupEntryCreationInformation FirstNewLookupEntry = new LookupEntryCreationInformation();
            FirstNewLookupEntry.Description = "First New Lookup Entry";
            var id = Guid.NewGuid();
            FirstNewLookupEntry.Id = id;
            FirstNewLookupEntry.Value = new LookupEntryValue();
            FirstNewLookupEntry.Value.TextValue = "cc";

            LookupEntryCreationInformation SecondNewLookupEntry = new LookupEntryCreationInformation();
            SecondNewLookupEntry.Value = new LookupEntryValue();
            SecondNewLookupEntry.Value.TextValue = "ddd";
            SecondNewLookupEntry.ParentId = id;
            SecondNewLookupEntry.Description = "Second New Lookup Entry";
            SecondNewLookupEntry.Id = Guid.NewGuid();

            List<LookupEntryCreationInformation> ListOfEntries = new List<LookupEntryCreationInformation>();

            ListOfEntries.Add(FirstNewLookupEntry);
            ListOfEntries.Add(SecondNewLookupEntry);

            ExistingLookupTable.Entries.Add(FirstNewLookupEntry);
            ExistingLookupTable.Entries.Add(SecondNewLookupEntry);

            projContext.LookupTables.Update();
            projContext.ExecuteQuery();
        }


        private static void ModifyExistingLookupEntry(Guid LookupTableGuid)
        {
            LookupTable ExistingLookupTable = projContext.LookupTables.GetByGuid(LookupTableGuid);

            projContext.Load(ExistingLookupTable.Entries);
            projContext.ExecuteQuery();

            LookupEntry ExistingLookupEntry = ExistingLookupTable.Entries.Where(x => x.FullValue == "aa.bbb").SingleOrDefault();
            LookupEntry ExistingParentLookupEntry = ExistingLookupTable.Entries.Where(x => x.FullValue == "aa").SingleOrDefault();

            //use a LookupEntryCreationInformation object? That doesn't seem right. 
            //but, if the id is the same as an existing id, it seems to work...
            //and, if you don't specify the parentid, it will be cleared, and the item will no longer be a child item
            LookupEntryCreationInformation FirstNewLookupEntry = new LookupEntryCreationInformation();
            FirstNewLookupEntry.Description = ExistingLookupEntry.Description;
            FirstNewLookupEntry.Id = ExistingLookupEntry.Id;
            FirstNewLookupEntry.ParentId = ExistingParentLookupEntry.Id;
            FirstNewLookupEntry.Value = new LookupEntryValue();
            FirstNewLookupEntry.Value.TextValue = "mmm";

            ExistingLookupTable.Entries.Add(FirstNewLookupEntry);
            projContext.LookupTables.Update();
            projContext.ExecuteQuery();
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s