Friday, February 12, 2016

Automating site creation


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. Also you can able to create your own power-shell script using below steps

Summary:

  1. Create a list
  2. Create a script
  3. Add the script to the task scheduler
  4. Requested site is created.
Steps for automating site creating with a SharePoint list and Power-shell:
  1. Create a list where users can add there site request. The list has to have a least to following fields:
    1. Title (Text)
    2. Status (Choice: Requested, Approved, Created).
    3. 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.

No comments: