Tag Archives: SharePoint 2010

SharePoint Surveys and the mystery of the missing partial response

Note: this applies to SharePoint 2010 and 2013, it is not viable for SharePoint online / Office 365.

Fun fact, SharePoint, if you’ve got branching logic in your survey, allows you to save your response to a survey!

This is great news. However on TechNet someone asked if it were possible to find these incomplete posts to remind the user that they haven’t finished?

Normally you’d assume that all you’d need to do is log in with a site collection admin account and you’d magically be able to see all the responses, even those that haven’t been completed. However in this case you’d be wrong. Even with Site Collection admin rights those incomplete responses are hidden from you.

Now that data is still in the SharePoint list and rather than mess about with permissions and settings, which could have unforeseen consequences, let’s see if we can’t pull it out of the list with PowerShell?

The first thing to do is see if we can get the real number of items from the survey. The easiest way to do that is to check the item count.

$web = Get-SPWeb "URL"
$survey = $web.Lists["SurveyName"]
Write-Host "Items in the survey : " $survey.ItemCount

That gives you the number of items, which you can compare to those visible. In my case I had one completed and one partial, leading to a total count of two. Which the script agreed with.

When you look at the object in PowerShell you can see that there’s a few fields that are different between a completed and a partial response. In this case we have two that are of interest.

Image showing different HasPublishedVersion and Level for two items.

The differences between a visible, completed item (1) and an incomplete item (2)

Of the two i’m going to use the ‘HasPublishedVersion’ field. Live the ‘Level’ field it will become true as long as a version has been completed, however unlike Level it will remain true even if the user starts to edit it later and somehow manages to do a partial save.

Let’s extend our script to list out the number in each group, then list the users who created them.

$web = Get-SPWeb "http://portal.tracy.com/sites/GD/"
$survey = $web.lists["Survey"]
$unPublishedEntries = $survey.Items | ? {-not $_.HasPublishedVersion}

Write-Host "Surveys in list: " $survey.ItemCount
Write-Host "Of which entries are incomplete: " $unPublishedEntries.Count

Foreach ($entry in $unPublishedEntries)
{
    Write-Host "User: {0} has not completed their entry" -f $entry["Created By"]
}

And voila, my results give us:

PowerShell results showing the results from a script

Printout from script

Cataloging Choice Columns

It’s been a while but it’s time for a new post.

Someone asked on TechNet () how to get a summary of all the custom choice columns and the options they have. I didn’t have the time to put it together at that moment but I thought It’d be a good exercise.

This will work for 2010 or 2013 but will not work for Office 365 or SharePoint online. For that a different approach would be needed.

“I inherited a SharePoint 2010 Foundation site that contains about 40 custom columns and about 10 of those custom columns are of the type “Choice”. Is there a way using Powershell or something else to export to a .csv file a list of the custom columns and if they are the type “choice” to show the list of what the various choices are for each column?”

So, let’s assume that we’re only interested in the site columns. To do that we’ll have to grab the SPWeb object and loop through the columns there that are of the appropriate type and list them out.

#Get the web
$web = Get-SPWeb "http://sharepoint.domain.com/sites/sitecollection/subsite"

#Get the columns (note, these are named fields)
$Columns = $web.Fields | ? { $_.TypeAsString -eq "Choice"}

#Print out the number of columns
Write-Host "Number of columns found: " $Columns.Count

#Loop through each choice and print the name
foreach ($entry in $columns)
{
Write-Host ("Field Name: {0}" -f $entry.InternalName)
#Loop through the choices and print those out
foreach ($choice in $entry.Choices)
{
Write-Host ("  Choice: {0}" -f $choice)}
}

From here

That’ll list out the columns to the screen but it’s not a great solution. It’s printing out too many columns, it’s also just printing them to the screen. We need it to serialise this into a format that we can use.

Let’s start with serialisation.

There’s loads of ways to do this but my preference is to create custom objects to contain the information we collect, then assign them to an array which we can process later.

For a quick guide to PSObjects have a look here
So, after changing the write-hosts to write verbose and putting in our custom objects we get this!

Add-PSSnapin Microsoft.SharePoint.PowerShell -ea SilentlyContinue

#Get the web
$web = Get-SPWeb "http://portal.tracy.com/sites/cthub"

#Get the columns (note, these are named fields) 
$Columns = $web.Fields | ? { $_.TypeAsString -eq "Choice"}

#Print out the number of columns
Write-Host "Number of columns found: " $Columns.Count

#Create empty array
$ColumnDetailsArray = @()

#Loop through each choice column and create an object to hold it
foreach ($entry in $columns)
{
    $choicesArray = @()
    Write-Verbose ("Field Name: {0}" -f $entry.InternalName)
    
    #Loop through the choices and print those out
    foreach ($choice in $entry.Choices)
    {
        #Add each choice to the (local) array
        Write-Verbose ("  Choice: {0}" -f $choice)
        $choicesArray += $choice
    }
    #Create a result object to hold our data
    $ColumnDetailsArray += New-Object PSObject -Property @{
                        "Name" = $entry.InternalName
                        "Choices" = $choicesArray
                        }
}

Which actually makes things worse as we no longer get any results! Let’s add that in some xml work. I’m still not entirely happy with the way PowerShell and XML work together so this example is a bit clunky but it works.

#Create a starter XML for the system to work with
[xml] $xml = "<root><summary rundate='{0}' web='{1}'/></root>" -f 
    (Get-Date -f ddMMyyyy), 
    $web.Title

#loop through the results and genearate an xml object
foreach ($column in $ColumnDetailsArray)
{
    #Create an element to hold the top level item
    $columnElement = $xml.CreateElement("Choice")
    $columnElement.SetAttribute("Name", $column.Name) 

    #Loop through the choices and add entries for each
    foreach ($choice in $column.Choices)
    {
        $choiceElement = $xml.CreateElement("Choice")
        
        #Note that you need this Pipe Out-Null to prevent it writing to the console
        $choiceElement.InnerText = $choice
        $columnElement.AppendChild($choiceElement) | Out-Null
    }
    #Once it's built add the element to the root node
    $xml.root.AppendChild($columnElement)  | Out-Null
}
$xml.Save("C:\ResultFolder\ColumnSummary.xml")

So, this now dumps out the data we’ve asked for but it also dumps out all the pre-packaged columns. This is a problem that isn’t easily fixed, there isn’t an ‘OOTB’ flag on fields but there are a few we can use to filter them out.

If we grab a column from the results and run Get-Members on it there are a couple of fields that should be useful for filtering the results:

Sealed – This shows if the column is not supposed to be edited by human hands. Note that this could give false negatives in scenarios where a column has been deployed via the CTHub which I think seals columns (it definitely seals Content Types) as it pushes to consuming site collections

Hidden – Not relevant in this case but often handy. In this case we’ll filter out groups that are part of the ‘_hidden’ group earlier.

So if we now add that criteria to the earlier $columns filtering process we get

$Columns = $web.Fields | ? { $_.TypeAsString -eq "Choice" -and 
        -not $_.Sealed -and $_.Group -ne "_Hidden"
    }

But that’s still not perfect, so instead of filtering the terms out right now let’s make it a bit more useful first. When you look at columns in SharePoint they are organised in Groups. We can see those properties in PowerShell and group our elements using that field.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ea SilentlyContinue

#Get the web
$web = Get-SPWeb "http://portal.tracy.com/sites/cthub"

#Get the columns (note, these are named fields) 
#Also filter out the sealed fields
$Columns = $web.Fields | ? { $_.TypeAsString -eq "Choice" -and 
        -not $_.Sealed -and $_.Group -ne "_Hidden"
    }


#Print out the number of columns
Write-Host "Number of columns found: " $Columns.Count

#Create empty array
$ColumnDetailsArray = @()

#Loop through each choice column and create an object to hold it
foreach ($entry in $columns)
{
    $choicesArray = @()
    Write-Verbose ("Field Name: {0}" -f $entry.InternalName)
    
    #Loop through the choices and print those out
    foreach ($choice in $entry.Choices)
    {
        #Add each choice to the (local) array
        Write-Verbose ("  Choice: {0}" -f $choice)
        $choicesArray += $choice
    }
    #Create a result object to hold our data
    $ColumnDetailsArray += New-Object PSObject -Property @{
                        "Name" = $entry.InternalName
                        "Group" = $entry.Group
                        "Choices" = $choicesArray
                    }
}

#Create a starter XML for the system to work with
[xml] $xml = "<root><summary rundate='{0}' web='{1}'/></root>" -f 
    (Get-Date -f ddMMyyyy), 
    $web.Title

#Get a unique list of the groups in use in the site
foreach ($group in $ColumnDetailsArray | select Group -Unique)
{
    $groupText = $group.Group
    Write-Host "Group name: " $groupText
    $groupElement = $xml.CreateElement("Group")
    $groupElement.SetAttribute("Name", $groupText)
    
    #loop through the results and add them to the xml object
    foreach ($column in $ColumnDetailsArray | ? {$_.Group -eq $groupText})
    {
        #Create an element to hold the top level item
        $columnElement = $xml.CreateElement("Choice")
        $columnElement.SetAttribute("Name", $column.Name)

        #Loop through the choices and add entries for each
        foreach ($choice in $column.Choices)
        {
            $choiceElement = $xml.CreateElement("Choice")
        
            #Note that you need this Pipe Out-Null to prevent it writing to the console
            $choiceElement.InnerText = $choice
            $columnElement.AppendChild($choiceElement) | Out-Null
        }
        #Once it's built add the element to the root node
        $groupElement.AppendChild($columnElement)  | Out-Null
    }
    $xml.root.AppendChild($groupElement)  | Out-Null
}
$xml.Save("C:\ResultFolder\ColumnSummary.xml")

