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:
[csharp]
private static string BuildDefaultViewQuery()
{
var builder = new StringBuilder();
builder.Append(““);
builder.Append(““);
builder.Append(“1”);
builder.Append(“
“);
return builder.ToString();
}

private static string BuildPrivateViewQuery(string user)
{
var builder = new StringBuilder();
builder.Append(“ “);
builder.Append(““);
builder.Append(user);
builder.Append(“
“);
return builder.ToString();
}

private static string BuildPublicViewQuery(string user)
{
var builder = new StringBuilder();
builder.Append(“ “);
builder.Append(““);
builder.Append(user);
builder.Append(“
“);
builder.Append(““);
builder.Append(“1”);
builder.Append(“
“);
return builder.ToString();
}
[/csharp]
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.

[csharp]
private static SPListItemCollection (string url, string viewName, string userId)
{
using (var site = new SPSite(url))
{
using (SPWeb web = site.OpenWeb())
{
var query = new SPQuery { Query = BuildPrivateViewQuery(userId) };
return web.Lists[viewName].GetItems(query);
}
}
}

[/csharp]

Share

Iterating / Recurring Through Nested Folders in C#

I ran into this issue at a client, and I found the information frickin sparse that was out there. If, at some point, you need to iterate through subfolders in a document library, you will find a nice set of posts out there saying “hey, just use recursion”. Being one that prefers copying and pasting methods into my classes, that just doesn’t really blow my hair back.

So, if you need to iterate through all your subfolders, here is the simple method that you can use to do so. You could obviously make this better with generics, but I am entirely too lazy to do so. You will notice that you should test whether you are returning the “forms” folder, since that is reserved and we don’t really care what is in it.
[csharp]

public SPFolderCollection subFolderCollection = null;

private void recurSubFolders(HtmlTextWriter output, string folderPath, SPFolderCollection subFolders)
{
if ((this.subFolderCollection != null) && (this.subFolderCollection.Count > 1))
{
foreach (SPFolder folder in subFolders)
{
if (folder.Name.ToLower() != “forms”)
{
string text = folderPath + @”\” + folder.Name;
output.Write(text);
this.recurSubFolders(output, text, folder.SubFolders);
}
}
}
}

[/csharp]

That’s it. Happy recurring!

Share