kamagra how much to take

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:

  1. private static string BuildDefaultViewQuery()
  2. {
  3. var builder = new StringBuilder();
  4. builder.Append("<Where>");
  5. builder.Append("<Eq><FieldRef Name='DefaultView' /><Value Type='Boolean'>");
  6. builder.Append("1");
  7. builder.Append("</Value></Eq></Where>");
  8. return builder.ToString();
  9. }
  10.  
  11. private static string BuildPrivateViewQuery(string user)
  12. {
  13. var builder = new StringBuilder();
  14. builder.Append("<OrderBy><FieldRef Name='ViewName'/> </OrderBy>");
  15. builder.Append("<Where><Eq><FieldRef Name='ViewCreator' /><Value Type='Text'>");
  16. builder.Append(user);
  17. builder.Append("</Value></Eq></Where>");
  18. return builder.ToString();
  19. }
  20.  
  21. private static string BuildPublicViewQuery(string user)
  22. {
  23. var builder = new StringBuilder();
  24. builder.Append("<OrderBy><FieldRef Name='Audience'/> <FieldRef Name='ViewName'/></OrderBy>");
  25. builder.Append("<Where><Or><Eq><FieldRef Name='ViewCreator' /><Value Type='Text'>");
  26. builder.Append(user);
  27. builder.Append("</Value></Eq>");
  28. builder.Append("<Eq><FieldRef Name='Audience' /><Value Type='Choice'>");
  29. builder.Append("1");
  30. builder.Append("</Value></Eq></Or></Where>");
  31. return builder.ToString();
  32. }

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.

  1. private static SPListItemCollection (string url, string viewName, string userId)
  2. {
  3. using (var site = new SPSite(url))
  4. {
  5. using (SPWeb web = site.OpenWeb())
  6. {
  7. var query = new SPQuery { Query = BuildPrivateViewQuery(userId) };
  8. return web.Lists[viewName].GetItems(query);
  9. }
  10. }
  11. }
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>