Of course once you have the group name you can filter those options out by using a blacklist of groups to avoid reporting on.


Add-PSSnapin Microsoft.SharePoint.PowerShell -ea SilentlyContinue

#BlackList Group Names
#These are the known groups you get in a non publishing team site:
$blackList = @(
    "_Hidden",                                                                         
    "Base Columns",                                                                    
    "Content Feedback",                                                   
    "Core Contact and Calendar Columns",                                               
    "Core Document Columns",                                                         
    "Core Task and Issue Columns",                                                          
    "Custom Columns",                                                            
    "Display Template Columns",                                                          
    "Document and Record Management Columns",                                                
    "Enterprise Keywords Group",                                                             
    "Extended Columns",                                                             
    "JavaScript Display Template Columns",                                                   
    "Reports",                                                                  
    "Status Indicators"
)

#Get the web
$web = Get-SPWeb "http://portal.tracy.com/sites/cthub"

#Get the columns (note, these are named fields) 
#Also filter out the sealed fields
$Columns = $web.Fields | ? { $_.TypeAsString -eq "Choice" -and 
        -not $_.Sealed -and $_.Group -ne "_Hidden"
    }


#Print out the number of columns
Write-Host "Number of columns found: " $Columns.Count

#Create empty array
$ColumnDetailsArray = @()

#Loop through each choice column and create an object to hold it
foreach ($entry in $columns)
{
    $choicesArray = @()
    Write-Verbose ("Field Name: {0}" -f $entry.InternalName)
    
    #Loop through the choices and print those out
    foreach ($choice in $entry.Choices)
    {
        #Add each choice to the (local) array
        Write-Verbose ("  Choice: {0}" -f $choice)
        $choicesArray += $choice
    }
    #Create a result object to hold our data
    $ColumnDetailsArray += New-Object PSObject -Property @{
                        "Name" = $entry.InternalName
                        "Group" = $entry.Group
                        "Choices" = $choicesArray
                    }
}
#Create a starter XML for the system to work with
[xml] $xml = "<root><summary rundate='{0}' web='{1}'/></root>" -f 
    (Get-Date -f ddMMyyyy), 
    $web.Title

foreach ($group in $ColumnDetailsArray | select Group -Unique)
{
    $groupText = $group.Group

    #Check to see if the group name is in our blacklist
    if (-not $blackList.Contains($groupText))
    {
        Write-Verbose "Group name: " $groupText
        $groupElement = $xml.CreateElement("Group")
        $groupElement.SetAttribute("Name", $groupText)
    
        #loop through the results and genearate an xml
        foreach ($column in $ColumnDetailsArray | ? {$_.Group -eq $groupText})
        {
            #Create an element to hold the top level item
            $columnElement = $xml.CreateElement("Choice")
            $columnElement.SetAttribute("Name", $column.Name)

            #Loop through the choices and add entries for each
            foreach ($choice in $column.Choices)
            {
                $choiceElement = $xml.CreateElement("Choice")
        
                $choiceElement.InnerText = $choice
                #Note that you need this Pipe Out-Null to prevent it writing to the console
                $columnElement.AppendChild($choiceElement) | Out-Null
            }
            #Once it's built add the element to the root node
            $groupElement.AppendChild($columnElement)  | Out-Null
        }
        $xml.root.AppendChild($groupElement)  | Out-Null
    }
    else
    {
        Write-Verbose "Group skipped:" $groupText
    }
}
$xml.Save("C:\ResultFolder\ColumnSummary.xml")

And there you have it. A working report that will summarise all custom choice columns in a SPWeb object and save them in an XML file.

Changing Modified, and Created details in SharePoint

Sometimes you need to lie to SharePoint. In this post i’ll show you how to change the details for who created an item, modified it and when they modified it.

When you’re doing bulk uploads, dealing with lists where you wish to use the Advanced features of only allowing users to edit their own items or just testing some behaviour, eventually you’ll wish you can change the values that SharePoitn doesn’t let you change.

The first thing is, as always, to find the value we want to change:

#Add the SharePoint snapin
Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue

#set the web url and the list name to work upon
$url = "http://sharepoint/sites/cthub"
$listName = "Shared Documents"
$fileName = "FileName.xlsx"

#Get the appropriate list from the web
$web = get-SPWeb $url
$list = $web.lists[$listName]

#Get the file using the filename
$item = $list.Items | ? {$_.Name -eq $fileName}

#Print out current Created by and Created date
Write-Output ("item created by {0} on {1}" -f $item["Author"].tostring(), $item["Created"] )

#Print out current Created by and Created date
Write-Output ("item last modified by {0} on {1}" -f $item["Editor"].tostring(), ($item["Modified"] -f "dd-MM-yyyy"))

As you can see we access the item properties by treating the $item as a hashtable and use the property name as the key.

#Set the created by values
$userLogin = "ALEXB\AlexB"
$dateToStore = Get-Date "10/02/1984"

$user = Get-SPUser -Web $web | ? {$_.userlogin -eq $userLogin}
$userString = "{0};#{1}" -f $user.ID, $user.UserLogin.Tostring()

#Sets the created by field
$item["Author"] = $userString
$item["Created"] = $dateToStore

#Set the modified by values
$item["Editor"] = $userString
$item["Modified"] = $dateToStore


#Store changes without overwriting the existing Modified details.
$item.UpdateOverwriteVersion()

Setting the value is a bit more complicated. To do that you have to build the appropriate user string. In my example the user is already part of the site, if they haven’t previously been added to the user information list you’ll need an extra step here to insert them.

The second bit which differs from your usual PowerShell update is the use of the UpdateOverwriteVersion() method. There’s several update methods in SharePoint but only this one will preserve your changes to modified by and created by.

And now, the full script:

<#
Title: Set modified and created by details
Author: Alex Brassington
Category: Proof of Concept Script
Description
This script is to show how to read, modify and otherwise manipulate the created by and modified by details on documents.
This is to enable correction of incorrect data as part of migrations. It is also useful to enable testing of retention policies.
#>

#Add the SharePoint snapin
Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue

#set the web url and the list name to work upon
$url = "http://sharepoint/sites/cthub"
$listName = "Shared Documents"
$fileName = "FileName.xlsx"

#Get the appropriate list from the web
$web = get-SPWeb $url
$list = $web.lists[$listName]

#Get the file using the filename
$item = $list.Items | ? {$_.Name -eq $fileName}

#Print out current Created by and Created date
Write-Output ("item created by {0} on {1}" -f $item["Author"].tostring(), $item["Created"] )

#Print out current Created by and Created date
Write-Output ("item last modified by {0} on {1}" -f $item["Editor"].tostring(), ($item["Modified"] -f "dd-MM-yyyy"))

#Set the created by values
$userLogin = "ALEXB\AlexB"
$dateToStore = Get-Date "10/02/1984"

$user = Get-SPUser -Web $web | ? {$_.userlogin -eq $userLogin}
$userString = "{0};#{1}" -f $user.ID, $user.UserLogin.Tostring()


#Sets the created by field
$item["Author"] = $userString
$item["Created"] = $dateToStore

#Set the modified by values
$item["Editor"] = $userString
$item["Modified"] = $dateToStore


#Store changes without overwriting the existing Modified details.
$item.UpdateOverwriteVersion()

Deleting Versions

Someone asked for a script that could delete previous versions for SharePoint 3.0. I don’t have a 3.0 dev environment but I do have a 2010 build and it interested me.

Function Delete-SPVersions ()
{
[CmdletBinding(SupportsShouldProcess=$true)]
param(
    [Parameter(Mandatory=$True)][string]$webUrl, 
    [Parameter(Mandatory=$True)][string]$listName, 
    [Parameter(Mandatory=$False)][string]$numberOfMajorVersions
    ) 

    #Get the web
    $web = Get-SPWeb $webUrl


    #Get the list
    $list = $web.Lists[$listName]

    $list.Items | % {
        #Get the item in a slightly more usable variable
        $item = $_

        Write-Output ("Deleting versions for item {0}" -f $item.Name)
        
        #Get all major versions
        #Note, the version ID goes up by 512 for each major version.
        $majorVersions = $item.Versions | ? { $_.VersionID % 512 -eq 0}

        #get the largest version number
        $latestMajorID = $majorVersions | select VersionID -ExpandProperty VersionID | sort -Descending | select -First 1

        #Slightly lazy way to get the latest major version and format it as a single point decimal
        Write-Output ("   Latest major version to retain is {0:0.0}" -f ($latestMajorID /512))
        
        #Filter the major versions to only those which are lower than the highest number - 512 * $numberOfMajorVersions
        $majorVersionsToDelete = $majorVersions | ? {$_.VersionID -le ($latestMajorID - 512 * $numberOfMajorVersions)}
        if ($majorVersionsToDelete)
        {
            $majorVersionsToDelete | % {
                Write-Verbose ("  Deleting major version {0}" -f $_.VersionLabel)
                if ($pscmdlet.ShouldProcess($_.VersionLabel,"Deleting major version"))
                {
                    $_.Delete()
                }
            }
        }
        else
        {
            Write-Verbose "No major versions to delete"
        }
        
        #Re-fetch the item to ensure that the versions are still valid
        $item = $list.GetItemByUniqueId($item.UniqueId)
        
        #Get all the minor versions
        $minorVersions = $item.Versions | ? { $_.VersionID % 512 -ne 0}

        #Delete Minor versions greater than the last major version kept
        $minorVersionsToDelete = $minorVersions | ? {$_.VersionID -lt $latestMajorID}
        if ($minorVersionsToDelete)
        {
            $minorVersionsToDelete | % {
                Write-Verbose ("Deleting minor version {0}" -f $_.VersionLabel)
                if ($pscmdlet.ShouldProcess($_.VersionLabel,"Deleting minor version"))
                {
                    $_.Delete()
                }
            }
        }
        else
        {
            Write-Verbose "No minor versions to delete"
        }
    }
    $web.Dispose()
}

