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.

Upgrade SharePoint Sandbox Solution

There are certain scenarios where we have to deploy the sandbox solution again with new functionality (like new column in list). Since existing solution already stores crucial data, then retracting the previous version of solution will result in losing of existing data.

Let say, you have sandbox solution which is already deployed on production with version 1.0.0.0. Now you have to made some changes in list schema of the same solution. To do that, follow these steps to make changes in solution so that upgradation works well:
  1. Make changes in your list like add new column and save it.
  2. Open feature which is linked with list.
  3. Click on “Manifest” option. Expand Edit options.
  4. Replace with the below content, change ListName with you list Name and save it. Here I am upgrading the list in which I have added one more column.
    <?xml version=”1.0″ encoding=”utf-8″ ?>
    <Feature xmlns=”http://schemas.microsoft.com/sharepoint/“>
    <UpgradeActions>
    <VersionRange BeginVersion=”0.0.0.0″ EndVersion=”1.9.9.9″>
    <ApplyElementManifests>
    <ElementManifest Location=”ListName\Elements.xml” />
    </ApplyElementManifests>
    </VersionRange>
    </UpgradeActions>
    </Feature>
  5. Now open properties of same feature (press F4 when you are at feature window). There you will find Version property. Currently it is blank and you have put there any version which lies within 0.0.0.0 to 1.9.9.9
  6. Let say I am putting 1.1.0.0 and save it.
  7. Publish the solution and create wsp package.
  8. Rename the wsp package because existing solution with same name is already present.
  9. Now open Sharepoint management shell with admin account.
  10. Execute below powershell commands to add and upgrade the new solution
    1. Add-SPUserSolution -Site <sitecollectionURL> -LiteralPath <full path of sandbox solution wsp>
    2. Update-SPUserSolution -Identity <existingSolutionName> -Site $workspaceUrl -ToSolution <newSolutionName>
  11. Go to list settings and you will find new column.

Note : Follow above steps in case you have to update content type. Changes will be done at step 4 where you have to use AddContentTypeField instead of ApplyElementManifests

For more details, please follow read below articles:

Create additional lookup columns using powershell in SharePoint

In SharePoint, we can create additional lookup columns through user interface but the internal name of these fields are not appropriate. Also, there is no provision to create additional fields for “User” or “UserMulti” type fields.
The below powershell can create additional columns by taking certain parameters

  1. $web : SharePoint web object
  2. $parentListName : List name in which additional lookup columns need to be created
  3. $parentCol : Lookup column internal name
  4. $childColName : Additional column name which we want to create
  5. $lookupColType : Type of additional lookup column (e.g. “Lookup”, “LookupMulti”, “User”, “UserMulti”)
  6. $showField : Field to be bind with additional column (e.g. “ID”)
function CreateAdditionalLookupColumns([Microsoft.SharePoint.SPWeb] $web, [string] $parentListName, [string] $parentCol, [string] $childColName, [string] $lookupColType, [string] $showField)
{
$parentList = $web.Lists.item($parentListName)

$fieldLookup = $parentList.Fields.GetFieldByInternalName($parentCol)
#write-host $fieldLookup.SchemaXml `r`n

if($lookupColType -eq "Lookup" -or $lookupColType -eq "User")
{
$schemaXML = ''
$schemaXML = [string]::Format($schemaXML,$childColName, $fieldLookup.Id, [guid]::NewGuid(), $childColName, $lookupColType, $fieldLookup.LookupList, $showField, $childColName, $fieldLookup.SourceID);
}
elseif($lookupColType -eq "UserMulti")
{
$schemaXML = ''
$schemaXML = [string]::Format($schemaXML,$childColName, $fieldLookup.Id, [guid]::NewGuid(), $childColName, $lookupColType, $fieldLookup.LookupList, $showField, $childColName, $fieldLookup.SourceID);
}
elseif($lookupColType -eq "LookupMulti")
{
$schemaXML = ''
$schemaXML = [string]::Format($schemaXML,$childColName, $fieldLookup.Id, [guid]::NewGuid(), $childColName, $lookupColType, $fieldLookup.LookupList, $showField, $childColName, $fieldLookup.SourceID);
}

#write-host $schemaXML `r`n

write-host "---------" $parentListName "----------------------" `r`n

$fieldExist = $parentList.Fields.GetFieldByInternalName($childColName)
if($fieldExist -ne $null)
{
write-host "field deleted : " $fieldExist `r`n
$parentList.Fields.Delete($childColName)
}

$fieldNew = $parentList.Fields.AddFieldAsXml($schemaXML);
write-host "field created : " $fieldNew `r`n
write-host "-----------------------------------------------------" `r`n
}

