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#:
  1. public static bool ViewFieldComparison(SPView firstView, SPView secondView)
  2. {
  3. if (!Equals(firstView.ViewFields.Count, secondView.ViewFields.Count))
  4. {
  5. return false;
  6. }
  7. string[] firstViewArray = new string[firstView.ViewFields.Count];
  8. string[] secondViewArray = new string[secondView.ViewFields.Count];
  9. firstView.ViewFields.ToStringCollection().CopyTo(firstViewArray, 0);
  10. secondView.ViewFields.ToStringCollection().CopyTo(secondViewArray, 0);
  11. for (int i = 0; i <firstViewArray.Length; i++)
  12. {
  13. if (!Equals(firstViewArray[i].CompareTo(secondViewArray[i]), 0))
  14. {
  15. return false;
  16. }
  17. }
  18. return true;
  19. }

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#:
  1. public static bool DoesViewExist(string view, SPList list)
  2. {
  3. foreach (SPView view in list.Views)
  4. {
  5. if (Equals(view.Title, view))
  6. {
  7. return true;
  8. }
  9. }
  10. return false;
  11. }

3 Responses to “SPView Field Comparison”

  1. Links (5/29/2008) « Steve Pietrek - Everything SharePoint Says:

    [...] SPView Field Comparison [...]

  2. vairamuthu Says:

    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)

  3. vairamuthu Says:

    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)

Leave a Reply