Failed Search Scripting

Sometimes knowing what doesn’t work is as useful as what does work. In that vein here’s how I spent my journey home…

A post on technet asked about how to deal with long running search crawls that were impacting users when they overran into business hours. In large SharePoint environments that shouldn’t really happen but it’s a fairly common concern in smaller shops.

Ideally you’ll tune your search so that it always completes in time but that doesn’t always work. For those edge cases there’s two options:

  1. Pause a specific (or all) crawl(s) during working hours.
  2. Reduce the impact of the crawls during working hours

Pausing a crawl is easy, it’s also done very well by other people such as:
Ed Wilson

I wanted to drop the performance of the crawl down so that it can still keep going but not impact the end users.

The first step was to find out how to create a crawl rule to reduce the impact of the search

$shRule = Get-SPEnterpriseSearchSiteHitRule –Identity "SharePoint"

#Cripple search
$shRule.HitRate = 1000
$shRule.Behavior = 'DelayBetweenRequests'
$shRule.Update()

#Revive search
$shRule.HitRate = 8
$shRule.Behavior = 'SimultaneousRequests'
$shRule.Update()

It turns out that in the API a crawl rule is known as a hit rule. Hit rules have two important values, the ‘rate’ and the behaviour.

The script above was enough to let me create a rule and set it to either run at a normal page or with a 16 minute delay between requests. And it worked!

Well, it created a rule and that rule worked. Sadly i’d forgotten that the crawler rules are only checked when you start a crawl. If you start a crawl then punch the delay between items up to 1000 it won’t make a blind bit of difference.

It turns out that pausing the crawl doesn’t make the search engine re-check the crawl rate.

So, a failure. The only thing i can think of is using reflection to find out what is happening in the background and then doing something deeply unsupported to modify the values in flight. Maybe another time.

Check Blob Cache settings across a farm

A routine but really annoying task came up today. The status of BLOB caching for all Web Applications, normally I’d hop into the config files and check but these are spread over 6 farms.
To make things worse each farm had between 4 and 5 web apps and between 1 and 3 WFEs. In total that should have meant checking in the region of 50 config files.

Sod that.

Getting data from the config file is easy, after all it’s just an xml document. Once we’ve got the document it’s a nice easy task to suck the actual values out:

	$xml = [xml](Get-Content $webConfig)
           $blobNode =  $xml.configuration.SharePoint.BlobCache
            
           $props = @{
                'Location' = $blobNode.location;
                'MaxSize' = $blobNode.maxsize;
                'Enabled' = $blobNode.enabled
            }
           New-Object -TypeName PSObject -Property $props
That on it's own isn't that helpful. After all we don't just want to get the values for ONE config file, we want to get them for something like 50. Also I don't really want to have to list each one out, I just want them all... Through some rather excessive PowerShelling I know that we can get the config file's physical path from the IIS settings on a web application:
$WebApp = Get-SPWebApplication "http://sharepoint.domain.com" 
$WebApp.IISSettings
#This is horrible code but I haven't found a better way to get the value in a usable format.
$physicalPath = ($settings.Values | select path -Expand Path).Tostring()

That will give us the physical path to the folder containing the web.config file. It's only a small leap to make it loop through all the locations…

#Script to check the blob caching configuration in all webconfig files in your farm on all servers.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ea SilentlyContinue

$outputFile = "C:\Folder\TestFile.txt"

#This is a rubbish way of getting the server names where the foundation web app is running but i can't
#find a better one at the moment.
$serversWithFWA =Get-SPServiceInstance `
    | ? { $_.TypeName-eq "Microsoft SharePoint Foundation Web Application" -AND $_.Status -eq "Online"} `
    | Select Server -ExpandProperty Server | select Address

	
	
Get-SPWebApplication | %{
 
    $configResults = @()
	$webAppName = $_.name
    Write-Host "Webapp $webAppName"
    
    #Get the physical path from the iis settings
    $settings =$_.IISSettings
    $physicalPath = ($settings.Values | select path -Expand Path).Tostring()
     
    #foreach server running the Foundation Web Application.
    foreach ($server in $serversWithFWA)
    {
        #Build the UNC path to the file Note that this relies on knowing more or less where your config files are
        #This could be improved using regex.
        $serverUNCPath = $physicalPath.Replace("C:\",("\\" + $server.Address + "\C$\"))
        $webConfig = $serverUNCPath +"\web.config"
 
        #If the file exists then try to read the values
        If(Test-Path ($webConfig))
        {
            $xml = [xml](Get-Content $webConfig)
            $blobNode =$xml.configuration.SharePoint.BlobCache

            $props = @{
				'Server' = $server.Address;
                'Location' = $blobNode.location;
                'MaxSize' = $blobNode.maxsize;
                'Enabled' = $blobNode.enabled
            }
            $configResults += New-Object -TypeName PSObject -Property $props
        }
    }
	#Print the results out for the GUI
	$webAppName
    $configResults | ft
	
	#Output the data into a useful format - start by printing out the file name
	$webAppName >>  $outputFile
	#CSV because the data is immeasurably easier to load into a table etc. later. HTML would be a good alternative
	$configResults | ConvertTo-CSV >> $outputFile
}

Automating SharePoint Search Testing

I was browsing technet, as you do, when i found this comment on Search best practices:

We recommend that you test the crawling and querying functionality of the server farm after you make configuration changes or apply updates

http://technet.microsoft.com/en-us/library/cc850696(v=office.14).aspx

This chimed with me as a client I worked with had failed to do this and had paid the price when their Production search service went down for a day.

The article continues:

An easy way to do this is to create a temporary content source that is used only for this purpose. To test, we recommend that you crawl ten items — for example .txt files on a file share — and then perform search queries for those files. Make sure that the test items are currently not in the index. It is helpful if they contain unique words that will be displayed at the top of the search results page when queried. After the test is complete, we recommend that you delete the content source that you created for this test. Doing this removes the items that you crawled from the index and those test items will not appear in search results after you are finished testing

To put that in bullet point format:

  1. Test the search system doesn’t already have test content
  2. Create some test content to search
  3. Create a new content source
  4. Crawl the test content
  5. Search for the test content
  6. Check that the test content is there
  7. Remove the test content by blowing away the content source
  8. Confirm it’s no longer there

It’s a good test script. It also breaks down nicely into some bullet points. With a bit of thought we can break this down into some simple tasks:

  1. Run a search and test the results
  2. Create some files
  3. Create a new content source
  4. Crawl the test content
  5. Run a search and test the results
  6. Remove the test content by blowing away the content source
  7. Run a search and test the results

The common aspect is to run a search three times and test the results. Of course the results will, hopefully, vary depending on when we run that test but we can manage that.

Let’s go with the big one. Running a search:

Function Check-TestPairValue ()
{
<#
.DESCRIPTION
Takes a pipeline bound collection of test values and search terms and searches for
them using the searchPageURL.
Returns either 'Present' or 'Not Found' depending on the result.
Not currently production grade#> 
    [CmdletBinding()]
    Param (
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]$testPair,
    [Parameter(Mandatory=$true)]$searchPageURL
     )
    BEGIN{
        #Create the IE window in the begin block so if the input is pipelined we don't have to re-open it each time.
        $ie = New-Object -com "InternetExplorer.Application"
        $ie.visible = $true
    }
    PROCESS
    { 
        #Get the test value and the search term from the pair
        $testValue = $testPair[0]
        $searchTerm = $testPair[1]
        
        #Open the navigation page
        $ie.navigate($searchPageURL)

        #Wait for the page to finish loading
        while ($ie.readystate -ne 4)
        {
            start-sleep -Milliseconds 100
        }
        Write-Verbose "Page loaded"
        
        #Get the search box
        $searchTextBoxID = "ctl00_m_g_2f1edfa4_ab03_461a_8ef7_c30adf4ba4ed_SD2794C0C_InputKeywords"
        $document = $ie.Document
        $searchBox = $document.getElementByID($searchTextBoxID)

        #enter the search terms
        $searchBox.innerText = $searchTerm
        Write-Verbose "Searching for: $searchTerm - Expected result: $testValue"    
        
        #Get the search button
        $searchButtonID = "ctl00_m_g_2f1edfa4_ab03_461a_8ef7_c30adf4ba4ed_SD2794C0C_go"
        
        #Run the search
        $btn = $document.getElementByID($searchButtonID)
        $btn.click()
        
        #Wait for the results to be loaded
        while ($ie.locationurl -eq $searchPageURL)
        {
           start-sleep -Milliseconds 100
        }
        
        Write-Verbose "Left the page, waiting for results page to load"
        #Wait for the results page to load
        while ($ie.readystate -ne 4)
        {
            start-sleep -Milliseconds 100
        }
        Write-Verbose "Results page loaded"
        #Once loaded check that the results are correct
        $document = $ie.document

        #Check that the search term results contain the test results:
        $firstSearchTermID = "SRB_g_9acbfa38_98a6_4be5_b860_65ed452b3b09_1_Title"
        $firstSearchResult = $document.getElementByID($firstSearchTermID)
        
        $result =""
        
        #test that the title of the file is equal to the search result
        If ($firstSearchResult.innerHTML -match $testValue)
        {
            $result ="Present"
        }
        else
        {
            $result ="Not Found"
        }
        
        Write-Verbose "Test $result"
        
        #Create a new PS Object for our result and let PowerShell pass it out.
        New-Object PSObject -Property @{
            TestCase = $searchTerm
            Result = $result
        }    
    }
    END {
        #Close the IE window after us
        $ie.Quit()
    }
}

