How to: Programmatically remove a Field (SPField) from a view (SPView)

This is yet another very simple example of a question I’ve been getting over and over the past couple of months through comments and emails – instead of answering it each time in the mail, I’m simply shooting it out here.

Deleting a Field from a View programmatically

Alright, here’s a good starting point for you to check out if you simply want to remove a field from the view. Simple as that.

SPList list = SPContext.Current.Web.Lists["MyAwesomeList"];
SPView view = list.Views["MyView"];

if(view.ViewFields.Exists("MyField"))
{
    bool allowUnsafe = SPContext.Current.Web.AllowUnsafeUpdates;

    SPContext.Current.Web.AllowUnsafeUpdates = true;
    view.ViewFields.Delete("MyField");
    view.Update(); 

    SPContext.Current.Web.AllowUnsafeUpdates = allowUnsafe;
}

The aforementioned code is all there’s to it in order for you to get started.
Simple, quick, effective!