Get all subsites of a subsite

Getting a list of all subsites of a particular site (not a site collection) was a little more work than I expected, so here is how I did it.

Let’s say we have the following situation site structure:

SiteStructure

What if we want an overview of all sites under “Https://portal.sharepointrelated.com/Projects”?

My first thought was to use the “Webs” property of the SPWeb object. Unfortunately, this only shows the direct subsites for this site. This means that for “Https://portal.sharepointrelated.com/projects”, it only shows the Level 3 sites.

Solution

To work around this, I used the “AllWebs” property of the SPSite object and filtered the URL’s starting with “Https://portal.sharepointrelated.com/projects”.

Here is the code used: (Download .zip file)


param ( [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] [String]$StartWeb, [Boolean]$IncludeStartWeb = $true )

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$subsites = ((Get-SPWeb $StartWeb).Site).allwebs | ?{$_.url -like "$StartWeb*"}

foreach($subsite in $subsites) { Write-Host $subsite.url }

As you can see in the source code, I added 2 parameters to the script:

StartWeb: String. This is the starting URL. All subsites under this site will be showed in the result.

IncludeStartWeb: Boolean. When set to $false, the output will not include the URL provided in the StartWeb parameter.

Advertisements

When were my site collections created?

Have you ever wondered when your site collections were created?
Try the following one liner to get all the information you need:

Get-SPSite -Limit All | %{$root = $_.rootweb; $date = $root.created.toShortDateString(); Write-Host "$($root.url) was created on $($date)"}

image

Resolving “The super user account utilized by the cache is not configured.” by using PowerShell

We have all seen the following error in the Application Event viewer after installing a clean SharePoint installation and creating site collections (Event ID 7362):

Object Cache: The super user account utilized by the cache is not configured. This can increase the number of cache misses, which causes the page requests to consume unneccesary system resources.

The first time I saw this error I figured there had to be a way to do this in PowerShell. This post helped me achieve my goal: Mirjam’s blog.

Because I implement several new farms each month, I was getting annoyed that I had to set the permissions and then use PowerShell to set the accounts for each web application.

I created the following PowerShell script to enable the caching for all Web applications. The 2 users that are explained in Mirjam’s blog have to already exist in the current environment.

If there are any questions or remarks regarding the script I would be pleased to hear them, so I can update the script and hopefully improve it. So here it is:


# This script will add the superuser and superreader account to the Policy for web app
# and also set the web application properties so that the users will be used for caching
# The script is designed in a way that you do NOT have to adjust the script before use
Add-PSSnapin Microsoft.SharePoint.PowerShell

# Function
function addUsersToPolicyAndProperties
{
$webapps = get-spwebapplication
$superuser = Read-Host "Fill in the SuperUser account: <Domain><Username>"
$superreader = Read-Host "Fill in the SuperReader: <Domain><Username>"

#foreach statement that will loop through all web applications
foreach($webapp in $webapps)
{
try {
$SUpolicy = $webapp.Policies.Add($superuser,$superuser)
$SUpolicy.PolicyRoleBindings.Add($webapp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl))
$SRpolicy = $webapp.Policies.Add($superreader,$superreader)
$SRpolicy.PolicyRoleBindings.Add($webapp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullRead))
$webapp.Properties["portalsuperuseraccount"] = [string]$superuser
$webapp.Properties["portalsuperreaderaccount"] = [string]$superreader

$webapp.Update()

Write-Host "$webapp.displayname has been configured correctly"
}
catch
{
Write-Host "Error adding the users for $webapp.displayname" -fore red
}
}
Write-Host "All web applications are done, press any key to exit" -fore green
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
#Call function

addUsersToPolicyAndProperties

Resolving "The super user account utilized by the cache is not configured." by using PowerShell

We have all seen the following error in the Application Event viewer after installing a clean SharePoint installation and creating site collections (Event ID 7362):

Object Cache: The super user account utilized by the cache is not configured. This can increase the number of cache misses, which causes the page requests to consume unneccesary system resources.

The first time I saw this error I figured there had to be a way to do this in PowerShell. This post helped me achieve my goal: Mirjam’s blog.

Because I implement several new farms each month, I was getting annoyed that I had to set the permissions and then use PowerShell to set the accounts for each web application.

I created the following PowerShell script to enable the caching for all Web applications. The 2 users that are explained in Mirjam’s blog have to already exist in the current environment.

If there are any questions or remarks regarding the script I would be pleased to hear them, so I can update the script and hopefully improve it. So here it is:


# This script will add the superuser and superreader account to the Policy for web app
# and also set the web application properties so that the users will be used for caching
# The script is designed in a way that you do NOT have to adjust the script before use
Add-PSSnapin Microsoft.SharePoint.PowerShell

# Function
function addUsersToPolicyAndProperties
{
$webapps = get-spwebapplication
$superuser = Read-Host "Fill in the SuperUser account: <Domain>\<Username>"
$superreader = Read-Host "Fill in the SuperReader: <Domain>\<Username>"

#foreach statement that will loop through all web applications
foreach($webapp in $webapps)
{
try {
$SUpolicy = $webapp.Policies.Add($superuser,$superuser)
$SUpolicy.PolicyRoleBindings.Add($webapp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl))
$SRpolicy = $webapp.Policies.Add($superreader,$superreader)
$SRpolicy.PolicyRoleBindings.Add($webapp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullRead))
$webapp.Properties["portalsuperuseraccount"] = [string]$superuser
$webapp.Properties["portalsuperreaderaccount"] = [string]$superreader

$webapp.Update()

Write-Host "$webapp.displayname has been configured correctly"
}
catch
{
Write-Host "Error adding the users for $webapp.displayname" -fore red
}
}
Write-Host "All web applications are done, press any key to exit" -fore green
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
#Call function

addUsersToPolicyAndProperties