Thursday, February 07, 2019

enumerate columns from subsites

This case is something which I've came back to a few times.
Customer wants to output columns from certain content types from a number of sites.
Its always a hassle to get the correct names of columns so I've built a method to output the values more dynamically.

1. First getFields.ps1 reads columns containg values from a list item, outputs to gridview, chosen values gets saved to customfields.csv
2. Second getSitecontenttypefields.ps1 enumerates all sites on chosen destination and loops all items of specified content types and outputs chosen fields from customfields.ps1 to an object which is saved to a csv-file.

$customUrl = "https://mysiteadress.com/mysubsite/publications/2018/Pages/2018-21.aspx"
Add-PSSnapin microsoft.sharepoint.powershell
$scriptPath = split-path -Parent $myinvocation.MyCommand.Definition
Remove-Variable -ErrorAction SilentlyContinue savedFields
$webUrl = $customUrl.replace($customUrl.split("/")[-1],"").replace($customUrl.split("/")[-2],"").TrimEnd("/")
$web = Get-SPWeb $webUrl
$object = @()
$oneItem = $web.GetListItem($url)
foreach ($field in $oneItem.ContentType.fields) {
if (![string]::IsNullOrEmpty($oneItem[$field.staticName])) {
$object += new-object psobject -Property @{fieldName=$field.StaticName;fieldValue=$oneItem[$field.staticName]}
}
}
write-output "Found $($object.count) items"
$savedFields = $object |select fieldName,fieldValue |Out-GridView -Title "Choose columns" -PassThru
if (![string]::IsNullOrEmpty($savedFields)) {
$savedFields |select fieldname | export-csv -Path "$($scriptPath)\customfieldsList.csv" -Encoding UTF8 -NoTypeInformation
write-output "Output $(($savedFields|measure-object).count) fields to file"
}
else {
write-output "No fields chosen"
}
view raw getFields.ps1 hosted with ❤ by GitHub
$siteCollection = "https://mysiteadress.com"
$webRootUrl = "https://mysiteadress.com/mysubsite/publications/"
$contentTypeName = "my-content-type"
#$VerbosePreference = "continue" #uncomment to show verbose
$scriptPath = split-path -Parent $myinvocation.MyCommand.Definition
import-module "$($scriptPath)\SpSiteTools.psm1" -force
$fieldNames = import-csv -path "$($scriptPath)\customfieldsList.csv"
Add-PSSnapin microsoft.sharepoint.powershell
$webSites = get-SubSites -siteToCountWebSites $webRootUrl -siteCollection $siteCollection
$outputObjects = get-columnvalues -websites $websites -contenttype $contentTypeName -fields $fieldNames -Verbose
write-output "Found $($outputObjects.count) items in $($website.count) websites"
$outputObjects |export-csv -Path "$($scriptPath)\output2.csv" -NoTypeInformation -Encoding UTF8
function get-SubSites ($siteToCountWebSites,$siteCollection)
{
$rootSite = get-spweb -Identity $siteToCountWebSites
$countSites= get-spweb -Site $siteCollection -limit all |where-object {$_.url -like "$($rootSite.url)*"}
return $countSites
}
function select-tag {
param($string,
$tag="a")
$htmlString2 = new-object -ComObject "HTMLFILE"
$htmlString2.IHTMLDocument2_write($string)
$htmlString2.body.getElementsByTagName($tag)|select -ExpandProperty pathname
}
function get-pageLib {
param(
$website
)
if ($website.lists.title -contains 'Sidor') {
$listName = "Sidor"
}
elseif ($website.lists.title -contains 'Pages') {
$listName = "Pages"
}
else {
write-verbose "Couldn't find list!"
$listname = $null
}
return $listName
}
function get-columnvalues {
param(
[parameter(mandatory=$true)]
[validateNotNullOrEmpty()]
$websites,
[parameter(mandatory=$true)]
[validateNotNullOrEmpty()]
$contentType,
[parameter(mandatory=$true)]
[validateNotNullOrEmpty()]
$fields
)
$objects = @()
foreach ($website in $webSites) {
$listName = get-pageLib -website $website
write-verbose "Using library: $listName"
$listItems = $website.lists[$listName].items
foreach ($item in $listItems) {
if ($item.ContentType.Name -like $contentTypeName) {
write-verbose "Content Type: $($item.contentType.name)"
$properties = @{}
foreach ($fieldName in $fieldNames.fieldName) {
$properties.add($fieldName,$item[$fieldName])
}
$objects += new-object psobject -Property $properties
}
else {
write-verbose "Content Type: $($item.contentType.name)"
}
}
}
return $objects
}



No comments:

Powershell and Uptimerobot

Uptimerobot can be quite tedious when you need to update many monitors at once. For example say you bought the license for Uptimerobot and n...