Tag Archive for PowerShell

Show Web Template

Carrying on from my previous post Hide Web Template, this post demonstrates how to show a web template in the New Site dialog for a specific web using a PowerShell script:

function Add-SPWebTemplate {
	[CmdletBinding()]
	param(
		[parameter(Position=1,Mandatory=$true)][Microsoft.SharePoint.SPWeb]$web
	)
	process {
		$templates = New-Object "System.Collections.ObjectModel.Collection``1[[Microsoft.SharePoint.SPWebTemplate, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c]]"
		$availableWebTemplates = $web.GetAvailableWebTemplates(1033);
 
		$availableWebTemplates | ForEach-Object {
			if ($_.ID -eq 53) {
				Write-Host ("Blank Internet web template is already enabled for web '", $_.Title, "' at ", $_.Url -join "") -NoNewline
				return
			}
 
			$templates.add($_)
		}
 
		$web.AllowAllWebTemplates()
		$allWebTemplates = $web.GetAvailableWebTemplates(1033);
 
		$allWebTemplates | Where{ $_.ID -eq 53 } | ForEach-Object {
			$templates.add($_)
		}
 
		Write-Host ("Enabling Blank Internet web template for web '", $_.Title, "' at ", $_.Url -join "") -NoNewline
		$web.SetAvailableWebTemplates($templates, 1033);
		$web.Update()
		Write-Host " - Done." -ForegroundColor Green
	}
}
 
 
Get-SPWebApplication | ForEach-Object {
	$_.Sites | Where { $_.ServerRelativeUrl -ne '/sites/example' } | ForEach-Object {
		$_.AllWebs | ForEach-Object {
			Add-SPWebTemplate $_
		}
	}
}

Read more

Hide Web Template

In PowerShell, if you want to remove a web template from the New Site dialog for a specific web, take a look at the following script:

function Remove-SPWebTemplate {
	[CmdletBinding()]
	param(
		[parameter(Position=1,Mandatory=$true)][Microsoft.SharePoint.SPWeb]$web
	)
	process {
		$templateList = New-Object "System.Collections.ObjectModel.Collection``1[[Microsoft.SharePoint.SPWebTemplate, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c]]"
 
		$availableWebTemplates = $web.GetAvailableWebTemplates(1033);
		$availableWebTemplates | Where{ $_.ID -ne 53 } | ForEach-Object {
		     $templateList.add($_)
		}
 
		if ($templateList.Count -gt 0) {
			Write-Host ("Removing Blank Internet web template with from web '", $_.Title, "' at ", $_.Url -join "") -NoNewline
			$web.SetAvailableWebTemplates($templateList, 1033);
			$web.Update()
			Write-Host " - Done." -ForegroundColor Green
		}
		else {
			Write-Host ("No web templates found for web ", $_.Url -join "") -ForegroundColor Red
		}
	}
}
 
Get-SPWebApplication | ForEach-Object {
	$_.Sites | Where { $_.ServerRelativeUrl -ne '/sites/example' } | ForEach-Object {
		$_.AllWebs | ForEach-Object {
			Remove-SPWebTemplate $_
		}
	}
}

Read more

Export MetaData Terms

Central administration provides a way to import metadata term sets and terms by uploading a CSV file. However, it does not provide a mechanism for exporting terms to a CSV.

There are quite a lot of PowerShell and command line utilities on the web for accomplishing this, however, the ones that I found did not cater for terms that are more than one level deep. After all, you can create a term hierarchy that is up to seven levels deep.

So, to help fill this gap I’ve written a simple PowerShell script that does exactly this.

One caveat with importing CSV files through Central Administration is that it can only import one term set per CSV. So when exporting more than one set you have to create a separate file for each of them. You’d have thought the import script would have been able to handle multiple sets…
Read more

SPRequest ULS Entries

The SharePoint ULS logs always report objects not being explicitly disposed of, even for some of the SharePoint object model calls, although these are supposedly false positives.

Now, not all entries that are flagged will be issues, but initially there’s no apparent easy way of determining if the object is indeed causing a memory leak.

A sample log entry will look like:

An SPRequest object was reclaimed by the garbage collector instead of being explicitly freed. To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it. Allocation Id: {FDE3179D-E9C7-476B-9E60-ED68C55827ED} To determine where this object was allocated, set Microsoft.SharePoint.Administration.SPWebService.ContentService. CollectSPRequestAllocationCallStacks = true.

The last line of this entry indicates a way of getting more useful information to allow you to diagnose the problem and determine if it requires fixing.
Read more

Start SharePoint Timer Job Script

I’ve written a PowerShell script that starts a SharePoint timer job, which is useful if you do a lot of work that relies on certain timers firing and don’t want to have to keep going into the Central Administration site to manually start the desired timer. For instance, the Workflow timer fires every 15 minutes by default and although you could reduce this to 1 minute, sometimes you just want to try and force it to start. If, like me, you don’t have the patience to wait up to a minute for it to execute 🙂

The script is pretty simple really, the main component being

job-workflow | Start-SPTimerJob

Now, this on its own isn’t much use, so the following script allows you to run it and receive confirmation that the process was started.
Read more

Follow

Get every new post delivered to your Inbox

Join other followers: