Saturday, October 20, 2012

How to Deploy wsp in SharePoint 2010 using PowerShell

Overview
In SharePoint 2010, STSADM command is supported but PowerShell commands are recommended to use. PowerShell command is powerful and extensible. It is not case sensitive but i suggest to use Caml Case format for good practice. It is used to deploy solutions/features in SharePoint 2010 environment.

Format of the PowerShell command
Basically Powershell coammands are built with Verb, Hyphen -) and Noun syntax. So Powershell command has two parts which are:
  1. Verb
  2. Noun
Example: Execute below command in Powershell window to add the wsp solution file
              Add-SPSolution "D:\\Filename.wsp"
Here, Add is a Verb and SPSolution is a Noun with Hyphen(-). i hope that you can understand it.

Solution Deployment
There are eight steps involved in SharePoint 2010 deployment those are:
  1. Deactivate Features
  2. Uninstall Features
  3. Retract Solution
  4. Delete Solution
  5. Add Solution
  6. Deploy Solution
  7. Install Features
  8. Activate Features
Example:  Change solution name , SolutionPath and HostName.

$solutionName="TestWP.wsp"
$SolutionPath="D:\\WebParts\TestWP.wsp"
Write-Host 'Going to disable feature'
disable-spfeature -identity TestWPFeature -confirm:$false -url http://HostName:1111
Write-Host 'Going to uninstall feature'
uninstall-spfeature -identity TestWPFeature -confirm:$false -force
Write-Host 'Going to uninstall solution'
Uninstall-SPSolution -identity $solutionName  -allwebapplications -confirm:$false

Write-Host 'Going to remove solution'
Remove-SPSolution –entity $solutionName -confirm:$false
Write-Host 'Going to add solution'
Add-SPSolution $SolutionPath
Write-Host 'Going to install solution to all web applications'
Install-SPSolution –entity $solutionName –Allwebapplications –GACDeployment

Write-Host 'Going to enable Feature' 
Enable-spfeature -identity TestWPFeature -confirm:$false -url http://HostName:1111 

Save above script as a filename "deploySolutionScript.ps1" and save below script as bat file named "RunDeployment.bat" then double click on bat file which will deploy the solution.
cd /d %~dp0
powershell -noexit -file    "\DeploySolutionScript.ps1" "%CD%"

Note: Make sure that all the files should be placed inside a folder.

Conclusion
Powershell script is easy way to deploy your custom solutions/Features into SharePoint platform. Powershell also supports SharePoint Programming features. So you can do all kind of SharePoint programs. Powershell script's Basic knowledge must for SharePoint developers.

Thursday, January 26, 2012

Cancel the LongRunningOperationJob in SharePoint 2007 when click on Cancel button

About  LongRunningOperationJob: 

              In SharePoint, Microsoft.SharePoint.Publishing.Internal provides "LongRunningOperationJob" class which prevents the timeout issue and used when long running operations(example: insert/update data in database at button submit event if its taking long time). It needs to activate the publishing feature : in SharePoint site , go to SiteActions->SiteSettings-> under "Site Collection Administration" click on "Site collection features"->Office SharePoint Server Enterprise Site Collection features.
 
Issue Faced:

              In SharePoint 2007, i am uploading excel file with large data and reading data from excel file then inserting data into DB. in this case i am using "LongRunningOperationJob" class in aspx page, successfuly its uploaded. but user wants to cancel this upload operation while uploading. i used "UserCanCancel = true;" Cancel button is enabled, but it is not cancelled the uploading process when user hit the Cancel button..how to cancel it when hit the cancel button?.

Anwser :
                i found the solution after did some analyzation,  "LongRunningOperationjob" class contains a property named "UserRequestedCancel" which can stop the uploading process using below lines.

Code:

            for (int i = 0; i <= worksheet.Cells.LastRowIndex; i++)
              {
                    if (!this.UserRequestedCancel)
                    {
                        this.StatusDescription = "Record " + (i + 1).ToString() + " is under proccess...";
                        this.UpdateStatus();
                        this.OperationsPerformed++;
                    }
                   else
                       break;
              }

by default UserRequestedCancel will be false. once clicked on Cancel button UserRequestedCancel will be true.