Retract (uninstall and remove) SharePoint solution


Scenario:
You want to retract a solution already deployed to SharePoint. Normally you would use Visual Studio (r-click retract) for this but you don't have Visual Studio installed on the SharePoint server.

PowerShell Script:

$webUrl = "http://site:port"
$wspFileName = "myOldSolution.wsp"

function WaitForJobToFinish([string]$solutionName)
{ 
    $JobName = "*$solutionName*"
    $job = Get-SPTimerJob | ?{ $_.Name -like $JobName }
    if ($job -eq $null) {
        Write-Host 'Timer job not found'
    }
    else {
        Write-Host -NoNewLine "Waiting to finish job $JobFullName"

  $JobFullName = $job.Name
        while ((Get-SPTimerJob $JobFullName) -ne $null) {
            Write-Host -NoNewLine .
            Start-Sleep -Seconds 2
        }
        Write-Host  "done"
    }
}

# Add SharePoint Snapin
Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$webApp = Get-SPWebApplication $webUrl

$solution=Get-SPSolution -Identity $wspFileName -ErrorAction:SilentlyContinue
# Check whether the solution exists in the Solutions Store.
if ($solution -ne $null) {
 # Check whether the solution is already deployed.
 if ($solution.Deployed) {
  write-Host "Retracting" $solution.Name -ForegroundColor Yellow
  if ($solution.ContainsWebApplicationResource) {
   Uninstall-SPSolution -identity $solution.Name -allwebapplication -confirm:$false
  }
  else {
   Uninstall-SPSolution -identity $solution.Name -confirm:$false
  }

  WaitForJobToFinish $solution.Name
 }

 # Remove solution from store.
 Write-Host "Removing" $solution.Name -ForegroundColor Yellow          
 Remove-SPSolution –Identity $solution.Name -confirm:$false
}

Find all pages using a specific Web Part

Scenario:
You are looking for all pages where a specific Web Part is used

PowerShell Script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.WebPartPages")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")

$wpName = "Register css web part";
$siteUrl = "http://site:port"
$libraryName = @("Pages");
$site = Get-SPSite $siteUrl;

foreach($spweb in $site.AllWebs)
{
 $url = $spweb.Url;
 foreach($library in $libraryName) {
  $list = $spweb.Lists[$library]
  foreach($listItem in $list.Items) {
   $WebPageUrl = $listItem.Url
   if ($WebPageUrl) {
    if ($spwebpart.Title -and $listItem.File.CheckOutType -ne "None") {
     Write-Host "Skipping checked out page $url/$WebPageUrl" -ForegroundColor Cyan
    }
    $spWpManager = $spweb.GetLimitedWebPartManager($WebPageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
    foreach($spWebPart in $spWpManager.Webparts) {
     if($wpName -eq $spWebPart.Title) {
      Write-Host "Found '$wpName' on $url/$WebPageUrl" -ForegroundColor Yellow
     }
    }
   }
  }
 }
 $spweb.Dispose();

 if($spWpManager -ne $null) {  
  $spWpManager.Dispose();
 }  
}

FIX: The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered

Scenario:
You try to run a PowerShell script and the script fails with this message:
The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered

Cause:
When you run this cmdlet to add a user to the SharePoint_Shell_Access role, you must have membership in the securityadmin fixed server role on the SQL Server instance, membership in the db_owner fixed database role on all affected databases, and local administrative permission on the local computer.

Fix:
Run this script in PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue; 
Add-SPShellAdmin -UserName Domain\UserName;

More on Add-SPShellAdmin:
http://technet.microsoft.com/en-us/library/ff607596.aspx

Activate or deactivate timer job feature using PowerShell

Scenario:

  • You want to activate your newly deployed feature to install a timer job
  • You want to deactivate the feature and remove the job
  • You want to deploy an update to the existing timer job feature
Message:
The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Note:
I was able to Deactivate the feature from the SharePoint UI, as a farm admin, but the Activate still failed for me.

Solution:
Use the following PowerShell commands:

 Disable-SPFeature -Identity "TimerJobName" -url http://site:port  
or
 Enable-SPFeature -Identity "TimerJobName" -url http://site:port  

To determine your feature's name you can use (which can be tricky sometimes) use this command:
 Get-SPFeature | where-object {$_.DisplayName -match "MyPattern"} | Select DisplayName  

Delete broken web part from page

Scenario: 
You have a broken web part on your page and cannot remove it in edit mode

Solution: 
Open your page in maintenance mode by appeding "?contents=1" to the url.

Example:

http://site:port/web/page.aspx
will become
http://site:port/web/page.aspx?contents=1










Here select your webpart and click the "Delete" button.

Format code in your blog post

Scenario:
In your post on blogger.com you want format the code to stand out from the normal text.

Solution:
You can use this: http://codeformatter.blogspot.ca/

Delete a web part from all pages in a site collection

Scenario:
One of your custom web parts becomes obsolete and you decide to remove from all pages where is is used.

Powershell Script:

 Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue  
 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.WebPartPages")  
 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")  
 $wpName = "Register CSS web part";  
 $siteUrl = "http://site:port"  
 $libraryNames = @("Pages");  
 $site = Get-SPSite $siteUrl;  
 $checkedOut = @()  
 foreach($spweb in $site.AllWebs)  
 {  
      $url = $spweb.Url;  
      Write-Host "Processing $url"  
      foreach($lib in $libraryNames)  
      {  
           $list=$spweb.Lists[$lib]  
           foreach($item in $list.Items)  
           {  
                $WebPageUrl = $item.Url  
                if ($WebPageUrl.length > 0)  
                {  
                     Write-Host "Processing page $WebPageUrl"  
                     $spWpManager = $spweb.GetLimitedWebPartManager($WebPageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);  
                     $webparts = @()  
                     if ($item.File.CheckOutType -ne "None")  
                     {  
                          $checkedOut = $checkedOut + $WebPageUrl + ","  
                     }  
                     foreach($spwebpart in $spWpManager.Webparts)  
                     {  
                          if($spwebpart.Title -eq $wpName)  
                          {  
                               $webparts = $webparts + $spwebpart.ID    
                          }  
                     }  
                     foreach($webpartId in $webparts)   
                     {  
                          Write-Host "Deleting " + $spwebpart.Title + " from " + $item.Name  
                          $spWpManager.DeleteWebPart($spWpManager.Webparts[$webpartId])  
                     }  
                }  
           }  
      }  
      if ($checkedOut -ne @()) {  
           Write-Host "The following pages were checked out and could not be processed: $checkedOut" -foreground yellow   
           }  
      $spweb.Update();  
      $spweb.Dispose();  
      $checkedOut = @()  
      if($spWpManager -ne $null)  
      {    
           $spWpManager.Dispose();  
      }    
 }