none
Delete custom site columns added to a group programmatically RRS feed

  • Question

  • HI,

    How to delete the site columns which are added to a Group in the site column. I have tried the below code:

    public void DeleteField(SPWeb web, string name)
            {
                if (web.Fields.ContainsField(name))
                {
                    var field = web.Fields.GetFieldByInternalName(name);
                    field.Delete();
                }
            }

    However, I am looking for Deleting field based on group name. How to achieve this?

    Thanks

    Wednesday, November 28, 2012 8:48 AM

Answers

  • Hi Venkatzeus,

    Have a try of the following code:

    public void DeleteField(SPWeb web,string groupname)
    {  
      foreach(SPField field in web.Fields) 
      {  
         If(field.Group.Equals(groupname))
         {  
           field.Delete(); 
         }           
      }         
    }

    Best Regards.


    Kelly Chen
    TechNet Community Support

    • Marked as answer by Venkatzeus Friday, November 30, 2012 8:49 AM
    Thursday, November 29, 2012 7:49 AM
    Moderator

All replies

  • Hi

    Did you try anything like this?

    public void DeleteField(SPWeb web, string name)
    {
       if (web.Fields.ContainsField(name))
       {
           SPField field = web.Fields.GetFieldByInternalName(name);
           if (field.Group.Equals("Group you want to delete from"))
           {
               field.Delete();
           }
       }
    }


    Kind Regards

    Bjoern
    http://spviking.com
    Twitter: Follow @bjoern_rapp

    Wednesday, November 28, 2012 9:17 AM
    Moderator
  • HI,

    Thanks for the reply.

    Sorry for the confusion.

    I was looking for : - Rather than giving all the field names, passing the group name and delete the fields inside the group.

    Thanks

    Wednesday, November 28, 2012 10:01 AM
  • Hi Venkatzeus,

    Have a try of the following code:

    public void DeleteField(SPWeb web,string groupname)
    {  
      foreach(SPField field in web.Fields) 
      {  
         If(field.Group.Equals(groupname))
         {  
           field.Delete(); 
         }           
      }         
    }

    Best Regards.


    Kelly Chen
    TechNet Community Support

    • Marked as answer by Venkatzeus Friday, November 30, 2012 8:49 AM
    Thursday, November 29, 2012 7:49 AM
    Moderator
  • Here is my code 


                    using (ClientContext clientContext = new ClientContext("mysiteURL"))
                    {
                        SecureString passWord = new SecureString();

                        foreach (char c in "Password".ToCharArray()) passWord.AppendChar(c);

                        clientContext.Credentials = new SharePointOnlineCredentials("myoffice@123.com",passWord);
                        Web web = clientContext.Web;
                       List<Field> fieldsInGroup = new List<Field>();
                        FieldCollection allFields = web.Fields;
                        foreach (Field field in allFields)
                        {
                            if (field.Group.Equals("QATS"))
                            {
                                field.DeleteObject();
                            }

                                                
                        }

    Getting this error 

    Message = "The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested."


    • Edited by Satish Rao Monday, December 5, 2016 3:58 PM
    Monday, December 5, 2016 3:55 PM
  • When using CSOM you have to load necessary collections and execute queries, your code should have these extra lines:

                    Web web = clientContext.Web;
                    FieldCollection allFields = web.Fields;
    
                    clientContext.Load(allFields);
                    clientContext.ExecuteQuery();
    
                    List<Field> fieldsInGroup = new List<Field>();
                    foreach (Field field in allFields)
                    {
                        if (field.Group.Equals("QATS"))
                        {
                            fieldsInGroup.Add(field);
                        }
                    }
                    foreach (Field field in fieldsInGroup)
                    {
                        field.DeleteObject();
                    }
                    clientContext.ExecuteQuery();
    You shouldn't delete fields in "foreach" on web.Fields collection as well, add them to separate list you can iterate instead.

    Wednesday, January 11, 2017 2:57 PM
  •                ClientContext ctx = ContextConfig.getClientContext();
                    Web web = ctx.Site.RootWeb;

                    Field field = web.AvailableFields.GetByInternalNameOrTitle("columnName");
                    ctx.Load(field);
                    ctx.ExecuteQuery();
                    field.DeleteObject();
                    ctx.ExecuteQuery();


                    Console.WriteLine("Site column deleted successfully");

    This will solve your problem.

                
    Sunday, August 11, 2019 1:53 PM