0

I am unable to delete the hidden columns (IconOverlay) from the document library using CSOM. At the time of provisioning, the IconOverlay column was added to content type and content type is associated with the document library.

Now I have to remove that column from the document library, so first, I am removing the column from the content type and I am successful in doing that but when it comes to removing the orphan IconOverlay column from the document library, it is throwing an exception "You can not delete hidden column". Any help will be appreciated.

  • why don't you try setting the column as visible (field.Hidden = false), and then delete it? – Tiago Duarte Oct 11 '17 at 11:07
  • I have tried that, I am getting an exception like you can not change this attribute. – Mahesh kumar Chiliveri Oct 11 '17 at 11:08
3

Here's a solution working in SPServer, but CSOM.

The idea is to change the field schema's CanToggleHidden property.

You should adapt it slightly to get the library field, not the site field.

This is way verbose, you could probably do this in 2 lines of code :p

Hope it helps.

public void TryMakeColumnVisible()
{
    //params
    string url = "http://yoursite";
    string fieldTitle = "test_column_number";
    bool hidden = false;
    bool canToggle = true;

    ConsoleExtensions.WriteWarning("Checking column hidden...");
    context = new ClientContext(url);
    context.Credentials = new System.Net.NetworkCredential(UserName, Password, Domain);

    Web web = context.Web;
    context.Load(web);
    context.ExecuteQuery();

    Field field = web.Fields.GetByTitle(fieldTitle);
    context.Load(field);
    context.ExecuteQuery();

    string canToggleCurrent = Regex.Replace(field.SchemaXml, ".*CanToggleHidden=\"(.*?)\".*", "$1");

    string newSchemaXml = Regex.Replace(field.SchemaXml, "CanToggleHidden=\"" + canToggleCurrent + "\"", "CanToggleHidden=\"" + canToggle.ToString().ToUpper() + "\"");

    Console.WriteLine("Field          : " + field.Title);
    Console.WriteLine("Hidden         : " + field.Hidden);
    Console.WriteLine("CanToggleHidden: " + canToggleCurrent);
    Console.WriteLine(field.SchemaXml);

    //without this --> Unhandled Exception: Microsoft.SharePoint.Client.ServerException: Cannot change Hidden attribute for this field
    //update schema to allow update hidden property
    field.SchemaXml = newSchemaXml;
    field.Update();
    context.ExecuteQuery();

    //update hidden property
    field.Hidden = hidden;
    field.Update();
    context.ExecuteQuery();

    Console.WriteLine(Environment.NewLine + "Setting " + fieldTitle + " with Hidden " + hidden + " and CanToggleHidden " + canToggle);
    Console.WriteLine("Hidden         : " + field.Hidden);
    Console.WriteLine(newSchemaXml);
}

enter image description here

  • 2
    It worked very well, but for some reason, i am unable to get the CanToggleHidden Property in the SchemaXML, so, I have added that attribute and now I am able to delete the hidden column. – Mahesh kumar Chiliveri Oct 13 '17 at 9:31
  • My modification is to remove CanToggleHidden (if it's there) and then add it right after Hidden. Foolproof: string newSchemaXml = field.SchemaXml; newSchemaXml = newSchemaXml.Replace(" CanToggleHidden=\"FALSE\"", ""); newSchemaXml = newSchemaXml.Replace(" CanToggleHidden=\"TRUE\"", ""); newSchemaXml = newSchemaXml.Replace(" Hidden=\"FALSE\"", " Hidden=\"FALSE\" CanToggleHidden=\"TRUE\""); newSchemaXml = newSchemaXml.Replace(" Hidden=\"TRUE\"", " Hidden=\"TRUE\" CanToggleHidden=\"TRUE\""); – flayman Aug 10 '18 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.