SPView Field Comparison
Posted in SharePoint 2007 Development |Posted by Adam Buenz
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:
C#:
-
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:
C#:
-
public static bool DoesViewExist(string view, SPList list)
-
{
-
foreach (SPView view in list.Views)
-
{
-
if (Equals(view.Title, view))
-
{
-
return true;
-
}
-
}
-
return false;
-
}











May 29th, 2008 at 7:01 pm
[...] SPView Field Comparison [...]
August 1st, 2008 at 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)
August 1st, 2008 at 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)
November 26th, 2008 at 7:06 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.