Pages

Search This Blog

Wednesday, April 1, 2015

PowerShell script for deploying multiple WSP solutions

In case of big SharePoint Farms with large application domain we need to deploy multiple solutions, Doing it manually will be a lot tough and time consuming task. So a more manageable way could be to create a PowerShell script, that runs on the Farm to deploy the listed wsp solutions. I created one of such solution for a client who had around 50-60 such solutions. Below is the script that you can use to deploy wsp's solutions to SharePoint Farm. This script should be placed inside a folder along with the wsp solutions which needs to be deployed. 

Copy-Paste the below script in a text file and rename its extension to - *.ps1


function LoadSharePointPowerShellEnvironment
{
       write-host
       write-host "Setting up PowerShell environment for SharePoint..." -foregroundcolor Yellow
       write-host
       Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
       write-host "SharePoint PowerShell Snapin loaded." -foregroundcolor Green
}

function DeploySolution($packageName, $packagePath, $WebAppUrl)
{
         $spAdminServiceName = "SPAdminV4"
         #check if solution already exists
         $solution = Get-SPSolution $packageName -ErrorAction SilentlyContinue
         if ($solution -ne $null)
         {
            #Write-Host
               #Write-Warning "The solution $packageName already exists. Press Enter if you want to deploy that solution,
               #else first uninstall the solution and re-run the exe."
               #Read-Host
         #Deploy the solution
         if ($solution.Deployed)
               {
            Write-Host
               Write-Host " $packageName Already deployed... " -foregroundcolor Red
         }
         else
         {
                 if (!$solution.ContainsWebApplicationResource)
                 {
                      Write-Host "Deploying solution $packageName to the Farm..."
                      $solution | Install-SPSolution -GACDeployment -Force -Confirm:$false
                 }
                 else
                 {
                      if ($WebAppUrl -eq $null -or $WebAppUrl.Length -eq 0)
                      {
                        Write-Warning "The solution $packageName contains web application resources but no web applications were specified to deploy to."
                        return
                      }
                      else
                      {
                        Write-Host "Installing solution $packageName to WebAppUrl"
                        $solution | Install-SPSolution -GACDeployment  -WebApplication $WebAppUrl -Force -Confirm:$false
                      }
                 }

                 Write-Host
                 Write-Host "starting admin service...(this may take few minutes to complete)" -foregroundcolor Yellow
                 Stop-Service -Name $spAdminServiceName
                 Start-SPAdminJob -Verbose
                 Start-Service -Name $spAdminServiceName

                 #Block until we're sure the solution is deployed.
                 do { Start-Sleep 2 } while (!((Get-SPSolution $packageName).Deployed))
        
                 Write-Host
                 Write-Host " $packageName Deployed Sucessfully... " -foregroundcolor Green
          }
         }
         else
         {
              #Add the solution
              #Write-Host
              #Write-Host "Adding solution... $packageName..." -foregroundcolor Yellow
              #$solution = Add-SPSolution $packagePath
        Write-Host "Solution Not Found ... $packageName..." -foregroundcolor Red
         }

        
}

#
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                                                                    Deploy the package
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#


LoadSharePointPowerShellEnvironment

Write-host "Deploying Solution..." -foregroundcolor Yellow

Write-Host
write-host "Enter the Web Application Url where you need to deploy the solution..."
[string]$Url = Read-Host
[string]$WebAppUrl =  $Url.Trim()

$objSite = Get-SPSite $WebAppUrl -ErrorAction SilentlyContinue
if($objSite -eq $null)
{
       Write-Host
       Write-Host "Web Application Url not correct or Web application doesnot extsts. Re-run the exe with correct url." -foregroundcolor Red
       Read-Host
}
else
{
     $dir = $(gl)
     $list = Get-ChildItem $dir | where {$_.extension -eq ".wsp"}
     $currentDir = Get-Location
     foreach($my_file in Get-ChildItem $list)
     {
        write-host $my_file.Name
           [string]$packageName = $my_file.Name
        [string]$packagePath = $currentDir.Path + "\" + $packageName
        DeploySolution $packageName $packagePath $WebAppUrl
     }
}





PowerShell script for adding multiple WSP solutions

In case of big SharePoint Farms with large application domain we need to add multiple solutions, Doing it manually will be a lot tough and time consuming task. So a more manageable way could be to create a PowerShell script, that runs on the Farm to add the listed wsp solutions. I created one of such solution for a client who had around 50-60 such solutions. Below is the script that you can use to add wsp's solutions to SharePoint Farm. This script should be placed inside a folder along with the wsp solutions which needs to be added. 

Copy-Paste the below script in a text file and rename its extension to - *.ps1



$dir = $(gl)
 $list = Get-ChildItem $dir | where {$_.extension -eq ".wsp"}
 foreach($my_file in Get-ChildItem $list)
 {
       write-host $my_file.Name
       $currentDir = Get-Location
       [string]$packageName = $my_file.Name
        $solution = Get-SPSolution $packageName -ErrorAction SilentlyContinue
         if ($solution -ne $null)
         {
            Write-Host
               Write-Host "The solution $packageName already exists." -foregroundcolor Red
         }
         else
         {
              [string]$packagePath = $currentDir.Path + "\" + $packageName
              Write-Host
              Write-Host "Adding solution... $packageName..." -foregroundcolor Yellow
              $addedsolution = Add-SPSolution $packagePath
        if ($addedsolution -ne $null)
        {
            Write-Host "Solution added successfully... $packageName..." -foregroundcolor Green
        }
        else
        {
            Write-Host "ERROR in adding solution... $packageName..." -foregroundcolor Red
        }
         }
 }


