Tuesday 6 February 2018

SharePoint Content Type Columns Re-order with PowerShell

Multiple SharePoint List re-order using PowerShell


Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
Start-SPAssignment -Global #Start Assignment for disposal
$StartTime = Get-Date
$CurrentDirectory = (Get-Location).Path
$CSVLocation  = "$CurrentDirectory\CSVs"
$SiteCollList = Import-Csv -path "${CSVLocation}\SiteColl.csv"
foreach($SiteColl in $SiteCollList)
{
    $SPWeb = Get-SPWeb $SiteColl.Title
    $CTDetailsList = Import-Csv -path "${CSVLocation}\ColumnOrderDetails.csv"
    foreach($c in $CTDetailsList)
    {
        $ctname=$c.CTName
        $csvFileName=$c.ColumnOrderListName+".csv"
        Write-Host $ctname
        Write-Host $csvFileName
        $SPContentType = $SPWeb.ContentTypes[$c.CTName]
        $FieldList = Import-Csv -path "${CSVLocation}\$csvFileName"
        $OrderedFieldNames = @();
        foreach($Field in $FieldList)
        {
            $OrderedFieldNames += $Field.InternalName 
        }
        Write-Host $OrderedFieldNames
        if($SPContentType.ReadOnly)
        {
            $SPContentType.ReadOnly=$false
            $SPContentType.Update()
        }  
        $FieldToHide = $SPContentType.fields.GetFieldByInternalName("Title")
        $SPContentType.FieldLinks[$FieldToHide.Id].Hidden = $false
        $SPContentType.Update()
        write-Host "Making Title field as visible" -ForegroundColor Green
        write-Host $OrderedFieldNames
        $SPContentType.FieldLinks.Reorder($OrderedFieldNames)
        $SPContentType.Update($true)
        $FieldToHide = $SPContentType.fields.GetFieldByInternalName("Title")
        $SPContentType.FieldLinks[$FieldToHide.Id].Hidden = $true
        $SPContentType.Update()
        write-Host "Making Title field as Hidden" -ForegroundColor Green
        if(!$SPContentType.ReadOnly)
        {
            $SPContentType.ReadOnly=$true
            $SPContentType.Update()
        }
    }
    $SPWeb.Dispose()
}
Stop-SPAssignment -Global #Stop Assignment for disposal
$EndTime = Get-Date
Write-Host "Started:" $StartTime -ForegroundColor Green
Write-Host "Finished:" $EndTime -ForegroundColor Green

Thursday 1 February 2018

Remove Ghost Workflow Or Duplicate Nintex Workflow Instance (Orphaned Object)

Issue Description:- Nintex Workflows are showing duplicate instance present in site collection level and not able to delete it. May be some reference missed between workflow instance mapping and due to that it make orphaned object in the database side.

Resolution:-  This error sometimes occurs when database mappings are changed. This causes entries for workflows in multiple Nintex Content Database. The attached script should resolved the issue for you. It will purge the dbo.workflow table for each Nintex Database. Once the site's inventories are accessed again the tables will be rebuilt and the duplicate entries should be removed.

The script itself shouldn't have any affect on any workflows that are running and shouldn't cause any downtime.


# Clearing Console Window
Clear-Host
# Set Error Preference
#$ErrorActionPreference = 'silentlycontinue'
# BEGIN SCRIPT RUN
Write-Host "-----------------------------------------------------"
Write-host "|       Rebuilding Nintex Workflow Inventory        |"
Write-Host "-----------------------------------------------------"
Write-Host ""
Write-progress -Activity "Adding snapins and assemblies to PowerShell session..." -Id 1 -PercentComplete "5" -Status "Adding SharePoint Snap-ins"
#Adding SharePoint Powershell Snapin
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA silentlycontinue
Write-progress -Activity "Adding snapins and assemblies to PowerShell session..." -Id 1 -PercentComplete "8" -Status "Adding Nintex Assemblies"

# Loading SharePoint and Nintex assemblies into the PS session
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Nintex.Workflow")
[void][System.Reflection.Assembly]::LoadWithPartialName("Nintex.Workflow.SupportConsole")
[void][System.Reflection.Assembly]::LoadWithPartialName("Nintex.Workflow.Administration")
Write-progress -Activity "Opening a connection to the Nintex Configuration Database..." -Id 1 -PercentComplete "15" -Status "Please Wait."
# Grab Nintex Config database name
$CFGDB = [Nintex.Workflow.Administration.ConfigurationDatabase]::OpenConfigDataBase().Database
Start-sleep -Seconds 5
Write-progress -Activity "Opening a connection to the Nintex Database..." -Id 1 -PercentComplete "25" -Status "Processing Databases"
#Creating an instance of the .net SQL Client
$cmd = New-Object -TypeName System.Data.SqlClient.SqlCommand
$cmd.CommandType = [System.Data.CommandType]::Text
$cmd.CommandTimeout = '0'
$Cmd.CommandText = "TRUNCATE Table dbo.workflows;" #TRUNCATING Tables
Write-progress -Activity "Opening a connection to the Nintex Database..." -Id 1 -PercentComplete "50" -Status "Clearing existing WF history Data."
#Removing existing WF inventory from Nintex databases
Write-Host "Clearing existing WF Inventory from Nintex databases..."
Write-Host ""
 foreach ($database in [Nintex.Workflow.Administration.ConfigurationDatabase]::GetConfigurationDatabase().ContentDatabases)
{

    Write-host "     Clearing WF Inventory From DB:"$database.SqlConnectionString #-replace ".\[(.*?)\]."
        $reader = $database.ExecuteReader($cmd)
}
Write-host ""
Write-progress -Activity "Rebuilding Workflow Inventory" -Id 1 -PercentComplete "50" -Status "Please Wait..."
#Repopulating Workflow Inventory Tables
Write-Host "----------"
Write-Host "Queuing new instance of the WF inventory Upgrade (SPTimer) job"
TRY {
    
    [Nintex.Workflow.Administration.UpgradeHelper]::ScheduleInsertWorkflowDatabaseJob()
    Write-Host "Done."
    }
CATCH {
    $ErrorMessage = $_.Exception.Message
    Write-Host "An error occured with the WF inventory rebuild job. Please contact Nintex support with the details below..."
    $ErrorMessage
}
#Finished!
Write-Host "----------"
Write-Host ""
Write-Host "The Nintex Workflow inventory rebuild is complete. "