PortiBlog

Automating site creation

6 november 2014

For an automatic site creation we have the ability to use a lot of tools, like SharePoint Self-Service Site Creation, Workflows, Nintex, PowerShell and a lot of other tools. For an automatic site creation process I created some PowerShell scripts. This example is to show you how I handle the site requests within a SharePoint list, use PowerShell to create the site and the Task Scheduler to run the PowerShell script. The list and script are an example, I guess you want to store more information in the list like owners, templates, etc. This example is to show you how you can automatically create a site by a user request within minutes.

Summary:

  1. Create a list
  2. Create a script
  3. Add the script to the task schedular
  4. Requested site is created.

Steps for automating site creating with a SharePoint list and PowerShell:

1. Create a list where users can add there site request. The list has to have a least to following fields:

  • Title (Text)
  • Status (Choice: Requested, Approved, Created).
  • URL (Hyperlink)

script1
The users have the ability to add a new item in this list. You can create a workflow to approve the requests and update the status field by the workflow.


2. Create a PowerShell script “siteCreation.ps1”. The script will create a new web for all items in the Teamsites list with status Approved.


if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
 Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$NewSiteURL = "http://portal/teams/"
$sourceWebURL = "http://portal/site/"
$sourceListName = "Teamsites"

$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
$spSourceItems = $spSourceList.Items | where {$_['Status'] -eq "Approved"}
$list = $spSourceWeb.Lists[$sourceListName]
$spSourceItems | ForEach-Object {
 $Title = $_['Title']
 $url = $NewSiteURL + "/" + $Title
 Write-Host "Create site: $Url"
 New-SPWeb –url $url -name $Title -template STS#0 
 $_['Status'] = "Created"
 $_['URL'] = $url + ", " + $Title 
 $_.Update()
}
 

3. Create a task that will run every hour and start the site creation script.


$A = New-ScheduledTaskAction -execute "powershell" -argument "-nologo -noprofile -noninteractive E:\[location of script]\siteCreation.ps1"
$T = New-ScheduledTaskTrigger -Once -At 17:00PM
$T.RepetitionInterval = (New-TimeSpan -Minutes 60)
$T.RepetitionDuration = ([Timespan]::MaxValue)
Register-ScheduledTask -TaskName "SharePoint site creation" -Trigger $T -Action $A -description "Run site creation script every hour." -User "$env:USERDOMAIN\$env:USERNAME" -Password 'p@ssword' -RunLevel 1


4. The script will create a new web for all the requested sites in the Teamsites list with status Approved. After the site creation the status of the item will be modified to Created and the URL to the web is filled in.

script2 script3

 

Submit a comment