PowerShell - Update default list view by adding 2 columns for all site under all site collection under specified Managed Path:-
<#
.SYNOPSIS
<A brief description of the script>
.DESCRIPTION
<A detailed description of the script>
.PARAMETER <paramName>
<Description of script parameter>
.EXAMPLE
<An example of using the script>
#>
$UrlDochubWebApp = Read-Host "Enter destination web app url"
$FieldPropetyInternalname = "Property"; # column 1
$FieldTitleInternalname = "Title"; # Column 2
#Loading Snap in for Microsoft.Sharepoint.Powershell
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{
Write-Host "Loading SharePoint Powershell Snapin" -ForegroundColor Green
$snapin = Add-PSSnapin "Microsoft.Sharepoint.Powershell"
Write-Host $snapin
}
function updateView($list)
{
$View = $list.DefaultView;
$ViewFields = $View.ViewFields;
try
{
if(!$ViewFields.Exists($FieldTitleInternalname))
{
$ViewFields.add($FieldTitleInternalname);
$ViewFields.MoveFieldTo($FieldTitleInternalname,2);
}
if(!$ViewFields.Exists($FieldPropetyInternalname))
{
$View.ViewFields.add($FieldPropetyInternalname);
$View.ViewFields.MoveFieldTo($FieldPropetyInternalname,4);
}
$View.update();
}
catch [System.Exception]
{
Write-Host $_;
}
}
function updateLIbraries()
{
$Webapplication =GEt-SPWebAPplication $UrlDochubWebApp
$AllLibraryNames = New-Object "System.Collections.Specialized.StringCollection"
$AllLibraryNames.Add("Library1") | Out-Null;
$AllLibraryNames.Add("Library2") | Out-Null ;
$AllLibraryNames.Add("Library3") | Out-Null ;
$AllLibraryNames.Add("LIbrary4") | Out-Null ;
if($Webapplication -ne $null)
{
$Allsites = $Webapplication.Sites
foreach($Site in $Allsites)
{
$siteUrl = $Site.Url;
foreach($web in $site.AllWebs)
{
try
{
$WebURl = $web.Url;
if(($web.Url.toLOwer() -match ("/debt/"))) # Condition to check managed path
{
$WebURl = $web.Url;
$allLists = $web.Lists;
$alllistsCount = $allLists.Count;
for($counter = 0;$Counter -lt $alllistsCount;$counter++)
{
$list = $allLists[$counter];
$ListTitle = $list.Title
$LIstITemCount = $list.ItemCount;
if(($list.BaseType -eq "DocumentLibrary") -and ($AllLibraryNames.Contains("$ListTitle") ) )
{
updateView $list;
}
}
Write-Host "Updated for $WebURl";
}
}
finally
{
$web.Dispose();
}
}
}
}
}
updateLIbraries;
<#
.SYNOPSIS
<A brief description of the script>
.DESCRIPTION
<A detailed description of the script>
.PARAMETER <paramName>
<Description of script parameter>
.EXAMPLE
<An example of using the script>
#>
$UrlDochubWebApp = Read-Host "Enter destination web app url"
$FieldPropetyInternalname = "Property"; # column 1
$FieldTitleInternalname = "Title"; # Column 2
#Loading Snap in for Microsoft.Sharepoint.Powershell
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{
Write-Host "Loading SharePoint Powershell Snapin" -ForegroundColor Green
$snapin = Add-PSSnapin "Microsoft.Sharepoint.Powershell"
Write-Host $snapin
}
function updateView($list)
{
$View = $list.DefaultView;
$ViewFields = $View.ViewFields;
try
{
if(!$ViewFields.Exists($FieldTitleInternalname))
{
$ViewFields.add($FieldTitleInternalname);
$ViewFields.MoveFieldTo($FieldTitleInternalname,2);
}
if(!$ViewFields.Exists($FieldPropetyInternalname))
{
$View.ViewFields.add($FieldPropetyInternalname);
$View.ViewFields.MoveFieldTo($FieldPropetyInternalname,4);
}
$View.update();
}
catch [System.Exception]
{
Write-Host $_;
}
}
function updateLIbraries()
{
$Webapplication =GEt-SPWebAPplication $UrlDochubWebApp
$AllLibraryNames = New-Object "System.Collections.Specialized.StringCollection"
$AllLibraryNames.Add("Library1") | Out-Null;
$AllLibraryNames.Add("Library2") | Out-Null ;
$AllLibraryNames.Add("Library3") | Out-Null ;
$AllLibraryNames.Add("LIbrary4") | Out-Null ;
if($Webapplication -ne $null)
{
$Allsites = $Webapplication.Sites
foreach($Site in $Allsites)
{
$siteUrl = $Site.Url;
foreach($web in $site.AllWebs)
{
try
{
$WebURl = $web.Url;
if(($web.Url.toLOwer() -match ("/debt/"))) # Condition to check managed path
{
$WebURl = $web.Url;
$allLists = $web.Lists;
$alllistsCount = $allLists.Count;
for($counter = 0;$Counter -lt $alllistsCount;$counter++)
{
$list = $allLists[$counter];
$ListTitle = $list.Title
$LIstITemCount = $list.ItemCount;
if(($list.BaseType -eq "DocumentLibrary") -and ($AllLibraryNames.Contains("$ListTitle") ) )
{
updateView $list;
}
}
Write-Host "Updated for $WebURl";
}
}
finally
{
$web.Dispose();
}
}
}
}
}
updateLIbraries;
No comments:
Post a Comment