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

No comments:

Post a Comment