SPView Field Comparison
When programmatically working with data that is collected in SharePoint lists, you are undoubtedly going to employ SharePoint views, denoted by SPView objects. When working with multiple views, there may come a time when it is necessary to do rudimentary field comparison between two views.
In order to do this, you can use this simple method below:
-
public static bool ViewFieldComparison(SPView firstView, SPView secondView)
-
{
-
if (!Equals(firstView.ViewFields.Count, secondView.ViewFields.Count))
-
{
-
return false;
-
}
-
firstView.ViewFields.ToStringCollection().CopyTo(firstViewArray, 0);
-
secondView.ViewFields.ToStringCollection().CopyTo(secondViewArray, 0);
-
for (int i = 0; i <firstViewArray.Length; i++)
-
{
-
if (!Equals(firstViewArray[i].CompareTo(secondViewArray[i]), 0))
-
{
-
return false;
-
}
-
}
-
return true;
-
}
If you wanted to firstly test whether the view exists, you can implement this as well based on what types of guards you wanted to realize:
-
public static bool DoesViewExist(string view, SPList list)
-
{
-
foreach (SPView view in list.Views)
-
{
-
if (Equals(view.Title, view))
-
{
-
return true;
-
}
-
}
-
return false;
-
}
Related posts:
- Show Whether A SharePoint Field Contains Metainformation
- Test For Flat Folder Structure In SharePoint Document Library
- Typed Dictionary With SharePoint Web Titles And Typed SPListItem Collection
- Getting a SharePoint Field Value In C#
- Updating A View Name With An Event Receiver
4 Comments »
RSS feed for comments on this post. TrackBack URL























Articles & Research
SharePoint Architecture
Research Methodology
Personal/Off-Topic
Article Or Research Filed Under 

[...] SPView Field Comparison [...]
Pingback by Links (5/29/2008 « Steve Pietrek - Everything SharePoint) — May 29, 2008 @ 7:01 pm
Hi
I need to merge more than on view.
How can i do that one.Ist view has 3 columns ,second view has 5 columns(3 columns same in view1 + extra 2 columns)
Comment by vairamuthu — August 1, 2008 @ 8:56 am
Hi
I need to merge more than on view.
How can i do that one.Ist view has 3 columns ,second view has 5 columns(3 columns same in view1 + extra 2 columns)
Comment by vairamuthu — August 1, 2008 @ 8:56 am
Hi,
This code is really help ful.But i want to find the view field display name.
if i use ‘viewfields’ property,it is displaying internal name of theview field.For my project i need display name of the view field.
if you know,kindly give reply.
Thanks,
komal.
Comment by komal — November 26, 2008 @ 7:06 am