Well to be honest that’s the only tricky bit in the process. From there on in it’s plumbing.
We create some test files:


Function Create-SPTestFiles ()
{
[CmdletBinding()]
Param(
    $filesToCreate,
    [string]$folderPath
    )
    
    If (!(Test-Path $folderPath))
    {
    	#Folder doesn't exist.
        Write-Verbose "Folder does not exist - attempting to create"
    	New-Item $folderPath -type directory
    }

    #if the files don’t exist. Create them
    Foreach ($file in $filesToCreate)
    {
         $fileName = $file[0]
    	$filePath = $folderPath + "\" + $fileName
    	If (Test-Path $filePath)
        {
            Write-Verbose "File $fileName already exists. Skipping"
            Write-EventLog -LogName "Windows PowerShell" -Source "PowerShell" -EventId 103 -EntryType Error -Message "Test content already present."
        }
        else
    	{
            Write-Verbose "Creating $fileName"
    		$file[1] >> $filePath
    	}
    }
    Write-Verbose "All files created"
}

We create a content source (this function isn’t perfect here but i’m stealing it from another script)


Function Ensure-TestContentSourceExists ()
{
    [CmdletBinding()]
    Param(
    $sa,
    [string]$contentSourceName,
    [string]$filePath
    )
    $testCS = $sa | Get-SPEnterpriseSearchCrawlContentSource | ? {$_.Name -eq $contentSourceName}
    if ($testCS)
    {
        Write-Verbose "Content Source $contentSourceName already exists"
    }
    else
    {
        Write-Verbose "Content Source $contentSourceName does not exist, creating"
        New-SPEnterpriseSearchCrawlContentSource -SearchApplication $sa -Type file -name $contentSourceName -StartAddresses $filePath | Out-Null
        $testCS = $sa | Get-SPEnterpriseSearchCrawlContentSource | ? {$_.Name -eq $contentSourceName}
    }
    #Output the content source
    $testCS
}

Run the crawl and wait for it to finish.


Function Run-TestCrawl ()
{
    [CmdletBinding()]
    Param ($contentSource)
    #Run a crawl for that content source
    $contentSource.StartFullCrawl()

    #Set a flag to allow us to abort if the duration is excessive
    $stillNotStupidDuration = $true
    $startTime = Get-Date
    $crawlTimeout = 5
    $crawlInitalTime = 2

    Write-Verbose "Starting crawl. Waiting for 2 minutes (Default SharePoint minimum search duration)"
    Sleep -Seconds 120
    #Wait for it to finish
    while ($contentSource.CrawlStatus -ne "Idle" -AND $stillNotStupidDuration -eq $true)
    {
        Write-Verbose "Crawl still running at $timeDifference, waiting 10 seconds"
        Sleep -Seconds 10
        $timeDifference = (Get-Date) - $startTime
        if ($timeDifference.Minutes -gt $crawlTimeout)
        {
            $stillNotStupidDuration = $false
        }
        
    }
    Write-Verbose "Crawl complete"
}

Then we’re back to searching and clean up! Easy.

Of course there’s a little bit more plumbing to be done to stick it all together: so here’s a fully functioning script.

Param (

    #Name of the search service application to test
    $searchAppName = "Search Service Application",

    #Path to the shared folder
    #NOTE: THIS HAS TO BE SETUP BEFORE RUNNING THE SCRIPT MANUALLY (It can be scripted but i haven't)
    $fileSharePath = "\\spintdev\TestFolder",


    #The search page
    $searchSiteURL = "http://sharepoint/sites/search/Pages/default.aspx",

    #Start generating the report
    $reportFolder = "C:\AutomatedTest",
   
    #Flag to set or reject verbose output
    $printVerbose = $false
)


Add-PSSnapin Microsoft.SharePoint.PowerShell -ea SilentlyContinue


Function Process-ASTPassFail ()
{
<#Internal helper function. Will be used for reporting#>
Param($collectionThatShuldBeEmpty,
    $failText,
    $passText
    )

    if ($collectionThathouldBeEmpty -ne $null)
     {
        Write-Warning $failText
        Write-EventLog -LogName "Windows PowerShell" -Source "PowerShell" -EventId 102 -EntryType Error -Message $failText
        $thisTestText = $failText + "`n"
    }
    else
    {
        $sucsessText =  $passText
        Write-Host $sucsessText
        Write-EventLog -LogName "Windows PowerShell" -Source "PowerShell" -EventId 102 -EntryType Information -Message $passText
        $thisTestText = $passText + "`n"
    }
    $thisTestText
}


Function Create-ASTFiles ()
{
<#Creates sometest files for us to search later#>
[CmdletBinding()]
Param(
    $filesToCreate,
    [string]$folderPath
    )
    
    If (!(Test-Path $folderPath))
    {
    	#Folder doesn't exist.
        Write-Verbose "Folder does not exist - attempting to create"
    	New-Item $folderPath -type directory
    }

    #if the files don’t exist. Create them
    Foreach ($file in $filesToCreate)
    {
        $fileName = $file[0]
    	$filePath = $folderPath + "\" + $fileName
    	If (Test-Path $filePath)
        {
            Write-Verbose "File $fileName already exists. Skipping"
            Write-EventLog -LogName "Windows PowerShell" -Source "PowerShell" -EventId 103 -EntryType Error -Message "Test content already present."
        }
        else
    	{
            Write-Verbose "Creating $fileName"
    		$file[1] >> $filePath
    	}
    }
    Write-Verbose "All files created"
}

Function Test-ContentSourceCountAcceptable()
{
[CmdletBinding()]
Param($searchServiceApplication)
    
    #Check the maximum number of content sources allowed
    #http://technet.microsoft.com/en-us/library/cc262787(v=office.14).aspx
    $maxContentSources = 50
    
    $ContentSources = $sa | Get-SPEnterpriseSearchCrawlContentSource

    #Lazy way to check if there is only one item (note, also works for none)
    if ($ContentSources.Count -ne $null)
    {
        $CTSourceCount = $ContentSources.Count
    }
    else
    {
        #Note that this might be wrong if there are no CTs. Not a problem here but it's not a rigourous number
        $CTSourceCount = 1
    }

    #if it is below the limit. Stop and throw an error
    if ($count -ge $maxContentSources)
    {
        #Throw error and let slip the dogs of war
        Write-Verbose "Warning content type count is higher than Microsoft Boundaries"
        $false
    }
    else
    {
        #If we're under the MS limit then return true
        $true
    }
}

Function Ensure-ASTContentSourceExists ()
{
<#Check if conent source already exists. This should be re-written to delete it but for development purposes this is more efficient#>
    [CmdletBinding()]
    Param(
    $sa,
    [string]$contentSourceName,
    [string]$filePath
    )
    $testCS = $sa | Get-SPEnterpriseSearchCrawlContentSource | ? {$_.Name -eq $contentSourceName}
    if ($testCS)
    {
        Write-Verbose "Content Source $contentSourceName already exists. Deleting it."
        Write-EventLog -LogName "Windows PowerShell" -Source "PowerShell" -EventId 100 -EntryType Warning -Message "Unable to create a Content Source as one already exists"
        $testCS.Delete()     
    }
    else
    {
        Write-Verbose "Content Source $contentSourceName does not exist, creating"
        New-SPEnterpriseSearchCrawlContentSource -SearchApplication $sa -Type file -name $contentSourceName -StartAddresses $filePath | Out-Null
        $testCS = $sa | Get-SPEnterpriseSearchCrawlContentSource | ? {$_.Name -eq $contentSourceName}
    }
    #Output the content source - Note that this could result in an error state as a pre-existing one might be re-used.
    $testCS
}

