Friday, October 17, 2008

Sharepoint View Comparison

For one requirement we need to compare particular view with another view (let’s say default one) to check that the first view has been modified or not.

So for that one found two perspectives.
1) Compare the view fields only.
2) Compare the view fields with order also.

For that we found one good article from Adam Buenz

But I found the other ways.
For second approach we have a smart and easy way

Compare the view fields with order.

For that we just need to compare SchemaXml of ViewFields property.
Here is the snippet
private bool CompareViewWithOrder(SPView FirstView, SPView SecondView)
{
if (FirstView.ViewFields.SchemaXml == SecondView.ViewFields.SchemaXml)
return true;
else
return false;
}



Compare the view fields without Order.

If order doesn’t mean and you just want to compare the fields of the views then here is the snippet.
private bool CompareViewWithoutOrder(SPView FirstView, SPView SecondView)
{
string[] strarrFirstViewFields = new string[FirstView.ViewFields.Count];
string[] strarrSecondViewFields = new string[SecondView.ViewFields.Count];
FirstView.ViewFields.ToStringCollection().CopyTo(strarrFirstViewFields, 0);
SecondView.ViewFields.ToStringCollection().CopyTo(strarrSecondViewFields, 0);
if (strarrFirstViewFields.Length == strarrSecondViewFields.Length)
{
for (int i = 0; i < strarrFirstViewFields.Length; i++)
{
bool bFieldFound = false;
for (int j = 0; j < strarrSecondViewFields.Length; j++)
{
if (strarrFirstViewFields[i] == strarrSecondViewFields[j])
{
bFieldFound = true;
break;
}

}
if (!bFieldFound)
{
return false;
}
}
return true;
}
else
return false;
}


So these are the way for comparing the SharePoint views with fields and order of the fields.

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com