Tuesday, December 14, 2010

Deploying SharePoint 2010 Sites using PowerShell

This post will cover the following details..

1. Deploying a complete site to another server (using Backup / Restore).

2. Deploying a complete site to another server (using Site Template as a WSP) .

3. Deploying increments done to another server (using Export / Import).

4. Deploying WSP Solutions to another server.

5. Upgrading the changes done to a WSP Solution into another server.


A. Deploying a complete site onto another server:


a. Consider a SharePoint site given below as the Source site where the development has taken place.

Figure 1: Source SharePoint site to be deployed to another web-app / server

b. As you can see, there are some minor changes that I have done to the default SharePoint site template. Also note the URL of the source site.

c. To deploy this site onto some other site, we first need to backup this site.

d. We can do so, either by using the STSADM commands or by using the PowerShell commands. As STSADM commands are scheduled to be deprecated, we will explore the PowerShell commands.

e. In order to combine all the PowerShell commands that would be used while deployment, they can be written in a single .ps1 file. Follow the steps below to create one.

f. Create a new file in Notepad and save it as a .ps1 file say ‘BackUpScript.ps1’.

g. Right click on this file and click on ‘Edit’ as shown below:

Figure 2: Edit the PS1 file


h. Paste the following commands in it

$file = "D:\Rahul\Backup.log"
start-transcript $file
trap { stop-transcript; break}
Backup-SPSite -Identity "http://SERVER_NAME:PORT_NO/sites/Example/" -Path "D:/Rahul/SiteBackup.cmp" -Force -NoSiteLock
stop-transcript

Here is what the script does….

$file is the variable where we define the path of the log file to be created.

The output of the commands present between the start-transcript and the stop-transcript keywords will be shown on the screen as well as logged in the log file.

The trap keyword is like an exception handling clause which continues to log the output in the log file even if an error is encountered.

The Identity value should be assigned the URL of the source site.

The Path value should be assigned the path where the log file would be stored

The PowerShell command in normal case locks the site out so that no changes are allowed to the site while the backup takes place. The –NoSiteLock parameter does not lock the site.

The file should look as shown below.

Figure 3: BackupScript.ps1


i. Open the ‘SharePoint 2010 Management Shell’ from the Windows menu.
ii. Go to the path where the .ps1 file exists and execute the script using the command given below.
./BackUpScript.ps1  (Refer snapshot below)

Figure 4: Running the BackupScript.ps1

 Note that if you are running the command from a normal command prompt, you need to first load the PowerShell snapin. The SharePoint Management Shell does it for us.

iii. After the script gets executed, you will find two files created in the same folder.
The log file and the backup file in ’.cmp’ format will be created as shown below.


Figure 5: The log file and the backup file created
iv. Now it’s time to deploy the site onto another server. 

v. Create a DeployScript.ps1 file on the same lines and paste the foll. code in it
$file = "D:\Rahul\Deployment.log"
start-transcript $file
trap { stop-transcript; break}
Remove-SPSite -Identity "http:// SERVER_NAME:PORT_NO /sites/ExampleClone"
Restore-SPSite -Identity "http:// SERVER_NAME:PORT_NO /sites/ExampleClone" -Path "D:/Rahul/ SiteBackup.cmp " -Force
stop-transcript

vi. Note that the SERVER_NAME:PORT_NO here should be the one of the destination site where we want the site to be deployed. The Remove-SPSite command is just an additional check just in case a previous version of the site already existed on the same URL

vii. Execute the script from ‘SharePoint 2010 Management Shell’ in the same way as we did for the BackUpScript. While the script gets executed, it will ask for confirmation both while removing the site (i.e. in case it exists on the destination site) and while restoring the backup. Enter A on both instances as shown below.


Figure 6: Confirmation asked before Removing / Restoring a site collection

viii. The destination site should now be ready as shown below.
For the sake of demonstration, I did it on the same server but on a different web application with a different port number. The same process can also be used to deploy it on a different server. 

Figure 7: Site collection restored on the Destination web-app
B. Deploying the complete site onto another server (using Site Template as a WSP)


Another way to carry forward a complete site to another location is to create a Site template (which gets stored as a WSP in 2010) and then activate it on the destination server / web-app

