Wednesday, November 28, 2012

SAML webapp and rtfa cookie Log out issue SharePoint 2010

Scenario: Today I came across an issue on SAML enable multi-auth webapp on SharePoint 2010. The issue is that when I login as SAML user and then try to Log-out or Sign as different user. It doesn't log me out, meaning it doesn't redirect to appropriate STS login page.

Solution: The fix for this is two fold . One has to be done on ADFS by running below script

Set-ADFSRelyingPartyTrust -TargetName "SPS 2010 ADFS" -TokenLifetime 5

and the other script must be run on SharePoint Farm as below:-

$sts = Get-SPSecurityTokenServiceConfig

$sts.LogonTokenCacheExpirationWindow = (New-TimeSpan –minutes 1)
$sts.Update()
Iisreset
 
 
Detailed information can be found in Steve Peschka's blog:-
 
Note: "TokenLifeTime" value must be greater than "LogonTokenCacheExpirationWindow".

 

Wednesday, November 21, 2012

PowerShell Script to uninstall SharePoint solution pkg

Scenario: More often than not when I deliver solution pkg to test team or production, I create a quick PowerShell script that does the installation\uninstallation automatically. Having it here so that it can help me and others to quickly garb it.

Solution: Uninstall script:-

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

# assume the SolutionName.wsp is in the same directory as this script.

$ScriptDir = split-path -parent $MyInvocation.MyCommand.Path

$releaseDir = $ScriptDir + "\"

 

$SolutionID = "00000000-0000-00000-0000-000000000"

$Solution = get-spsolution | Where { ($SolutionID -eq $_.SolutionId) }

 

If($Solution -eq $null)

{ 

    Write-Host "Solution is not installed."

}

else

{

  #Check if solution is deployed in the farm

    If ($Solution.Deployed -match "True")

    {

        #Retract the  solution

        Write-Host "Retract Solution..."

        Uninstall-SPSolution -Identity $Solution

       

        # tell user to wait a few seconds for the retract to complete

        Read-Host "Wait 30 seconds for the solution to retract, then press Ok."

       

        # Remove the solution

        Write-Host "Remove Solution..."

        Remove-SPSolution -Identity $Solution

             

    }

    ElseIf($Solution.Deployed -match "False")

    {

        #retract the solution

        Write-Host "Removing Solution..."

        Remove-SPSolution -Identity $Solution       

    }
}
Install script:-

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"

# assume the Solution.wsp is in the same directory as this script.

$ScriptDir = split-path -parent $MyInvocation.MyCommand.Path

$releaseDir = $ScriptDir + "\"

$SolutionId = "000000000-0000-0000-0000-00000000"

$Solution = Get-SPSolution | Where { ($SolutionId -eq $_.SolutionId) }

$wspFileName = "Solution.wsp"

$solutionFilePath   = $releaseDir + $wspFileName    

If($Solution -eq $null)

{

   # Add solution to the farm

    Write-Host "Adding solution to the farm"

    Add-SPSolution  $solutionFilePath

   

     #Deploy solution to the farm

    Write-Host "Deploying solution from " $solutionFilePath

    Install-SPSolution -Identity $wspFileName -GACDeployment 

}

PowerShell check if SharePoint feature is activated at farm scope

Scenario: Most of the times when you ship solution package you deliver PowerShell installation scripts, part of which would be activating the features. But before activating you may want to check if its already activated at a given scope. And if activated already skip it. How to preform this check?

Solution:
$featureGuid = "11075dtfg-cb48-48a0-8b21-001ac2d"

$feature=(Get-SPFeature -Identity $featureGuid -ErrorAction SilentlyContinue -Farm) -ne $null

if($feature -ne $true)
{
  #feature is not activated at the given scope so activate it
}

In the above you can replace "-Farm" with any of the other valid scopes.


Other way to do this and only works for Webapp,Site, and web scoped features.
$webApp = read-host "Enter the URL of the SharePoint Web app"    
$webApp2 = Get-SPWebApplication $webApp
$UpdateFeature = $webApp2.Features[$UpdateFeatureGuid]

if($UpdateFeature -eq $null)
{
#Feature is currently deactivated,Activate Feature #Update

Wednesday, November 7, 2012

How to delete webparts from webpart gallery using powershell

Scenario: Deleting multiple WebParts from WebPart gallery using PowerShell

Solution:
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-null

$Url = 'http://portal.sharepoint.com'

$site = Get-SPSite $Url

$wpCatlog =[Microsoft.SharePoint.SPListTemplateType]::WebPartCatalog

$list = $site.GetCatalog($wpCatlog)

$wpID = New-Object System.Collections.ObjectModel.Collection[System.Int32]

foreach ($item in $list.Items)
{
  if($item.DisplayName.ToLower().Equals("webpartname1"))
  {   $wpID.Add($item.ID) }
}

foreach($wp in $wpID)

   $wpItem = $list.GetItemById($wp)
   $wpItem.Delete()
}
$list.Update()    

   

Monday, November 5, 2012

SharePoint2010: Programatically Delete Webpart From Gallery

Scenario: Programmatically deleting a webpart from webpart gallery

Solution: Add a feature receiver to the webpart feature and paste below code in featuredeactivating

SPSite site = properties.Feature.Parent as SPSite;

List<int> wpToDelete = new List<int>();

SPList wplist = site.GetCatalog(SPListTemplateType.WebPartCatalog);

foreach (SPListItem item in wplist.Items)
{

  
    if (item.DisplayName.ToLower().Contains("webpartname"))
    {
      wpToDelete.Add(item.ID);
    }

}

foreach(int wpID in wpToDelete)
{
 
  SPListItem wpitem = wplist.GetItemById(wpID);

  wpitem.Delete();
}
 
wplist.Update();
}