Posts Tagged ‘tip’

Quick tip: SharePoint powershell – get items in a list based on custom columns and other hints

Tuesday, February 14th, 2012

This may be handy when trying to find specific items in a list based on values of various fields:

$web = Get-SPWeb http://yourweb
$list = $web.Lists["Your Library Name"]

// this is the bit - get items of a particular content type
// ? is shorthand for where, and $_ is the item in the pipeline
$listItems = $list.Items | ?{$_.ContentType.Name -eq "Content Type Name"}

// or items based on a custom column - if using -like then the wildcard is *
$listItems = $list.Items | ?{$_["InternalFieldName"] -like "*this*"

// you could join them up using -and
$listItems = $list.Items | ?{$_.ContentType.Name -eq "Content Type Name" -and $_["InternalFieldName"] -like "*this*"

// or iterate the loop and print them out
foreach($item in $listItems) { Write-Host $item.Name, $item["InternalFieldName"] }

or more directly

$list.Items | ?{$_.ContentType.Name -eq "Content Type Name" -and $_["InternalFieldName"] -like "*this*" | foreach { $_.Name, $_["InternalFieldName"]

// or count them
$listItems.Count

or

$list.Items | ?{$_.ContentType.Name -eq "Content Type Name" -and $_["InternalFieldName"] -like "*this*" | foreach {$count++}
$count

Powershell can be infuriating – but when you find the syntax, it can be pretty helpful.

P.S For a bonus tip, next time you’re in Powershell, hit F7 ;)

SharePoint: Error occurred during deployment step activate features

Friday, August 5th, 2011

Quick (annoying) thing. Whilst deploying a solution, you may see this error:

Error occurred in deployment step ‘Activate Features’: Key cannot be null. Parameter name: Key

This is unhelpfully connected to your Content Type definitions where you have FieldRefs and you’re closing them with the long tag, as opposed to the short tag, i.e.,

<FieldRef ID=”…”></FieldRef>

replace it with this to fix:

<FieldRef ID=”…” />

Thanks to Dhiraj.