Function Run-ASTCrawl ()
{
<#
.SYNOPSIS
Runs a crawl for a content source and waits until it is complete.
.DESCRIPTION
Runs a crawl for a content source and waits for it to complete, features abort option that will exit the function if the crawl takes too long.
#>
    [CmdletBinding()]
    Param (
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]$contentSource,
    [Parameter(Mandatory=$false,ValueFromPipeline=$false)]$crawlTimeOut = 5
    )
    #Run a crawl for that content source
    $contentSource.StartFullCrawl()

    
    #Start the stopwatch, Note: replace with stopwatch.
    $startTime = Get-Date
    
    #Inital pause time under which there is no point checking for the crawl to be complete
    $crawlInitalTime = 120
    
    #Set a flag to allow us to abort if the duration is excessive
    $stillNotStupidDuration = $true

    Write-Verbose "Starting crawl. Waiting for $crawlInitalTime seconds (Default SharePoint minimum search duration)"
    Sleep -Seconds $crawlInitalTime
    #Wait for it to finish
    while ($contentSource.CrawlStatus -ne "Idle" -AND $stillNotStupidDuration -eq $true)
    {
        Write-Verbose "Crawl still running at $timeDifference, waiting 10 seconds"
        Sleep -Seconds 10
        $timeDifference = (Get-Date) - $startTime
        if ($timeDifference.Minutes -gt $crawlTimeout)
        {
            $stillNotStupidDuration = $false
        }
        
    }
    if ($stillNotStupidDuration)
    {
        Write-Verbose "Crawl complete"
    }
    else
    {
        Write-Warning "No longer waiting for process to complete. Search not finished, results will be unpredictable"
        Write-EventLog -LogName "Windows PowerShell" -Source "PowerShell" -EventId 103 -EntryType Critical -Message "Crawler took longer than the timeout value of $crawlTimeOut so the function exited early."
    }
}


Function Check-ASTPairValue ()
{
<#
.SYNOPSIS
Tests that a search term returns a file with the appropriate name
.DESCRIPTION
Takes a pipeline bound pair of test values and search terms and searches for
them using the searchPageURL page.
Returns either 'Present' or 'Not Found' depending on the result.
.EXAMPLE
$testContent | Check-ASTPairValue -searchPageURL $searchSiteURL 
#> 
    [CmdletBinding()]
    Param (
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]$testPair,
    [Parameter(Mandatory=$true)]$searchPageURL
     )
    BEGIN{
        #Create the IE window in the begin block so if the input is pipelined we don't have to re-open it each time.
        $ie = New-Object -com "InternetExplorer.Application"
        $ie.visible = $true
    }
    PROCESS
    { 
        #Get the test value and the search term from the pair
        $testValue = $testPair[0]
        $searchTerm = $testPair[1]
        
        #Open the navigation page
        $ie.navigate($searchPageURL)

        #Wait for the page to finish loading
        while ($ie.readystate -ne 4)
        {
            start-sleep -Milliseconds 100
        }
        Write-Verbose "Page loaded"
        
        #Get the search box
        $searchTextBoxID = "ctl00_m_g_2f1edfa4_ab03_461a_8ef7_c30adf4ba4ed_SD2794C0C_InputKeywords"
        $document = $ie.Document
        $searchBox = $document.getElementByID($searchTextBoxID)

        #enter the search terms
        $searchBox.innerText = $searchTerm
        Write-Verbose "Searching for: $searchTerm - Expected result: $testValue"    
        
        #Get the search button
        $searchButtonID = "ctl00_m_g_2f1edfa4_ab03_461a_8ef7_c30adf4ba4ed_SD2794C0C_go"
        
        #Run the search
        $btn = $document.getElementByID($searchButtonID)
        $btn.click()
        
        #Wait for the results to be loaded
        while ($ie.locationurl -eq $searchPageURL)
        {
           start-sleep -Milliseconds 100
        }
        
        Write-Verbose "Left the page, waiting for results page to load"
        #Wait for the results page to load
        while ($ie.readystate -ne 4)
        {
            start-sleep -Milliseconds 100
        }
        Write-Verbose "Results page loaded"
        #Once loaded check that the results are correct
        $document = $ie.document

        #Check that the search term results contain the test results:
        $firstSearchTermID = "SRB_g_9acbfa38_98a6_4be5_b860_65ed452b3b09_1_Title"
        $firstSearchResult = $document.getElementByID($firstSearchTermID)
        
        $result =""
        
        #test that the title of the file is equal to the search result
        If ($firstSearchResult.innerHTML -match $testValue)
        {
            $result ="Present"
        }
        else
        {
            $result ="Not Found"
        }
        
        Write-Verbose "Test $result"
        
        #Create a new PS Object for our result and let PowerShell pass it out.
        New-Object PSObject -Property @{
            TestCase = $searchTerm
            Result = $result
        }    
    }
    END {
        #Close the IE window after us
        $ie.Quit()
    }
}

######################################################################################
#Execution script begins here
######################################################################################


#Generate the output file location
$reportFilePath = $reportFolder+  "\SearchTest_Results_" + (Get-Date -Format "dd_MM_yyyy") + ".txt"

#Name of the search service application to test
$searchAppName = "Search Service Application"

#Path to the shared folder
#NOTE: THIS HAS TO BE SETUP BEFORE RUNNING THE SCRIPT MANUALLY (It can be scripted but i haven't)
$fileSharePath = "\\spintdev\TestFolder"


#The search page
$searchSiteURL = "http://sharepoint/sites/search/Pages/default.aspx"

#Start generating the report
$reportFilePath = "C:\AutomatedTest\SearchTest_Results_" + (Get-Date -Format "dd_MM_yyyy") + ".txt"



#All items from here on in are internal and do not have to be specified or modified unless you wish it.

#test content - deliberately junk and non sensical rubbish to trim down search results and avoid false negatives.
#Note: I have no particular insight or interest in the dietry foibles of the politicans listed below.
$testContent = @(
    ("FileA.txt","Miliband loves pie"),
    ("FileB.txt","Osbourne despises soup"),
    ("FileC.txt","Cameron tolerates beans"),
    ("FileD.txt","Clegg loathes eggs which is ironic"),
    ("FileE.txt","Benn likes red meat"),
    ("FileF.txt","Balls desires flan"),
    ("FileG.txt","Cable adores sandwiches"),
    ("FileH.txt","Hunt regrets cake")
)

#Junk content for an additional test to exclude false positive results
$itemToConfirmFailure =@(
"sdkfslskjladsflkj", "lflkfdskjlfdskjfdslkjf"
"sdkfslsfdjklfkjladsflkj", "lflskjfdslkjf"
)

#Only used internally.
$testCTName = "TestSearchContentType"

$startDateTime = Get-Date
$currentComputerName = $env:computername

#Header info
"Automated SharePoint Search Testing Results`n" >> $reportFilePath
"Test started at $startDateTime on Computer $currentComputerName" >> $reportFilePath
        
#Write the first test to the report
"Test 1 - Confirm search terms do not retrieve values `n" >> $reportFilePath
"Confirms that there are no files that can generate a false positive in the system.`n" >> $reportFilePath

Write-Host "Starting tests, checking that there is no pre-existing content that might cause false positives"
 
#Run a search for the testcontent
$deliberatelyFailedResults = @()
$deliberatelyFailedResults +=  $testContent | Check-ASTPairValue -searchPageURL $searchSiteURL -Verbose:$printVerbose
$falsePositives = $deliberatelyFailedResults | ? {$_.Result -eq "Present"}

$errorText = "Test failed, files found by search engine. Results not reliable"
$sucsessText =  "Test Passed, moving to next stage"

$testText = (Process-ASTPassFail -collectionThatShuldBeEmpty $falsePositives -passText $sucsessText -failText $errorText)
$testText >> $reportFilePath 
#Create the test files based on the array above
Create-ASTFiles -filesToCreate $testContent -folderPath $fileSharePath

#Get the search app
$sa = Get-SPEnterpriseSearchServiceApplication -Identity $searchAppName
if ($sa -eq $null)
{
    Write-EventLog -LogName "Windows PowerShell" -Source "PowerShell" -EventId 101 -EntryType Error -Message "Could not find search application $searchAppName"
}


Write-Host "Checking that we are within guidelines for number of Content Sources"
#Test the number of content sources already in place
$numberOfContentSourcesBelowThreshold = Test-ContentSourceCountAcceptable -searchServiceApplication $sa -Verbose:$printVerbose