PowerShell script for removing multiple WSP solutions

In case of big SharePoint Farms with multiple SharePoint WSP solutions deployed, sometimes it is very difficult to remove some specific solutions automatically as each solution retraction takes some time and we may also need to execute SharePoint 2010 Administration service (SPAdminv4) Job after removing individual solution from the Farm. A more manageable way could be to create a PowerShell script, that runs on the Farm to remove the listed wsp solutions. I created one of such solution for a client who had around 50-60 such solutions. Below is the script that you can use to completely uninstall the wsp solutions from SharePoint Farm. This script should be placed inside a folder along with the wsp solutions which needs to be removed. 


Copy-Paste the below script in a text file and rename its extension to - *.ps1

function LoadSharePointPowerShellEnvironment
{
       write-host
       write-host "Setting up PowerShell environment for SharePoint..." -foregroundcolor Yellow
       write-host
       Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
       write-host "SharePoint PowerShell Snapin loaded." -foregroundcolor Green
}

function RetractSolution($packageName)
{
  $spAdminServiceName = "SPAdminV4"
  $solution = Get-SPSolution $packageName -ErrorAction SilentlyContinue
         if ($solution -ne $null)
         {
                     #Retract the solution
                     if ($solution.Deployed)
                     {
                       Write-Host
                       Write-Host "Retracting solution $packageName..." -foregroundcolor Yellow
                             if ($solution.ContainsWebApplicationResource)
                             {
                                  $solution | Uninstall-SPSolution -AllWebApplications -Confirm:$false
                             }
                             else
                             {
                                  $solution | Uninstall-SPSolution -Confirm:$false
                             }
                       Write-Host
                       Write-Host "starting admin service...(this may take few minutes to complete)" -foregroundcolor Yellow
                       Stop-Service -Name $spAdminServiceName
                       Start-SPAdminJob -Verbose
                       Start-Service -Name $spAdminServiceName

                       #Block until we're sure the solution is no longer deployed.
                       do { Start-Sleep 2 } while ((Get-SPSolution $packageName).Deployed)
                     }
              #Delete the solution
              Write-Host
              Write-Host "Removing solution $packageName..."
              Get-SPSolution $packageName | Remove-SPSolution -Confirm:$false
              Write-Host "Solution Removed Sucessfully." -foregroundcolor Green
         }
         else
         {
               Write-Host
              Write-Host "No solution found to retract with name $packageName" -foregroundcolor Yellow
         }
}





#
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                                                                    Retract the package
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#


LoadSharePointPowerShellEnvironment

Write-host "Retracting Solution..." -foregroundcolor Yellow


 $dir = $(gl)
 $list = Get-ChildItem $dir | where {$_.extension -eq ".wsp"}
 foreach($my_file in Get-ChildItem $list)
 {
       write-host $my_file.Name
       [string]$packageName = $my_file.Name
       RetractSolution $packageName

 }



Wednesday, March 11, 2015

There are no page layout files available in the master page library of the top-level site. Add one or more page layouts to this library before you create a page or set page properties


I faced a very strange error in one of our integration environment where it started behaving weird on clicking of  "page layout and site templates" /_layouts/15/AreaTemplateSettings.aspx

Server Error in '/' Application.


There are no page layout files available in the master page library of the top-level site. Add one or more page layouts to this library before you create a page or set page properties.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Microsoft.SharePoint.SPException: There are no page layout files available in the master page library of the top-level site. Add one or more page layouts to this library before you create a page or set page properties.


In order to solve this issue just reactivate the site publishing feature again to reset the settings.



Wednesday, March 4, 2015

Repeat Header on every page in SSRS Report

Yesterday i got one new task in SSRS that to repeat header on every page and that will have two columns (company logo + address) but the address not to be repeat on every page.

I googled a lot and at most of the sites found that option "Repeat header rows on each page need to be repeat" need to be check or set the tablix property "RepeatOnNewPage" to True.








But all those solutions not worked for me because that were in the context of table.

After that i tried below simple steps:

1. Add the page header.


2. Insert the image and text box in report header.







3. After creating text box and writing text. Click on preview and save the report as pdf. 



4. I noticed the report header repeating itself on every page which supposed to be because header and footer will repeat themselves on each page.



5. So now need to hide the address text box from the other pages for which go to the text box properties --> visibility --> select show or hide based on an expression --> click "fx" --> enter "=IIF(cstr(Globals!PageNumber)="1",False,True)" --> click OK.






Basically expression "=IIF(cstr(Globals!PageNumber)="1",False,True)" show the text box only where page number is "1".
You can read more about expression at https://msdn.microsoft.com/en-us/library/ms157328.aspx




6. Preview the report again and save it as pdf and you will notice logo is visible on all pages but address is on only first page.