SharePoint CAML Extensions

Count the number of item

in the default list view:
    //using SPQueryToSql.Extensions
    var web = SPContext.Current.Web;
    var list = web.Lists["ListName"];

    var count = list.Count();


in the specific list view:
    //using SPQueryToSql.Extensions
    var web = SPContext.Current.Web;
    var list = web.Lists["ListName"];
    var view = list.Views["ViewName"];

    var count = list.Count(view);


in the specific query:
    //using SPQueryToSql.Extensions
    var web = SPContext.Current.Web;
    var list = web.Lists["ListName"];
    var query = new SPQuery
                            {
                               Query = @"<Where>
                                                <Eq>
                                                <FieldRef Name=""Money"" />
                                                <Value Type=""Text"">2</Value>
                                                </Eq>
                                                </Where>"
                            };

    var count = list.Count(query);


You can also use RandomId() to get id of random item or Any to check there is any item in View/Query.

Last edited Nov 18, 2012 at 6:27 PM by VitalyZhukov, version 3