SharePoint 2007. The max/min value of filed in SharePoint list

Tuesday, October 19, 2010

Today I'll show you how you can get the minimum or maximum value of the field in SharePoint list.

Solution

The solution lies in the fact that we simply sort the elements in this field from the previous to the late receipt at the minimum value (or vice versa to obtain the maximum). Also, we will limit the number of elements of one.

For convenience, I made a method for the extender SPListItem, which takes as a parameter to the internal name of the field. Here I will give methods for obtaining the maximum value. Getting the minimum value will differ only in the direction of sorting.

  1. /// <summary>
  2. /// Maximum value of the field in the list
  3. /// </summary>
  4. /// <typeparam Name="T">type values ​​in the field </typeparam>
  5. /// <param Name="list">List</param>
  6. /// <param Name="fieldInternalName">Internal name of the field </param>
  7. public  static  T GetMaxValue <T> (this  SPList list,  string  fieldInternalName)
  8. {
  9.     var  query =  New  SPQuery
  10.     {
  11.         // Take the only field that we are interested in
  12.         ViewFields =  string . Format ( "<FieldRef Name='{0}' />" , fieldInternalName),
  13.         // Build a query
  14.         Query = string.Format(@"<OrderBy><FieldRef Name='{0}' Ascending='False'/></OrderBy>"
  15.         fieldInternalName),
  16.         // Get only the first row
  17.         RowLimit = 1
  18.     };
  19.     // Execute a query
  20.     var  items = list.GetItems (query). Cast <SPListItem> ();
  21.     // If the collection is empty (for example, the list contains no elements) 
  22.     // Then return the default value for type
  23.     if  (items.Count () == 0)  return  default (T);
  24.     // Return the value of the field
  25.     return  items.First () [fieldInternalName] is T
  26.         ? (T) (items.First () [fieldInternalName])
  27.         : default(T);
  28. }

To increase the functionality, you can add one more parameter - wherePredicate and CAML-transfer request for pre-filtering the data. You will be able to filter out null values ​​before the search for the minimum value, for example:

  1. /// <summary>
  2. /// Maximum value of the field in the list
  3. /// </summary>
  4. /// <typeparam Name="T">type values ​​in the field </typeparam>
  5. /// <param Name="list">List</param>
  6. /// <param Name="fieldInternalName">Internal name of the field </param>
  7. /// <param Name="wherePredicat">Predicate for the pre-filter </param>
  8. public  static  T GetMaxValue <T> (this  SPList list,  string  fieldInternalName, 
  9.                                    string wherePredicat)
  10. {
  11.     var  query =  New  SPQuery
  12.     {
  13.         // Take the only field that we are interested in
  14.         ViewFields =  string . Format ( "<FieldRef Name='{0}' />" , fieldInternalName),
  15.         // Build a query
  16.         Query = string.Format(
  17.             @"{0}<OrderBy><FieldRef Name='{1}' Ascending='False' /></OrderBy>"
  18.             wherePredicat, fieldInternalName),
  19.         fieldInternalName),
  20.         // Get only the first row
  21.         RowLimit = 1
  22.     };
  23.     // Execute a query
  24.     var  items = list.GetItems (query). Cast <SPListItem> ();
  25.     // If the collection is empty (for example, the list contains no elements) 
  26.     // Then return the default value for type
  27.     if  (items.Count () == 0)  return  default (T);
  28.     // Return the value of the field
  29.     return  items.First () [fieldInternalName] is T
  30.         ? (T) (items.First () [fieldInternalName])
  31.         : default(T);
  32. }

Using

For example, to maximize the creation date and maximum values ​​of ID:

  1. var maxCreated = list.GetMaxValue<DateTime>("Created");
  2. var maxId = list.GetMaxValue<int>("ID");

I hope useful.

Share

Comments