SharePoint : Powershell snippet to upgrade sandbox solution

In previous article Upgrade SharePoint Sandbox Solution, we have explained how to upgrade the sandbox solution so that necessary changes must reflected properly. To upgrade the solution, do not deploy it from Visual Studio because it will retract the previous activated solution from solutions gallery and add new one.

You can use below powershell snippet, which will upgrade the existing sandbox solution with new one. Since solution package (*.wsp) have same solution id even if we rename them. So here we are finding the already activated sandbox solution in solution gallery from new solution package.


function Update-SPUserSolutons
{
		param($solutionFolderPath, $siteUrl)
		
		$files = Get-ChildItem -Path $solutionFolderPath -Recurse -File
		foreach ($file in $files)
		{
			if($(Get-SPUserSolution -Site $siteUrl -Identity $file.Name -ErrorAction SilentlyContinue) -eq $null)
			{
				Add-SPUserSolution -Site $siteUrl -LiteralPath $file.Fullname
                                # Get already activated solution from solution gallery
				$solutionName = Get-SPUserSolutionId -siteUrl $siteUrl -name $file.Name
				write-host "Upgrading solution $solutionName to $file.Name"
				Update-SPUserSolution -Identity $solutionName -Site $siteUrl -ToSolution $file.Name
				write-host "$file.Name upgraded"				
			}
		}
}

function Get-SPUserSolutionId
{
		param($siteUrl, $name)
		
		$newSolution = Get-SPUserSolution -Site $siteUrl -Identity $name;
		
		$solution = Get-SPUserSolution -Site $siteUrl |?{$_.SolutionId -eq $newSolution.SolutionId -and $_.Status -eq "Activated"}|select -first 1
		return $solution.Name;
}

#Call below function to upgrade solution
Update-SPUserSolutons -solutionFolderPath "E:\SandboxSolution\" -siteUrl "http:\\your-sitecollection-url" 

Note : In solutionFolderPath parameter, provide the folder path where you have placed your wsp. Make sure you have only wsp packages in that folder.

SharePoint : Get User’s login name from claims encoded value

There are sometimes scenarios where we have to get user’s login name and we have only claims encoded value (like this i:0#.w|domain\username or i:0#.f|membershipprovider|abc.xyz@comp.com). In SharePoint 2013 / 2010, there is SPClaimProviderManager class available which can decode the claims and provide user’s login name.

I have created a method, which accepts claims encoded value as parameter and returns user’s login name (only if claims are resolved properly).

private string GetUsernameFromClaim(string claimsEncodedUsername)
{
 try
 {
 SPClaimProviderManager mgr = SPClaimProviderManager.Local;
 if (mgr != null)
 {
 if (SPClaimProviderManager.IsEncodedClaim(claimsEncodedUsername))
 {
 // return the normal domain\username without any claims identification data
 return mgr.DecodeClaim(claimsEncodedUsername).Value;
 }
 }
 }
 catch (Exception ex)
 {
 return claimsEncodedUsername; // Or just return the original username.
 }

 // Return the original username value if it couldn't be resolved as a claims username
 return claimsEncodedUsername;
}

Note : Add namespace “Microsoft.SharePoint.Administration.Claims” to use above class.

Issue in SharePoint Folder name with apostrophe ( ` ) and Launch Dialog property of List / Library

In SharePoint list / library items are created for storing the data. Generally user wants a granular structure for maintaining data and for this they creates folder and place their items. Now, if “Launch Dialog” box property is set to “Yes” for this list / library, then details are shown in dialog box. Now if folder name contains apostrophe ( ‘ ) then JavaScript fails in SharePoint and dialog box will not open for all items which are placed inside that folder.

In SharePoint, certain characters are not accepted while creating folder names : ~ ” # % & * : < > ? / \ { | }.
But apostrophe ( ‘ ) is accepted by SharePoint in folder name, and above mentioned issue will applicable only when launch dialog property of list / library is enabled.

If dialog box of items are not opening properly, then look for all parent folders of that item and check whether those folder contains apostrophe ( ‘ ) or not. If yes, then rename that folder name by removing apostrophe.