Tuesday, March 5, 2013

Install WSP Solutions

Add and deploy features in SP2010

http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/12/02/adding-and-deploying-solutions-with-powershell-in-sharepoint-2010.aspx

Add assembly to GAC using powershell

http://blog.goverco.com/2012/04/use-powershell-to-put-your-assemblies.html

Powershell batch file to remove and install wsp file again, need to remove last line and add -force attribute to deploy command

http://jstevensblog.com/post/Powershell-Script-to-Automatically-Deploy-Sharepoint-WSP-Packages.aspx

Finally, above batch file looks like shown below after adding commands to deploy farm level solution and activate features at the web application level. Please note, this file needs to be saved in the folder where wsp files to be installed are located, and also this file would install all wsp files in the folder to all web applications at the farm.

# Change Powershell execution directory to the scripts folder
$scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
Set-Location $scriptBase

# Declare constants
$packages = (dir *.wsp | Select-Object name
$currentDir = (Get-Location).Path 
$urlWebApplication = "http://sharepoint:1234/"

if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0))
{
    Write-Progress -Activity "Loading Sharepoint Powershel Snapin" -Status "Loading Microsoft.SharePoint.PowerShell"
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}


Start-SPAssignment -Global    # This cmdlet takes care of the disposable objects to prevent memory leak.
Write-Host "Started package installation"

# Wait for existing timer jobs to finish
function WaitForJobToFinish ([string]$solutionName)   
{
    $JobName = "solution-deployment-$solutionName*"   
    $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.."   
    }
}

# Retract each solution .wsp file at web application
foreach ($i in $packages
    Write-Host -NoNewLine "Waiting for deployment jobs to finish..."           
    while ((Get-SPTimerJob  | ?{$_.Name -like "solution-deployment*"}) -ne $null)        
    {
        Write-Host -NoNewLine .
        Start-Sleep -Seconds 2       
    }
    Write-Host  "Finished waiting for job.."   
    Write-Host "Retracting: "$i.Name
    $solution = (Get-SPSolution | where-object {$_.Name -eq $i.Name}) 
    Write-Host $solution.Name 

   

    #Uninstall soltion
    if ($solution -ne $null
    { 
        Write-Host "Solution Found..."

        # Deactivate features
        $features = Get-SPFeature -Limit ALL | where {$_.solutionId -eq $solution.SolutionId}
        foreach($feature in $features)
        {
            If($feature.Status -ne "Offline")
            {
                Write-Host  "Deactivating Feature: "$feature.Name
                Disable-SPFeature -identity $feature.Id -URL $urlWebApplication -Confirm:$false
            }
        }
        if ($solution.Deployed -eq $true
        { 
            Write-Host "Uninstalling..."
            try
            {
                if ($solution.ContainsWebApplicationResource)
                {
                    Uninstall-SPSolution -Identity $i.Name -AllWebApplications -Confirm:$false
                }
                else
                {
                    Uninstall-SPSolution -Identity $i.Name -Confirm:$false
                }
            }
            catch
            {
                Uninstall-SPSolution -Identity $i.Name -Confirm:$false 
            }
        }
        do
        {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2
        }
        until ($solution.Deployed -eq $false)
        WaitForJobToFinish $i.Name
        Start-Sleep -Seconds 5
        Write-Host "Retract Completed!"
        Write-Host "Removing Solution..."
        Remove-SPSolution -Identity $i.Name -Force -Confirm:$false
        Write-Host "Remove Completed!"
    } 
    else
    { 
        Write-Host "WSP Package was not previously installed, moving to next step"
    } 

# Add each solution .wsp file at the web application and activate features
foreach ($i in $packages)
    Write-Host "Deploying: " $i.Name
    $solution = (Get-SPSolution | where-object {$_.Name -eq $i.Name}) 
    if ($solution -eq $null
    { 
        Write-Host "Adding Solution: " $i.Name
        $solution = Add-SpSolution -LiteralPath ($currentDir + "\" + $i.Name) 
        WaitForJobToFinish $solution.Name
        Write-Host "Deployment Completed!"
        Write-Host "Installing Solution: " $i.Name
        try
        {
            if ($solution.ContainsWebApplicationResource)
            {
                Write-Host "Installing for web application " $urlWebApplication
                Install-SPSolution -Identity $solution.Name -WebApplication $urlWebApplication -GACDeployment -force
            }
            else
            {
                Install-SPSolution -Identity $solution.Name -GACDeployment -force
            }
        }
        catch
        {
            Install-SPSolution -Identity $solution.Name -GACDeployment -force
        }
        do
        {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2
        }
        until ($solution.Deployed -eq $true)
        WaitForJobToFinish $i.Name
        Start-Sleep -Seconds 5
        Write-Host "Install Completed!"
        # Activate features
        $features = Get-SPFeature -Limit ALL | where {$_.solutionId -eq $solution.SolutionId}
        foreach($feature in $features)
        {
            Write-Host  "Activating Feature:" $feature.Name
            Enable-SPFeature -identity $feature.Id -URL $urlWebApplication
        }
    } 
}
Stop-SPAssignment -Global    # This cmdlet takes care of the disposable objects to prevent memory leak.



Also there is a related script to activate web application, site collection or web level features based on a CSV file available to download at http://www.sharepointpals.com/post/Activating-Feature-in-Web-SiteCollection-WebApplication-Scopes-using-PowerShell-in-SharePoint-2013

No comments:

SSL Error - The connection for this site is not secure

 After cloning a git repo of dot net framework website and trying to run it all I could see was this error Turns out the fix was to simply e...