#Only progress if we're not going to breach the content type limit.
if ($numberOfContentSourcesBelowThreshold)
{
    Write-Host "Within the Acceptable number of Site Collections"
    #Get the content source.
    $testCS = Ensure-ASTContentSourceExists -sa $sa -contentSourceName $testCTName -filePath $fileSharePath -Verbose:$printVerbose
    
    Write-Host "Running the crawl - estimated completion in approximately 2 minutes"
    #Run the crawl and wait for it to complete
    Run-ASTCrawl -contentSource $testCS -Verbose:$printVerbose

    $searchResults = @()
    Write-Host "Crawl Complete, testing links"
    
    $searchResults += $testContent | Check-ASTPairValue -searchPageURL $searchSiteURL -Verbose:$printVerbose
    $failures = $deliberatelyFailedResults | ? {$_.Result -ne "Present"}
    
    #Write the  test to the report
    "Test 2 - Test new content`n" >> $reportFilePath
    "Confirms that search works for our new content.`n" >> $reportFilePath
    
    $errorText = "Test failed, files were not found"
    $sucsessText =  "Passed main test."
    $failures += (Process-ASTPassFail -collectionThatShuldBeEmpty $falsePositives -passText $sucsessText -failText $errorText)

    #Confirm that the test will fail given junk input.
    $deliberatelyFailedResults = @()
    $deliberatelyFailedResults +=  $itemToConfirmFailure | Check-ASTPairValue -searchPageURL $searchSiteURL -Verbose:$printVerbose
    $falsePositives = $deliberatelyFailedResults | ? {$_.Result -eq "Present"}
    
    #Write the  test to the report
    "Test 3 - Check for junk terms `n" >> $reportFilePath
    "Confirms that search doens't find some junk values.`n" >> $reportFilePath
    
    $errorText = "Test failed, files found by search engine when given junk data"
    $sucsessText =  "Passed confirmation test - junk values not found"
    $testText = (Process-ASTPassFail -collectionThatShuldBeEmpty $falsePositives -passText $sucsessText -failText $errorText)
    $testText >> $reportFilePath 
    
    
    #Clean up the content source 
    $CSToDelete = $sa | Get-SPEnterpriseSearchCrawlContentSource | ? {$_.Name -eq $testCTName}
    $CSToDelete.Delete()
    
    #Delete the files
    foreach ($combo in $testContent)
    {
        $fileName = $combo[0]
        $file = Get-ChildItem -Path $fileSharePath | ? {$_.name -eq $fileName}
        $file.Delete()
    }
    #Note that the content source may take a minute to be deleted
    Write-Host "Pausing for 1 minute to allow the index to update"
    Sleep -Seconds 60   

    #Run a search for the testcontent
    $deliberatelyFailedResults = @()
    $deliberatelyFailedResults +=  $testContent | Check-ASTPairValue -searchPageURL $searchSiteURL -Verbose:$printVerbose
    $falsePositives = $deliberatelyFailedResults | ? {$_.Result -eq "Present"}

    #Write the  test to the report
    "Test 3 - Confirm search terms are removed`n" >> $reportFilePath
    "Confirms that the test search content is removed from the system.`n" >> $reportFilePath
    
    $errorText = "Test failed, files found by search engine when given junk data"
    $sucsessText =  "Passed confirmation test. Test files are not present"
    $testText = (Process-ASTPassFail -collectionThatShuldBeEmpty $falsePositives -passText $sucsessText -failText $errorText)
    $testText >> $reportFilePath 
}
else
{
    $errorText = "Error - Unable to create a Content Source as the total number of Content Sources is greater than the Microsoft boundary"
    Write-EventLog -LogName "Windows PowerShell" -Source "PowerShell" -EventId 100 -EntryType Warning -Message $errorText
    $errorText >> $reportFilePath 
}

"Automated SharePoint Search Testing Completed at $(Get-Date) `n" >> $reportFilePath 

So there we have it. A fully functioning automated testing process for SharePoint Search. It would be nice if it sent an email but i’m planning on rolling this into some SCOM work i’m playing with.

I haven’t tested this on 2013 yet, it’d need at least some tweaks to field IDs and maybe more structural work to get the Search API right for 2013. If anyone is interested i’ll knock up a new version.

Gooey SharePoint Scripting

Today i’m going to show you how to script Sharepoint through the GUI. Whilst in this example we’ll be running the code on server the same concepts and approach can be used to Script it from any machine that can hit the relevant website…

Our example might seem to be a little forced but it’s based on a real world experience. We had a client who had a fairly complicated Content Type scenario, over 150 Content Types spread over 8 levels of inheritance with untold columns. Then we discovered an issue and needed to publish every single one of those content types. This is the classic example of where PowerShell should be used but awkwardly they’d been burnt with PowerShell publishing before.
As such we had a flat edict, no PowerShell publishing of content types. It must go through the GUI.

A post i’d seen recently by Dr James McCaffrey popped into my head. It was about using PowerShell to automate testing of web applications using PowerShell.
Why not use the same process to automate the publishing of the content types?

The first thing to do is to get ourselves an IE window:

$ie = New-Object -com "Internet Explorer"
#This starts by default in a hidden mode so let's show it
$ie.Visible = $true

This isn’t much use on its’ own so let’s send it to a page. In our case we want to go to the page to publish one of our content types. We know that the publish page itself is an application page that is referenced from a site collection root web with the following URL syntax:

siteCollectionRoot/_layouts/managectpublishing.aspx?ctype=ContentTypeID

Glossing over how to get the ContentTypeID for now we have this:

$pageUrl= "http://sharepoint/sites/cthub/_layouts/managectpublishing.aspx?ctype=0x0100A4CF347707AC054EA9C3735EBDAC1A7C"
$ie.Naviagte($pageUrl)

Now PowerShell moves fast, so we’ll need to wait for Javascript to catch up.

While ($ie.ReadyState -ne 4)
{
	Sleep -Milliseconds 100
}

Now we’re there, let’s get the publish button. Thankfully this button has a consistent ID that we can get using the trusty F12 button in IE.

Image of Identifying an element's ID uwing F12

Identifying an element’s ID uwing F12

The catchily titled “ctl00_PlaceHolderMain_ctl00_RptControls_okButton” button? Depressingly i think i’m starting to see the naming convention behind these ids…

$textBoxID = "ctl00_PlaceHolderMain_ctl00_RptControls_okButton";
#You have to pass the Document into it's own object otherwise it will fail
$document = $ie.Document
$button= $document.getElementByID($buttonID)

And now all we need to do is to click that button:

$button.Click()

Now you might think that we’ve done all we need to do here and slap it into a foreach loop and be done with it. Of course you can’t do that as you need to give IE time to send that request using our good old friend Javascript.
So we wait for the page to re-direct us:

 
While ($ie.locationurl -eq $url)
{
start-sleep -Milliseconds 100
}

Now we can slap it into a foreach loop and with a little bit of work we can come up with something like the code below:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ea SilentlyContinue

#URL for the content type hub to use
$CTHubURL= "https://sharepoint/sites/cthub"

#Get the Content Type hub
$site = Get-SPSite $CTHubURL

#Content Types to publish
$ContentAndColumns = @(
("Document Type 1"),
("Document Type 2"),
("Document Type 3")
)


#Open a new IE window
$ie = New-Object -com "InternetExplorer.Application"

#Make the window visible
$ie.visible = $true

#Loop through the content types and publish them
foreach ($contentTypeName in $ContentTypes)
{
    
    Write-Verbose "Processing $ContentTypeName"
    
    #Content types live at the root web
    $web = $site.rootWeb
    #Get the content type using it's name
    $ct =   $web.ContentTypes[$ContentTypeName]
    #Get the GUID for the CT
    $GUID = $ct.ID.ToString()
    #Get the URL for the page based on the content type hub url, the application page that does publishing and the GUID
    $url = $CTHubURL+ "/_layouts/managectpublishing.aspx?ctype=" + $GUID  
    #Go to the page
    $ie.navigate($url)
    #Wait for the page to finish loading
    while ($ie.ReadyState -ne 4)
     {
        start-sleep -Milliseconds 100
     }
     #The ID of the button to press
    $buttonID = "ctl00_PlaceHolderMain_ctl00_RptControls_okButton"
    $document = $ie.Document
    $btn = $document.getElementByID($buttonID)
    #Push the button
    $btn.click()
    #Wait for the page to be re-directed
     while ($ie.locationurl -eq $url)
     {
        start-sleep -Milliseconds 100
     }
     Write-Verbose "Content Type $contentTypeName published"
}

I don’t know about you but there is something deeply neat about sitting at your desk watching IE do the dull task that you were convinced was going to bring your RSI back with a vengance, and in half the time you could do it.

This example might not be useful for that many people but the concept is intriguing. There’s no reason most of this can’t be done without any code on the server at all, the only time we use it is to get the GUIDs and those can be pre-fetched if needs be. Nor does it need any significant rights, as long as the account you use has permision to get into that site collection and publish content types then that’s all they need.

The logical destination of this is Office 365, the scripts and rules for running them on there are limited and limiting, they have to be. But the beauty of Scripting is that we don’t have to be limited by the detail of code, we can use higher level components and tools to worry about that for us. In this case, the GUI that microsoft were kind enough to provide us for when it’s too awkward to find the PowerShell console.

Tweaking exercise

One of my colleagues needed a PowerShell script to report on the SharePoint Site Collection Quotas in use on all sites, as well as how much of the site was being used.
Not being a PowerShell or SharePoint expert they asked for a second opinion, since I had an hour an a half of train journey they got a bit more than they expected.

The original Script:

$t = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.quotatemplates
$tFound = $false
$webApp = Get-SPWebApplication | %{$_.Sites} | Get-SPSite -Limit ALL
$webApp | fl Url, @{n=”Storage Used/1MB”;e={[int]($_.Usage.Storage/1MB)}},
@{n=”Storage Available Warning/1MB”; e={[int](($_.Quota).StorageWarningLevel/1MB)}},
@{n=”Storage Available Maximum/1MB”; e={[int](($_.Quota).StorageMaximumLevel/1MB)}},
@{n=”Sandboxed Resource Points Warning”;e={[int](($_.Quota).UserCodeWarningLevel)}},
@{n=”Sandboxed Resource Points Maximum”;e={[int](($_.Quota).UserCodeMaximumLevel)}},
@{n=”Quota Name”; e={ foreach($qt in $t){if($qt.QuotaId -eq [int](($_.Quota).QuotaID)){$qt.Name; $tFound = $true}} if($tFound -eq $false){“No Template Applied”}$tFound=$false;}} >> c:quotaoutput.txt
if($parent) {$webApp.Dispose(); $t.Dispose()}

