Tuesday 24 February 2015

Run reusable workflow for all list Items in SharePoint using PowerShell

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
# URL of the Site
$web = Get-SPWeb -Identity https://sharepointsrv/site1
$manager = $web.Site.WorkFlowManager
# Name of the list
$list = $web.Lists["Shared Documents"]
# Name of the Workflow
$assoc = $list.WorkflowAssociations.GetAssociationByName("On Item Created","en-US")
$data = $assoc.AssociationData
$items = $list.Items
foreach($item in $items) {
        $wf = $manager.StartWorkFlow($item,$assoc,$data,$true)
}
$manager.Dispose()
$web.Dispose()

Saturday 21 February 2015

Update SharePoint list or library view using PowerShell

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$GetAllWebDetails = "D:\[Location]\GetAllWebDetails.csv"
#---------------Please do not delete-------
Start-SPAssignment -Global #Start Assignment for disposal
$Time=Get-Date
Write-Host Start Time:$Time -ForegroundColor Yellow `n
$ListNameStore=@("Management","ABC","123")
function UpdateViews($webUrl)
{
    $web = Get-SPWeb -Identity $webUrl
    Write-Host $web.Url
    $webLists = $web.Lists
    for ($i = 0; $i -lt $webLists.Count; $i++)
    {
        $list = $web.Lists[$i];
        foreach($stlist in $ListNameStore) {
            if($list.Title -eq $stlist ) {
          
                $column = $list.Fields["Column Name"]
                if($column -ne $null){  
                    $column.Hidden = $false  
                    $column.ReadOnlyField = $false  
                    $column.Update()
                    $list.Fields.Delete($column) 
                    $list.Update()
                }
              }
          }
    }
   
    $Web.Dispose()
}

IMPORT-CSV $GetAllWebDetails |
  ForEach-Object {
    $WebUrl=$_.WebUrl
      Write-Host  Update Views is Started in $WebUrl -ForegroundColor Green 
    $site = Get-SPSite $WebUrl
    foreach($web in $site.AllWebs){
        UpdateViews  $web.Url
    }
    Write-Host  Update Views is completed in $WebUrl -ForegroundColor Green
}
$EndTime = Get-Date
Write-Host `n End Time:$EndTime -ForegroundColor Yellow

Attach Lookup Columns to document and document set content types using PowerShell

Below is the PowerShell code for adding user in Attach Lookups To Document And DocSet Content Types.
- Need to change some configure details in script.
- Need to chagne in CSV file like ContentType, SiteColumn and ColumnSetting.


Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
Function CheckColumnExistsOnContentType{
Param($ContentType, $ColumnName)      
$Fields = $ContentType.Fields | ? {$_.InternalName -eq $ColumnName}
Write-Host $ColumnName $Fields
If($Fields -eq $Null){
Return $false
}
Else{
Return $true}      
}
 
    $SPSite = Get-SPSite -Identity http://serverName/
    $Web = $SPSite.RootWeb
    $Create = Import-Csv -path "C:\[location]\AttachLookupsToDocumentAndDocSetContentTypes.csv"
$Create
    ForEach($Row in $Create){
        $CT = $Web.ContentTypes[$Row.ContentType];
        $FieldAdd = $Web.Fields.GetFieldByInternalName($Row.SiteColumn)
  $ColumnExists = CheckColumnExistsOnContentType -ContentType $CT -ColumnName $FieldAdd
  if($ColumnExists -eq $True)
  {
   Write-Host "Attached Column" $FieldAdd.InternalName "to" $CT.name -ForegroundColor Cyan
   $FieldLink = New-Object Microsoft.SharePoint.SPFieldLink($FieldAdd)
   If($($_.'ColumnSetting') -eq "Hidden"){
    $FieldLink.Required = $false
    $FieldLink.Hidden = $true
   }
   ElseIf(($($_.'ColumnSetting') -eq "Required")){
        $FieldLink.Required = $true
   }
   Else{
       $FieldLink.Required = $false
   }
   $CT.FieldLinks.Add($FieldLink)
   $CT.Update($true)
   $Web.Update()
   $Web.Dispose()
   Write-Host " Column is added in Content Type" -ForegroundColor Green
  }
}

Flush Blob Cache in SharePoint 2013

Running in all the servers.


Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$webApp = Get-SPWebApplication http://ServerName.com
[Microsoft.SharePoint.Publishing.PublishingCache]::FlushBlobCache($webApp)
Write-Host "Flushed the BLOB cache for:" $webApp