a. Browse to the source site collection.
b. Click on ‘Site Actions -> Site Settings’ as shown below.

Figure 8: Site Actions -> Site Settings

c. Click on the ‘Save Site As Template’ link under the ‘Site Actions’ sub-menu as shown below.

Figure 9: 'Save Site As Template' link
d. Enter a template Name, a filename and check ‘Include Content’ if required as shown below. Click Ok.  

Figure 10: 'Save as Template' Settings


e. You should get a Successfully Created message along with a link to the Solution Gallery

Figure 11: Success message on site template creation
 f. Go to the Solution Gallery and click on the newly created template. Save it to a location on your machine.

g.  Now, Go to Create Site Collection page from the Central Admin, and while selecting the template… Select ‘Select template later..’ option under the ‘Custom’ tab as shown below

Figure 12: Template Selection option while creating a site


h. Click OK.
i.   Browse to the newly created site and go to its Solution gallery
j.  Click on ‘Upload Solutions’ link under the Solutions tab

Figure 13: Upload Solution in the Solution Gallery
k. Browse the template saved before and click Ok
l.  Click on ‘Activate Solution’ link from the pop-up window that appears


Figure 14: Activate the solution after it is uploaded
m. The Solution gallery should now show the solution as activated
n. Click on the Site Link at the top left and you should now be asked to select the template


Figure 15: Site Link




 o. Select the Example Template that we uploaded as shown below. Click Ok

Figure 16: Select the custom site template




p.  You will be asked to setup access Groups.  Create new Groups or use existing groups and click OK

Figure 17: Set Up Groups for the restored site




 q. The destination site would now be ready as shown below 

Figure 18: Restored Site (after the uploaded site template is applied)

r.   Given that you have a Site Template as a Sandboxed WSP ready, the process of creating a site from this custom site template can also be done using the following PowerShell script

$file = "D:\Rahul\RestoreWSPSiteTemplate.log"

start-transcript $file
trap { stop-transcript; break}
Remove-SPSite -Identity "http://DEST_SERVER_NAME:PORT_NO/sites/ExampleRestore"
New-SPSite "http:// DEST_SERVER_NAME:PORT_NO/sites/ExampleRestore" -OwnerAlias domain\rahul.vartak 

Add-SPUserSolution -LiteralPath "D:\Rahul\ExampleTemplate.wsp" -Site "http:// DEST_SERVER_NAME:PORT_NO/sites/ExampleRestore"

Install-SPUserSolution –Identity ExampleTemplate.wsp -Site "http:// DEST_SERVER_NAME:PORT_NO/sites/ExampleRestore"

$x = Get-SPSite "http:// DEST_SERVER_NAME:PORT_NO/sites/ExampleRestore"
$y = $x.GetWebTemplates(1033) | Where{ $_.Title -eq "ExampleTemplate" }

