About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Wednesday, 11 December 2013

PowerShell script to deploy Farm solution - single click - SharePoint 2010, 2013

Here is the Power Shell script to deploy any kind of SharePoint farm solution.

Note: 
1.  Make sure that, your solution should be in the same directory from where you are running the script.

2. Replace your own solution name @ line # 5

3. Pass necessary parameters to Install-SPSolution command.

4. Save file with .ps1 as extension. Example: psDeploy.ps1

5. Create .bat file and call the .ps1 file in it. Example : Deploy.bat

cd /d %~dp0
powershell -noexit -file    ".\
psDeploy.ps1" "%CD%"
pause


6. Once done, directly run the .bat file. No need of launching PowerShell console to execute.

-----------------------------------------------------------------------------------------------------------
Add-PsSnapin Microsoft.SharePoint.PowerShell

Write-Host 'Poweshell Script will initialize parameters'

$CurrentDir=$args[0]

# Replace respective solution

$solutionName="sp.web.webparts.wsp"

$SolutionPath=$CurrentDir + "\"+$solutionName

$logfile=$CurrentDir + "\log.log"

Start-Transcript $logfile

$errorActionPreference = 'Inquire'

# As we are performing deploy and redeploy solutions in single click we will get a known exception which is not requires to handle
# so that we can continue the next statement.

$solution = Get-SPSolution $solutionName  -ErrorAction SilentlyContinue
 
try
{
   
if ($solution -ne $null)
{
if($solution.Deployed)
{

try
{

# Gets the feature attrbutes to log the feature Id information
# $feature= Get-SPFeature -Identity $featureName

# Write-Host 'Powershell Script will now disable feature:' $feature.Id
# Disable-SPFeature -Identity $featureName -Confirm:$false -force

}

# Display and log the feature deactivation failure message with feature Id
catch [SPException]
{
Write-Error 'An error occured while deactivating the feature' $featureId.Id
}

Write-Host 'Poweshell Script will now retract solution:' $solutionName

Uninstall-SPSolution -Identity $solutionName -AllWebApplications -Confirm:$false      

Write-Host 'Poweshell Script is waiting for administrative timer jobs to complete...'

while ($Solution.JobExists)
{  
  Start-Sleep 2   
} 
}

Write-Host 'Poweshell Script will now remove solution:' $solutionName

Remove-SPSolution -Identity $solutionName -Confirm:$false

}

Write-Host 'Poweshell Script will now add solution:' $solutionName

Add-SPSolution $SolutionPath

Write-Host 'Poweshell Script will now deploy solution:' $solutionName

Install-SPSolution -Identity $solutionName -GACDeployment -AllWebApplications

Write-Host 'Poweshell Script is waiting for administrative timer jobs to complete...'

while ($Solution.JobExists)
{  
  Start-Sleep 2   
}

Write-Host 'Poweshell Script has finished deployment of solution:' $solutionName
}

# We can get an unknow error while deploying/redeploying the solution
# so need to dispaly and log the exception details
catch [system.Exception]
{
Write-Error 'An Unknown error occured while trying to deploy/redeploying the solution:' $_.Exception.Message
}

Stop-Transcript

Remove-PsSnapin Microsoft.SharePoint.PowerShell

------------------------------------------------------------------------------------------------------------

Sunday, 6 October 2013

How to delete SharePoint event receivers on feature deactivating method.


public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

        {

            SPWeb web = properties.Feature.Parent as SPWeb;
            try
            {

             SPList list = web.Lists.TryGetList("List name"); 

               if (list != null)
                 {
                  SPEventReceiverDefinition receiverDefinition = null; 

                  for (int i = list.EventReceivers.Count - 1; i >= 0; i--)
                            {
                                receiverDefinition = list.EventReceivers[i];
 
                                receiverDefinition.Delete();
                            }
                        }
            }

            catch (Exception ex)
            {

                PortalLog.LogString("Exception - {0}  -- {1} -- {2}", "XXXXXX", ex.Message, ex.StackTrace);

            }
        }