Thursday, April 12, 2012

Creating and deploying site template(wsp) using powershell

Follwoing code to install and deploye site

# Parameter Section Start
$languagePack="{1033}" # this line is used to notify the language pack used by the sharepoint server

$path= "C:\Test\Test1\Deployment\Sites\TestSite.wsp"  # path of the wsp to be read
$feature =  " Test Site_Feature4" #Feature Name of the wsp to be activated which should be site as scope
$solution =  " Test Site.wsp" # Solution name for the wsp which is going to be installed and deployed
$targetUrl = "http:// Test 1:22222/ Test /" # Target location of the site with its url going to be created
$targetWebAppUrl = "http:// Test 1:22222" # web application URl where the site will be deployed
$targetSiteColUrl = "http:// Test 1:22222" # site collection URl where the site will be deployed
$SiteTitle = " Test " # Title of the site which will be fetched from web template
$SiteName =" Test " # Name of the site which will be created
$SiteDesc = " Test  Site" # Description of the site which will be created
# Parameter Section End

# Function Section Start # Below code is used as a timer job for the installation and retracting process
function WaitForJobToFinish([string]$SolutionFileName)
{
    $JobName = "*solution-deployment*$SolutionFileName*"
    $job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
    if ($job -eq $null)
    {
        Write-Host 'Timer job not found'
    }
    else
    {
        $JobFullName = $job.Name
        Write-Host -NoNewLine "Waiting to finish job $JobFullName"
     
        while ((Get-SPTimerJob $JobFullName) -ne $null)
        {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2
        }
        Write-Host  "Finished waiting for job.."
    }
}
# Function Section End


# Main Section Start
Add-PsSnapin Microsoft.SharePoint.PowerShell

$user=[System.Security.Principal.WindowsIdentity]::GetCurrent().Name

Write-Host  "Deploying Solution Started.."

$WSP = Get-SPSolution | Where {
    ($solution -eq $_.Name)
}
if($WSP -eq $null) {
}
else
{
    Write-Host  "De-Activating feature -"$feature
    Disable-SPFeature -identity $feature -Url $targetSiteColURL -Confirm:$false
    Write-Host  "Feature de-activated - "$feature
 
    Write-Host  "Found " $WSP " - Uninstalling."
    Uninstall-SPSolution -Identity $solution -Confirm:$false
    WaitForJobToFinish($solution)
    Write-Host  $WSP "Uninstalled."
 
    Write-Host  "removing Solution -" $solution
    Remove-SPSolution -Identity $solution -Confirm:$false
    WaitForJobToFinish($solution)
    Write-Host  "removed Solution -" $solution
 
    Write-Host  "Deleting target site -" $targetUrl
    Remove-SPWEB -identity $targetUrl -Confirm:$false
    Write-Host  "Deleted target site - " $targetUrl

 
}



Write-Host  "Adding Solution"
Add-SPSolution -LiteralPath $path -Confirm:$false
Write-Host  "Added Solution"

Write-Host  "Installing Solution"
Install-SPSolution -Identity $solution -GACDeployment -Confirm:$false
WaitForJobToFinish($solution)
Write-Host  "Installed Solution"

Write-Host  "Activating feature - "$feature
Enable-SPFeature -identity $feature -Url $targetWebAppURL -Confirm:$false
Write-Host  "Feature activated - " $feature

#Below code finds the TemplateID for the site
$site= new-Object Microsoft.SharePoint.SPSite($targetWebAppUrl)
$loc= [System.Int32]::Parse(1033)
$templates= $site.GetWebTemplates($loc)
$templateId=""
foreach ($child in $templates)
{  
    if($child.Title -eq $SiteTitle)
    {
        $templateId= $child.Name
   
    }

}
$site.Dispose()



Write-Host  "Creating New site with templateID -" $templateId
$web = New-SPWeb -Url $targetUrl -Name $SiteName -Description $SiteDesc -AddToTopNav -Confirm:$false
Write-Host "New Site Created"
Write-Host "Applying template please wait....."
$web.ApplyWebTemplate($templateId)
$web.Dispose()
Write-Host "Site template applied successfully"
Write-Host -Fore Green "Solution deployment completed"
Write-Host "To visit the created site Browse - "-Fore Blue $targetUrl
get-pssession | remove-pssession

# Main Section End

//Using batch file to run the above script which we will name as InstallSite.ps1
Call it using batch file (.bat) which would contain follwoing code to run:

@echo off
@echo Creates and deploys Test.wsp
@echo args are folder path for solution file

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe  " & ' C:\ Test \ Test \Deployment\Sites\InstallSite.ps1'

@Pause



Similarly the webparts can be deployed just we need to check the scope of the webpart. and deploying site section wont be included.

No comments:

Post a Comment