$web = Get-SPWeb "http://siteURL"    # Your site URL

CreateAdditionalLookupColumns $web "ParentList" "ParentLookupColumnName" "AdditionalLookupColumnName" "Lookup" "ID"

CreateAdditionalLookupColumns $web "ParentList" "ParentLookupColumnName" "AdditionalLookupColumnName" "User" "Name"

Quota template for the site collection in SharePoint

The Quota template means to Lock or Unlock the SiteColletion for the user. It can be temporary or permanently used to prevent the access of the SharePoint site to the end users.

How to configure lock

These are the following steps to enable lock in sitecollection of SharePoint.

  • Go to Central Admin then click on Application Management
  • In the Site Collection section, click on Configure quotas and locks
  • Then Look for your site collection should be selected. (if not, then change to your site collection.)
  • In Site Lock Information section, select one of the following options
    • Unlock
    • NoAdditions
    • ReadOnly
    • NoAccess

Types of Locks in SharePoint

  • UnLock:  to unlock the site collection and make it available to users
  • NoAdditions: to prevent users from adding new content to the site collection. Updates and deletions are still allowed
  • ReadOnly: to prevent users from adding, updating, or deleting content
  • NoAccess: to prevent users from accessing the site collection and its content. Users who attempt to access the site receive an error.

 

To lock or unlock a site collection by using Windows PowerShell

The command to set lock using powershell is as follows

Set-SPSite -Identity “<SiteCollection>” -LockState “<State>”

Creating and configuring SharePoint 2013 search using powershell

In previous post of Sharepoint search overview, we knows about the SharePoint search architecture and its different components. Today, we are going to create and configure about Search service application (SSA) using powershell.
SharePoint search service application crawls the content of site collections and provides search functionality. The powershell needs to run in SharePoint management shell in SharePoint server. The script is suitable only for single server SharePoint farm.

# Get App Pool
$saAppPoolName = "SearchAppPool"

# Search Specifics, suitable for single server farm
$searchServerName = (Get-ChildItem env:computername).value
$serviceAppName = "Search Service Application"
$searchDBName = "SearchService_DB"

# Grab the Application Pool for Service Application Endpoint
$saAppPool = Get-SPServiceApplicationPool $saAppPoolName

# Start Search Service Instances
Write-Host "Starting Search Service Instances..."
Start-SPEnterpriseSearchServiceInstance $searchServerName
Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $searchServerName

# Create the Search Service Application and Proxy
Write-Host "Creating Search Service Application and Proxy..."
$searchServiceApp = New-SPEnterpriseSearchServiceApplication -Name $serviceAppName -ApplicationPool $saAppPoolName -DatabaseName $searchDBName
$searchProxy = New-SPEnterpriseSearchServiceApplicationProxy -Name "$serviceAppName Proxy" -SearchApplication $searchServiceApp

# Clone the default Topology (which is empty) and create a new one and then activate it
Write-Host "Configuring Search Component Topology..."
$clone = $searchServiceApp.ActiveTopology.Clone()
$searchServiceInstance = Get-SPEnterpriseSearchServiceInstance
New-SPEnterpriseSearchAdminComponent –SearchTopology $clone –SearchServiceInstance $searchServiceInstance
New-SPEnterpriseSearchContentProcessingComponent –SearchTopology $clone -SearchServiceInstance $searchServiceInstance
New-SPEnterpriseSearchAnalyticsProcessingComponent –SearchTopology $clone -SearchServiceInstance $searchServiceInstance
New-SPEnterpriseSearchCrawlComponent –SearchTopology $clone -SearchServiceInstance $searchServiceInstance
New-SPEnterpriseSearchIndexComponent –SearchTopology $clone -SearchServiceInstance $searchServiceInstance
New-SPEnterpriseSearchQueryProcessingComponent –SearchTopology $clone -SearchServiceInstance $searchServiceInstance
$clone.Activate()