First. Scripts are written by humans, for humans. Computers might use them but they are meant for us. There’s also a direct correlation between consistency of indenting and code quality. That monolithic block has to go.

$t = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.quotatemplates
$tFound = $false
 

$webApp = Get-SPWebApplication | %{$_.Sites} | Get-SPSite -Limit ALL
$webApp | fl Url, 
	@{n=”Storage Used/1MB”;e={[int]($_.Usage.Storage/1MB)}},
	@{n=”Storage Available Warning/1MB”; e={[int](($_.Quota).StorageWarningLevel/1MB)}},
	@{n=”Storage Available Maximum/1MB”; e={[int](($_.Quota).StorageMaximumLevel/1MB)}},
	@{n=”Sandboxed Resource Points Warning”;e={[int](($_.Quota).UserCodeWarningLevel)}},
	@{n=”Sandboxed Resource Points Maximum”;e={[int](($_.Quota).UserCodeMaximumLevel)}},
	@{n=”Quota Name”; e={ 
	        foreach($qt in $t)
	        {
	            if($qt.QuotaId -eq [int](($_.Quota).QuotaID))
	            {
	                 $qt.Name; 
	                 $tFound = $true
	            }
	        } 
	        if($tFound -eq $false)
	        {
	            “No Template Applied”
	        }
	    $tFound=$false;
	    }
	} >> c:PSoutput.txt
 
if($parent)
 {
	$webApp.Dispose(); 
	$t.Dispose()
}

At this point we can actually work out what happens. A collection of site collections are fetched, then we iterate through each of them, capturing bits of information, and then try to work out if the site has a quota and if so what it is called.

You might already have spotted the second item in there. If not here’s a hint, we’re getting a Collection of Site Collections.

Not a WebApplication, nor even a collection of them.

Note to self and others: Always use meaningful names.

I was slightly confused when I first read this as I assumed the names were meaningful. It took me a second, and a run through in debug mode, to convince myself otherwise.

So, let’s correct that name to something more meaningful. We’re in a simple scenario so we can use something short but descriptive like ‘AllSites’. While we’re there let’s also tidy up that $t to $templates

$templates = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.quotatemplates
$tFound = $false
 
$AllSites= Get-SPWebApplication | %{$_.Sites} | Get-SPSite -Limit ALL
$AllSites| fl Url, 
	@{n=”Storage Used/1MB”;e={[int]($_.Usage.Storage/1MB)}},
	@{n=”Storage Available Warning/1MB”; e={[int](($_.Quota).StorageWarningLevel/1MB)}},
	@{n=”Storage Available Maximum/1MB”; e={[int](($_.Quota).StorageMaximumLevel/1MB)}},
	@{n=”Sandboxed Resource Points Warning”;e={[int](($_.Quota).UserCodeWarningLevel)}},
	@{n=”Sandboxed Resource Points Maximum”;e={[int](($_.Quota).UserCodeMaximumLevel)}},
	@{n=”Quota Name”; e={ 
	        foreach($qt in $templates)
	        {
	            if($qt.QuotaId -eq [int](($_.Quota).QuotaID))
	            {
	                 $qt.Name; 
	                 $tFound = $true
	            }
	        } 
	        if($tFound -eq $false)
	        {
	            “No Template Applied”
	        }
	    $tFound=$false;
	    }
	} >> c:PSoutput.txt
 
if($parent)
 {
	$AllSites.Dispose(); 
	$template.Dispose()
}

Now, that makes it a bit nicer to read. The mislabeled variable is a big hint to our next item, return values from cmdlets. Let’s look at this one line:

$AllSites= Get-SPWebApplication | %{$_.Sites} | Get-SPSite -Limit ALL

Let’s work through what this does. First we get all the WebApplications in the farm, then for each of those we get their sites, then for each of those sites we run the Get-SPSite -Limit All comand for that single site.
Wait, what?
Yup, we get a collection of all the sites and then we step through each and fetch it again. It’s almost surprising it works until you realise just how clever the PowerShell compiler is at converting types.
In fact, all three lines following are equivalent:

	$sites = Get-SPWebApplication | % { $_.Sites} | Get-SPSite –Limit All
	$sites = Get-SPWebApplication | % { $_.Sites} 
	$sites = Get-SPSite –Limit All

Why make things more complicated than they need to be? Let’s go with the last one.

$templates = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.quotatemplates
$tFound = $false

$AllSites =  Get-SPSite -Limit ALL
$AllSites| fl Url, 
	@{n=”Storage Used/1MB”;e={[int]($_.Usage.Storage/1MB)}},
	@{n=”Storage Available Warning/1MB”; e={[int](($_.Quota).StorageWarningLevel/1MB)}},
	@{n=”Storage Available Maximum/1MB”; e={[int](($_.Quota).StorageMaximumLevel/1MB)}},
	@{n=”Sandboxed Resource Points Warning”;e={[int](($_.Quota).UserCodeWarningLevel)}},
	@{n=”Sandboxed Resource Points Maximum”;e={[int](($_.Quota).UserCodeMaximumLevel)}},
	@{n=”Quota Name”; e={ 
	        foreach($qt in $templates)
	        {
	            if($qt.QuotaId -eq [int](($_.Quota).QuotaID))
	            {
	                 $qt.Name; 
	                 $tFound = $true
	            }
	        } 
	        if($tFound -eq $false)
	        {
	            “No Template Applied”
	        }
	    $tFound=$false;
	    }
	} >> c:PSoutput.txt
 
if($parent)
 {
	$AllSites.Dispose(); 
	$template.Dispose()
}

That’s better, but looking down the script there’s another item that has probably grabbed your notice. What the heck is $parent?
I have my suspicions it is orriginally from a 2007 PowerShell script, back when we were still using WSS 3, STSADM, PowerShell V1.0 and dinosaur attacks were listed on the risk register.
Either way this has no place here, if we’re executing in Strict mode (which we should be) then the script won’t even compile. If we’re not then it’ll never fire as $null evaluates to $false.
That’s probably for the best really as $AllSites, being a collection, doesn’t have a .Dispose() method and nor does $template.
Let’s just blow that away completely.

$templates = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.quotatemplates
$tFound = $false

$AllSites =  Get-SPSite -Limit ALL
$AllSites| fl Url, 
	@{n=”Storage Used/1MB”;e={[int]($_.Usage.Storage/1MB)}},
	@{n=”Storage Available Warning/1MB”; e={[int](($_.Quota).StorageWarningLevel/1MB)}},
	@{n=”Storage Available Maximum/1MB”; e={[int](($_.Quota).StorageMaximumLevel/1MB)}},
	@{n=”Sandboxed Resource Points Warning”;e={[int](($_.Quota).UserCodeWarningLevel)}},
	@{n=”Sandboxed Resource Points Maximum”;e={[int](($_.Quota).UserCodeMaximumLevel)}},
	@{n=”Quota Name”; e={ 
	        foreach($qt in $templates)
	        {
	            if($qt.QuotaId -eq [int](($_.Quota).QuotaID))
	            {
	                 $qt.Name; 
	                 $tFound = $true
	            }
	        } 
	        if($tFound -eq $false)
	        {
	            “No Template Applied”
	        }
	    $tFound=$false;
	    }
	} >> c:PSoutput.txt

That’s better still. Sleeker and more readable. On the other hand that .Dispose method should be ringing some bells, as you all know SharePoint is infamous for not properly releasing memory for the key components. Without the .Dispose method the objects will sit in memory until the PowerShell session ends.

In C# we’d have ‘using’ blocks but they don’t really exist in PowerShell. Here we use the pipeline, anything that’s run in a pipeline is disposed of at the end by default.

It just so happens that our $AllSites object is only used once after being declared, by rolling that into the pipeline we can make use of this wonderful feature and streamline our code further!

$templates = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.quotatemplates
$tFound = $false
 
Get-SPSite -Limit ALL| fl Url, 
	@{n=”Storage Used/1MB”;e={[int]($_.Usage.Storage/1MB)}},
	@{n=”Storage Available Warning/1MB”; e={[int](($_.Quota).StorageWarningLevel/1MB)}},
	@{n=”Storage Available Maximum/1MB”; e={[int](($_.Quota).StorageMaximumLevel/1MB)}},
	@{n=”Sandboxed Resource Points Warning”;e={[int](($_.Quota).UserCodeWarningLevel)}},
	@{n=”Sandboxed Resource Points Maximum”;e={[int](($_.Quota).UserCodeMaximumLevel)}},
	@{n=”Quota Name”; e={ 
	        foreach($qt in $templates)
	        {
	            if($qt.QuotaId -eq [int](($_.Quota).QuotaID))
	            {
	                 $qt.Name; 
	                 $tFound = $true
	            }
	        } 
	        if($tFound -eq $false)
	        {
	            “No Template Applied”
	        }
	    $tFound=$false;
	    }
	} >> c:PSoutput.txt

Of course, that doesn’t work because of the aforementioned crapness of SharePoint and it’s memory handling. I’m working on a longer post on how to deal with it but for now just remember to kill your sessions as soon as you can.

So, if you run this you get a nice text file with a rubbish name dumped out at the end. The format might look something like this:

Url : http://mysites:8080
Storage Used/1MB : 2
Storage Available Warning/1MB : 0
Storage Available Maximum/1MB : 0
Sandboxed Resource Points Warning : 100
Sandboxed Resource Points Maximum : 300
Quota Name : No Template Applied