[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")
$site= new-Object Microsoft.SharePoint.SPSite("http:// DEST_SERVER_NAME:PORT_NO/sites/ExampleRestore")
$site.OpenWeb().ApplyWebTemplate($y)
$site.Dispose()
stop-transcript



C. Deploying increments done onto another server (using Export / Import)  


If your site is already deployed on the destination server, and you happen to create some lists / libraries or even a subsite; you can deploy the new lists, libraries or subsites with the help of Export / Import commands provided by PowerShell.

The same can also be used if some data has been added to the lists existing before and needs to be deployed. Usually applies to master or configuration lists.

Consider a SharePoint list ‘Knowledge Links’ in the source site that was created on the source site as shown below…


Figure 19: 'Knowledge Links' list to be carried over to another server / web-app
Now, if we have to push just this list onto another server which already has the same site deployed earlier without this list, we can do the following..

ix. Use the Export-SPWeb command as shown below. 

Export-SPWeb -Identity "http://SOURCE_SERVER_NAME:PORT_NO/sites/Example" -Path "D:/Rahul/ListBackup.cmd" -ItemUrl "Lists/Knowledge Links" -IncludeUserSecurity -Force  

The ItemUrl parameter should be given the relative URL of the list that we are trying to export. 

The IncludeUserSecurity parameter ensures that permissions that we have set on the list are also carried over.

The Force parameter overwrites the backup file if another one already exists at the same folder location.

x. Run the script and you should  find the exported list in specified folder in cmd format.

xi. We can now use the following Import-SPWeb command to import the list onto our destination server.

Import-SPWeb "http:// DEST_SERVER_NAME:PORT_NO /sites/ExampleClone " –Path "D:/Rahul/ListBackup.cmd" –IncludeUserSecurity

Notice that we don’t have to specify what item we are importing here (i.e. ItemUrl) as that information is already contained in the cmp file. The Import command is thus generic and can be used for a subsite or a library as well.

On running the import command, the list is generated as-is in the Destination web application as shown below. 


Figure 20: 'Knowledge Links' list imported as-is onto another web-app / server
The  Import-SPWeb command has several other switch parameters which may prove useful during deployment. Some of them are as follows.
o   ActivateSolutions  : Adding this parameter will result in all user solutions being activated during import
o   IncludeUserCustomAction : This parameter ensures that all the custom user actions are also included in the import

More information can be found by running the following command in PowerShell.
Get-Help Import-SPWeb -full

The Export / Import Commands will also come to use in scenarios where the development requires the use of SharePoint Designer. In such cases, the customizations done through SPD can be carried over to different servers using Export / Import.

Note 
: While restoring sites, lists, and libraries using the import-SPWeb cmdlet, the previous data will get overwritten unless the -UpdateVersions parameter is passed in the operation, which requires versioning to be enabled on the destination list or library. Using -UpdateVersions enables you to restore the information contained in the backup without overwriting new items created in the destination list or library

D: Deploying WSP Solutions to another server.

Deploying WSP Solutions has been made easier by PowerShell Commands. Find below the list of PowerShell commands which will be used extensively while deployment.

xii. Adding and Installing WSP Solutions 

Add-SPSolution “D:\Rahul\SharePointSolution.wsp“
Install-SPSolution –Identity SharePointSolution.wsp –WebApplication http://SERVER_NAME:PORT_NO  –GACDeployment

If you wanna add the solution as sandboxed, you can use the Add-SPUserSolution and Install-SPUserSolution commands instead. The GACDeployment parameter will not be used in that case. 

xiii. To retract and remove a solution, we can use the following commands:

Uninstall-SPSolution –Identity SharePointSolution.wsp –WebApplication http:// SERVER_NAME:PORT_NO
Remove-SPSolution–Identity SharePointSolution.wsp

E: Upgrading changes done to the WSP Solutions.

To upgrade a solution, we can specify which solution is to be updated and with which new solution file:
Update-SPSolution –Identity SharePointSolution.wsp –LiteralPath “D:\Rahul\SharePointSolution.wsp” –GacDeployment




free counters

5 comments:

  1. Thanks for sharing the useful info. Keep posting.

    Vinay
    http://vinaybhatia.blogspot.com/

    ReplyDelete
  2. Nilesh MandekarDec 22, 2010 09:07 AM

    Nice post.
    Please update the screenshot of point(g). It is currently showing uploaded template.

    ReplyDelete
  3. Thanks Vinay and Nilesh..!

    @Nilesh.. Corrected the screenshot of Note (g).

    ReplyDelete
  4. Hello
    I ran the command

    Install-SPSolution –Identity SharePointSolution.wsp –WebApplication http://SERVER_NAME:PORT_NO –GACDeployment

    but there was no output to indicate me whether the task was completed or not. I browse the site and i do not find my web application anywhere. Is there an additional process i have to do? thanks

    ReplyDelete
  5. For deploying the WSP to another server, i think this assumes that the script is executed on the same same server which has a Sharepoint installation. However how do you tackle this when you want to deploy to a SP Server remotely e.g. from a CI Server?

    Assume that I auto-build my solution on Server 1 and would like to deploy this solution to multiple environments (test, pre-prod, prod). Is it possible to give the URL parameter to the AddSPSolution cmdlet (and other subsequent cmdlets) so that I can deploy this to multiple environments without necessarily copying all the WSP's and the script to deploy to each individual environment?

    Many thanks for any help here.

    ReplyDelete