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;
string[] firstViewArray = new string[firstView.ViewFields.Count];
string[] secondViewArray = new string[secondView.ViewFields.Count];
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; } [/csharp] 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: [csharp] public static bool DoesViewExist(string view, SPList list) { foreach (SPView view in list.Views) { if (Equals(view.Title, view)) { return true; } } return false; } [/csharp]