Thursday, July 7, 2011

How to delete a field from a list view in SharePoint 2010

In my previous post Add a field to a list view in SharePoint 2010 I talked about how to add a field to a list view. Today we’ll look how to remove a field from a list view.

Just like adding, you can do this by selecting the modify view button and deselecting the field that you want to delete from the current view.

Here’s how it’s done via code.

public static void RemoveFieldFromListView(string webUrl, string fieldStaticName, string listName)
{
using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{                  
SPList list = null;
bool contains = false;
//This block will validate whether the given list is exists at the given web.
try
{
list = web.Lists[listName];
contains = true;
}
catch (Exception ex)
{
//Do your custom exceptions handling here.
}
if (contains)
{
try
{
web.AllowUnsafeUpdates = true;
SPField field = web.Lists[listName].Fields.TryGetFieldByStaticName(fieldStaticName) as SPField;// get the field using the static name of the field.
SPView view = list.DefaultView;/ get the default view.
if (view.ViewFields.Exists(field.StaticName)) // field is already in the view.
{
view.ViewFields.Delete(field); // delete the field from the view.
view.Update();// update the view
list.Update();// update the list
web.Update();// update the web
}
}
catch (Exception ex)
{
//Do your custom exceptions handling here.
}
finally
{
web.AllowUnsafeUpdates = false;
}
}                   
}
}
}

How to add a field to a list view in SharePoint 2010

Today I’m going to share my experience on modifying list views programmatically.
When you are developing, you might come across scenarios such as modifying the list views to add fields to the list view.

You can do this quite easily by selecting the modify view button and selecting the field that you want to add to the current view.


( That is straight forward. Why do you need to write a blog on this?)
Hold on….
How about this ? What if you need to do this via code?
Now I got your attention.

I also had to face such a situation in one of my projects, where my client wants to incorporate enhancements not as fresh deployments, but as upgrades.
So I spent some time and come up with a handy method which will add an existing field to a given list view.
I’m going to share it with you all, so you guys don’t need to reinvent the wheel.

Here’s the code.

public static void AddFieldToListView(string webUrl, string fieldStaticName, string listName, int? position)
{
using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = null;
bool contains = false;
//This block will validate whether the given list is exists at the given web.
try
{
list = web.Lists[listName];
contains = true;
}
catch (Exception ex)
{
//Do your custom exceptions handling here.
}
if (contains)
{
try
{
web.AllowUnsafeUpdates = true;
//some times you need extra privileges to execute code.
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPField field = web.Lists[listName].Fields.TryGetFieldByStaticName(fieldStaticName) as SPField; // get the field using the static name of the field.
SPView view = list.DefaultView; // get the default view.
if (view.ViewFields.Exists(field.StaticName)) // field is already in the view.
{
return;
}
view.ViewFields.Add(field); // add the field to the default view.
if (position.HasValue && position.Value < view.ViewFields.Count) // if the position is given, set the position of the field in the view.
{
view.ViewFields.MoveFieldTo(field.StaticName, position.Value);
}
view.Update();// update the view
list.Update();// update the list
});
web.Update();// update the web
}
catch (Exception ex)
{
//Do your custom exceptions handling here.
}
finally
{
web.AllowUnsafeUpdates = false;
}
}
}
}
}