Building SharePoint View Queries with CAML
Often times when building applications that will greatly use SharePoint views for data rotation it is helpful to build CAML builders into separate static classes that return a continuous string representation of the arbitrary query. In order to demonstrate this, consider the following examples BuildDefaultViewQuery, BuildPrivateViewQuery, and BuildPublicViewQuery:
-
private static string BuildDefaultViewQuery()
-
{
-
builder.Append("<Where>");
-
builder.Append("<Eq><FieldRef Name='DefaultView' /><Value Type='Boolean'>");
-
builder.Append("1");
-
builder.Append("</Value></Eq></Where>");
-
return builder.ToString();
-
}
-
-
private static string BuildPrivateViewQuery(string user)
-
{
-
builder.Append("<OrderBy><FieldRef Name='ViewName'/> </OrderBy>");
-
builder.Append("<Where><Eq><FieldRef Name='ViewCreator' /><Value Type='Text'>");
-
builder.Append(user);
-
builder.Append("</Value></Eq></Where>");
-
return builder.ToString();
-
}
-
-
private static string BuildPublicViewQuery(string user)
-
{
-
builder.Append("<OrderBy><FieldRef Name='Audience'/> <FieldRef Name='ViewName'/></OrderBy>");
-
builder.Append("<Where><Or><Eq><FieldRef Name='ViewCreator' /><Value Type='Text'>");
-
builder.Append(user);
-
builder.Append("</Value></Eq>");
-
builder.Append("<Eq><FieldRef Name='Audience' /><Value Type='Choice'>");
-
builder.Append("1");
-
builder.Append("</Value></Eq></Or></Where>");
-
return builder.ToString();
-
}
It should be noted that there are some constants that could instead be extracted to an enumeration and then Enum.GetName could be used for better component allocation. However, in this example since it is merely to show the overlying constructs, is kept purposefully plain.
In order to use one of static CAML methods, the ReturnCollectionExample method will take some basic parameters, than return the item collection associated with the BuildPrivateViewQuery query.
Related posts:
- Returning The SharePoint Start Workflow Link
- Use A Builder With JavaScript In WebParts
- Getting a SharePoint Field Value In C#
- Using Regular Expressions For SharePoint List Collection Queries
- Using DescriptionAttribute And Reflection For Building Typed Collections Off Enumerations
No Comments »
No comments yet.
RSS feed for comments on this post. TrackBack URL























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