Adding a field every SharePoint View
Sometimes one needs to change a SharePoint library view across all libraries and sites. Here’s how to do it. First loop through sites/webs/libraries and find the target libraries. In this example, I use Content Types enabled as one of the criteria. Next, I delete the field I want to add. This is to make the script safe to re-run. If we didn’t try to delete the field first, we would end up with a view with multiple columns for the same field. Note no Update() method is required to alter a View.
#adds "Field" to right in Default View; deletes field first, in case it already exists, then moves the field into position.
Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
$envrun="Prod" # selects environment to run in
if ($envrun -eq "Dev")
{
# you can always tune Dev to behave differently. I prefer to make my scripts multi-environment enabled
}
elseif ($envrun -eq "Prod")
{
$siteUrl = "http ://SharePoint/" #use ytour site colelction URL, or choose to loop through all site collections
}
else
{
Write-Host "ENVIRONMENT SETTING NOT VALID: script terminating..."
$siteUrl = $null;
return;
}
Write-Host "script starting"
$myheader = "STARTING: $(get-date)"
$SiteName=$SiteURL
$rootSite = New-Object Microsoft.SharePoint.SPSite($SiteName)
$site=$rootSite #skipping traversing all sites for now
Write-Host -foregroundcolor darkblue "$($site.id) - $($site.Url) - $($site.contentdatabase.id) - $($site.contentdatabase.name)"
$rootWeb = $site.RootWeb
$web=$rootWeb;
$JPLists=$web.Lists;
$JPListsCount=$JPLists.Count
for ($i=0;$i -lt $JPListsCount;$i++)
{
$JPLib=$JPLists[$i];
$A_Lib_Count++;
$SkipLib=$true; #true
if ( ($JPlib.BaseType -ne "DocumentLibrary") -or ($JPlib.hidden) )
{
# forget the rest and return to top
Write-Host -foregroundcolor green "fast test skipping Library: $($JPlib)";
Add-Content $mylogfile "Skipping Library: $($JPlib.title)`n";
}
elseif ($JPLib.Title -Match "SitesAssets|Photo|Image|CustomizedsReports|Templates|Pages|Picture|cache|style|Slide")
{
# forget the rest and return to top
Write-Host -foregroundcolor red "fast test skipping Library because it mentions $Matches: $($JPlib)";
}
elseif ($JPLib.BaseTemplate -ne "DocumentLibrary") #alternatively, only skip if -eq XMLForm
{
# forget the rest and return to top
Write-Host -foregroundcolor red "fast skipping Library because it is not of base DocumentLibrary, it is BaseType:$($JPlib.basetemplate): $($JPlib.title)";
}
elseif (($JPLib.ThumbnailsEnabled) -or ($JPLib.DefaultView -eq "AllSlides"))
{
# forget any library with thumbnails, these are not normal doclibs, and return to top
Write-Host -foregroundcolor red "fast test skipping Library because it has Thumbnails/Slides $($JPlib)";
}
elseif (!$JPLib.ContentTypesEnabled)
{
Write-Host -foregroundcolor red "skipping because it does not have CTs enabled: $($JPlib)";
}
else
{ $SkipLib=$false; }
if (!$SkipLib)
{
write-Host -foregroundcolor green "Processing Library: $($JPlib)";
try
{
$ListView = $JPLib.Views["All Documents"]; #This is the one view we are looking for
}
catch
{
$ListView=$null;
}
if ($ListView -eq $null) #let's not try to add a column to a non-existent view.
{
continue;
}
$ListFields=$JPLib.Fields;
$ListViewFields=$ListView.ViewFields;
#We might have duplicate entries; let's delete them all, then when we add the field, we know there's only one field
$stillDeleting=$true;
do {
try {$ListViewfields.Delete("LinkFilename")} #note the use of the internal (static) name whenever deleting
catch {$stillDeleting=$false}
} while ($stillDeleting)
$stillDeleting=$true; #similar field, just to be sure
do {
try {$ListViewfields.Delete("LinkFilenameNoMenu")}
catch {$stillDeleting=$false}
} while ($stillDeleting)
#Re-add field
$ListViewFields.add("LinkFilename");
#Move Field to position #2
$ListViewFields.MoveFieldTo("LinkFilename",1);
$ListView.Update(); #note no update() method is required
}
}
Want to talk?
Drop us a line. We are here to answer your questions 24*7.