none
How to delete a lookup field (FieldLookup) in a list using csom

    Question

  • Hi there,

    I'm trying to delete a lookup field in a list using CSOM but it appears not to be possible.

    Tryed the following combinations:

    FieldLookup columnToDeleteLU = (FieldLookup)columnToDelete;
    columnToDeleteLU.DeleteObject();
    list.Update();
    ctx.ExecuteQueryandRetry();

    OR

    Field columnToDeleteLU = list.Fields.FirstOrDefault(x => x.Id == columnDelete)
    columnToDelete.DeleteObject();
    list.Update();
    ctx.ExecuteQueryandRetry();

    And I always get the error:

    The primary lookup field cannot be deleted. There are dependent lookups created on this primary lookup field that should be deleted first."


    I tried to use the

     var primaryFieldID = columnToDeleteLU.PrimaryFieldId;
    list.Fields.FirstOrDefault(x => x.Id == columnDelete).DeleteObject();
    list.Update();
    ctx.ExecuteQueryandRetry();

    Also with no success. I'm a little stuck here. Any ideas?


    Friday, January 20, 2017 11:35 AM

All replies

  • Hi,

     

    Could you try with below code? I tested in my local and it works.

    using (var context = new ClientContext("https://xxx.sharepoint.com/sites/lee/"))
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    string password = "pw";
                    SecureString sec_pass = new SecureString();
                    Array.ForEach(password.ToArray(), sec_pass.AppendChar);
                    sec_pass.MakeReadOnly();
                    context.Credentials = new SharePointOnlineCredentials("user@xxx.onmicrosoft.com", sec_pass);
                    
                    List list = context.Web.Lists.GetByTitle("myList2");
                    context.Load(list);
                    context.ExecuteQuery();
                    var columnToDeleteLU = list.Fields.GetByTitle("LKField");
                    columnToDeleteLU.DeleteObject();
                    context.Load(list);
                    context.ExecuteQuery();
                    Console.WriteLine("Done");
                    Console.ReadKey();
                }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, January 23, 2017 2:48 AM
  • Hi,

    Is any update for your issue?

    Best
    Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Monday, February 6, 2017 8:36 AM