Monthly Archives: June 2014

SharePoint add Count to List View using powershell


As a best practice, we create lists that have substantial number of columns using powershell.

What I like about using powershell is – repetitive and controlled deployment in dev, test and prod are predictable.

Not just that, you can control the internal field names much better.

The Aggregations (or Count of rows) is done to the View – not to the List. So you need to get reference to the View (and not to the List).

$web = "http://dev/sites/yoursitename/"
$spsite = [Microsoft.SharePoint.SPSite]($web)
$web = $spsite.OpenWeb()
$view = $web.GetViewFromURL("Lists/YourList/AllItems.aspx)
$aggregations = "<FieldRef Name='LinkTitle' Type='COUNT'/>"
$view.Aggregations. = $aggregations
$view.AggregationsStatus = "On"
$view.Update()

That’s it.

Other Aggregations Types are ‘MAX’ for Maximum, ‘SUM’ for Sum’s, above example has ‘COUNT’ for Total’s.

Now that looks very simple, but in your real life scenario you are probably building up a more complex query and aggregations.

So how do I actually build the CAML query. Using spcamlqueryhelper.codeplex.com

Create a list first (using powershell or IE) – and understand the CAML by actually creating a view using UI (internet explorer) if you are uncomfortable with powershell.

When the view is available, use the SPCAML Query Helper to figure out what the CAML was for your view. I found this to be very handy.

CAML