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

Tobias Zimmergren
Tobias Zimmergren

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!

SharePoint

Tobias Zimmergren Twitter

Hi, I'm Tobias. I plan, architect and develop software and distributed cloud services. Nice to meet you!

Reactions and mentions


Hi, I'm Tobias 👋

Tobias Zimmergren profile picture

Find out more about me.

Recent comments

Recommended books!

Prepare for the AZ-500 exam!
Exam Ref AZ-500 Microsoft Azure Security Technologies
Azure Security Handbook!
Azure Security Handbook: A Comprehensive Guide for Defending Your Enterprise Environment
Prepare for the SC-900 exam!
Exam Ref SC-900 Microsoft Security, Compliance, and Identity Fundamentals