Url : http://sharepoint
Storage Used/1MB : 7
Storage Available Warning/1MB : 0
Storage Available Maximum/1MB : 0
Sandboxed Resource Points Warning : 100
Sandboxed Resource Points Maximum : 300
Quota Name : No Template Applied

I’m liking the data but if you’ve got hundreds of sites that’s going to be a nightmare to go through. It just so happens we can make use of one of the lesser known, but highly awesome, PowerShell features to help us here.

As we all know the world floats on Excel and if we’re honest that’s where this data’s going anyway for us to sort. Let’s dump it out into a CSV file, now we could re-write the format-list statement to dump the stuff out in strings and then concatenate our hearts out.
Or we can change two things, swap fl out for Select and insert ConvertTo-CSV.

$templates = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.quotatemplates
$tFound = $false
 
Get-SPSite -Limit ALL| Select Url, 
	@{n=”Storage Used/1MB”;e={[int]($_.Usage.Storage/1MB)}},
	@{n=”Storage Available Warning/1MB”; e={[int](($_.Quota).StorageWarningLevel/1MB)}},
	@{n=”Storage Available Maximum/1MB”; e={[int](($_.Quota).StorageMaximumLevel/1MB)}},
	@{n=”Sandboxed Resource Points Warning”;e={[int](($_.Quota).UserCodeWarningLevel)}},
	@{n=”Sandboxed Resource Points Maximum”;e={[int](($_.Quota).UserCodeMaximumLevel)}},
	@{n=”Quota Name”; e={ 
	        foreach($qt in $templates)
	        {
	            if($qt.QuotaId -eq [int](($_.Quota).QuotaID))
	            {
	                 $qt.Name; 
	                 $tFound = $true
	            }
	        } 
	        if($tFound -eq $false)
	        {
	            “No Template Applied”
	        }
	    $tFound=$false;
	    }
	} | ConvertTo-CSV >> c:PSoutput-CSV.CSV

That turns our text output into something like this:

#TYPE Selected.Microsoft.SharePoint.SPSite
“Url”,”Storage Used/1MB”,”Storage Available Warning/1MB”,”Storage Available Maximum/1MB”,”Sandboxed Resource Points Warning”,”Sandboxed Resource Points Maximum”,”Quota Name”
“http://mysites:8080″,”2″,”0″,”0″,”100″,”300″,”No Template Applied”
“http://sharepoint”,”7″,”0″,”0″,”100″,”300″,”No Template Applied”
“http://sharepoint/sites/CTHub”,”3″,”0″,”0″,”100″,”300″,”No Template Applied”
“http://sharepoint/sites/sync”,”3″,”0″,”0″,”100″,”300″,”No Template Applied”
“http://sharepoint/sites/TechNet”,”2″,”0″,”0″,”100″,”300″,”No Template Applied”

A hell of a lot uglier but with a little Excel care and attention it’s sortable, filterable and fit for use in a report.

What if we’re not going to be using Excel but we are going to be inspecting by eye, isn’t there a better format there? Well yes there is, you can use the ConvertTo-HTML option and that’ll turn the entire lot into a fully formed HTML file for you. With a modicum of genius and/or a particulary epic book by Don Jones you can add your own CSS and Jquery.

That works, but if this is going to be run more than once I don’t’ want my files overwriting the old ones, or even worse appending (as the script above will do, talk about confusing!)

Let’s slap a date stamp onto our output file:

$outputFolder = "C:"
$path = $outputFolder + "Output-" + (Get-Date -Format "dd-MM-yyyy") + ".txt"

Yes, I’m a Brit, we will use the only sensible date format in this blog.

With a slight modification we’re now here:


$outputFolder = "C:\Results\"
$path = $outputFolder + "Output-" + (Get-Date -Format "dd-MM-yyyy") + ".csv"

$templates = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.quotatemplates
$tFound = $false
 
Get-SPSite -Limit ALL| Select Url, 
	@{n=”Storage Used/1MB”;e={[int]($_.Usage.Storage/1MB)}},
	@{n=”Storage Available Warning/1MB”; e={[int](($_.Quota).StorageWarningLevel/1MB)}},
	@{n=”Storage Available Maximum/1MB”; e={[int](($_.Quota).StorageMaximumLevel/1MB)}},
	@{n=”Sandboxed Resource Points Warning”;e={[int](($_.Quota).UserCodeWarningLevel)}},
	@{n=”Sandboxed Resource Points Maximum”;e={[int](($_.Quota).UserCodeMaximumLevel)}},
	@{n=”Quota Name”; e={ 
	        foreach($qt in $templates)
	        {
	            if($qt.QuotaId -eq [int](($_.Quota).QuotaID))
	            {
	                 $qt.Name; 
	                 $tFound = $true
	            }
	        } 
	        if($tFound -eq $false)
	        {
	            “No Template Applied”
	        }
	    $tFound=$false;
	    }
	} | ConvertTo-CSV >>  $path

We’ve turned a script that shouldn’t even run into something that’s more legible, probably faster (more to come on this I hope), more efficient and giving more useful results.

What haven’t we done? We haven’t touched on the, frankly brutal, RAM leaks which are the massive elephant in the room. This script will make your server cry, if it’s a really large farm then it might even impact the stability or performance of your CA box or wherever you run it. If you’ve got thousands of site collections I recommend running this out of hours with Task manager open and a hand hovering over Ctrl + C.

What next? Elephant hunting and SPAssignments

Create A Document Library Based on a Content Type

Something that’s been winding me up for quite a while is the time needed to create a library, enable content types, add a content type to it, remove the OOB ‘Document’ Type and then finally create a new view that includes the fields on the content type.

It’s not much in the grand scheme of things. Probably about 5 minutes a time. But if you’re creating one, you’ll probably be creating another, and another, and another.

So, let’s do something about that, this script should do everything in the description above. Plus let’s throw in a flag that’ll let us create a list at the same time instead of just a Document Library.

Function Create-DocumentLibraryBasedOnContentType
{

[CmdletBinding()]
Param ([parameter(Mandatory=$true)][string] $ContentTypeName,
       [parameter(Mandatory=$true)][string] $documentLibraryName,
       [parameter(Mandatory=$true)][string] $SPWebURL,
       [parameter(Mandatory=$false)][switch] $IsAList,
       [parameter(Mandatory=$false)][switch] $ShowOnQuickLaunch)
    BEGIN   
        {
            
            Write-Verbose "Beginning Create-DocumentLibraryBasedOnContentType"
            
            $SPWeb = Get-SPWeb $SPWebURL
            $SPSite = $SPWeb.Site
            if ($SPWeb.Lists[$documentLibraryName] -ne $null)
            {
                Write-Error "Library already exists. Aborting" -ErrorAction Stop
            }
            #The default behaviour is to create a document library
            if ($IsAList)
            {
                $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::GenericList
                $listOrLibrary = "List"
            }
            else
            {
                $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
                $listOrLibrary = "Document Library"
            }
        }
    PROCESS
        {
            #get the content Type
            Write-Verbose "Getting content type $contentTypeName in site $siteURL"

            #Confirm that the content type exists.
            $contentType = $SPSite.RootWeb.ContentTypes | where {$_.Name -eq $contentTypeName}

            ##Abort if not found.
            if ($contentType -eq $null)
            {
                Write-Error "$ContentTypeName not found in site collection. Aborting" -ErrorAction Stop
            }
            else
            {
                #Create a library
                Write-Verbose "Creating Default $list"
                
                
                #Create the document Library, stripping out the spaces in the URL for cleanliness
                $libGuid =   $SPWeb.Lists.Add($documentLibraryName.Replace(' ',''),'',$listTemplate) 
                $docLib = $SPWeb.Lists[$libGUID]
                
                Write-Verbose "$listOrLibrary Created, updating settings"
                #Correct the title to appear without spaces
                $docLib.Title = $documentLibraryName
              
                #Add to the quick launch if relevant.
                if($ShowOnQuickLaunch)
                {
                    Write-Verbose "Adding to quick launch"
                    $docLib.OnQuickLaunch = $true
                }
                
                ##Allow modification of content types
                $docLib.ContentTypesEnabled = $true
                
                ##add content type
                 Write-Verbose "Adding the content type"
                $docLibInSitu = $doclib.ContentTypes.Add($contentType)
                
                ##remove 'document'
                if ($IsAList)
                {
                    $doclib.ContentTypes["Item"].Delete()
                }
                else
                {
                    $doclib.ContentTypes["Document"].Delete()
                }
                ##create view and set to default
                Write-Verbose "Creating Default View"
                $newView = $docLib.Views[0].clone("Default", 30, $true, $true)
                $newView.Update()
            }
        }
    END {
            $docLib.Update()
            $SPSite.Dispose()
            $SPWeb.Dispose()
            Write-Verbose "Ending Create-DocumentLibraryBasedOnContentType" 
        }
}

And the script to run it:

$contentTypeName = "AlexB_Document1b"
$siteURL = "http://SharePoint/sites/Subscriber/"
$docLibName = "Test DocLib"

Create-DocumentLibraryBasedOnContentType -ContentTypeName $contentTypeName -documentLibraryName $docLibName -SPWebURL $siteURL -Verbose -List -ShowOnQuickLaunch