{"id":556,"date":"2013-03-25T11:13:04","date_gmt":"2013-03-25T11:13:04","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=556"},"modified":"2024-04-26T12:55:51","modified_gmt":"2024-04-26T12:55:51","slug":"adding-one-field-to-a-sharepoint-view-in-every-library-in-every-site","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/adding-one-field-to-a-sharepoint-view-in-every-library-in-every-site\/","title":{"rendered":"Adding one field to a SharePoint View in every library in every site"},"content":{"rendered":"<h4>Adding a field every SharePoint View<\/h4>\n<p>Sometimes one needs to change a SharePoint library view across all libraries and sites. Here\u2019s 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\u2019t 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.<\/p>\n<pre lang=\"php\">#adds \"Field\" to right  in Default View; deletes field first, in case it already exists, then moves the field into position. \r\nAdd-PSSnapin \"Microsoft.SharePoint.PowerShell\" -ErrorAction SilentlyContinue\r\n \r\n$envrun=\"Prod\"          # selects environment to run in\r\nif ($envrun -eq \"Dev\")\r\n{\r\n# you can always tune Dev to behave differently.  I prefer to make my scripts multi-environment enabled\r\n}\r\nelseif ($envrun -eq \"Prod\")\r\n{\r\n$siteUrl = \"http :\/\/SharePoint\/\"  #use ytour site colelction URL, or choose to loop through all site collections\r\n}\r\nelse\r\n{\r\nWrite-Host \"ENVIRONMENT SETTING NOT VALID: script terminating...\"\r\n$siteUrl =  $null;\r\nreturn;\r\n}\r\n \r\nWrite-Host \"script starting\"\r\n$myheader = \"STARTING: $(get-date)\"\r\n \r\n$SiteName=$SiteURL\r\n$rootSite = New-Object Microsoft.SharePoint.SPSite($SiteName)\r\n \r\n $site=$rootSite  #skipping traversing all sites for now\r\n \r\n   Write-Host -foregroundcolor darkblue \"$($site.id) - $($site.Url) - $($site.contentdatabase.id) - $($site.contentdatabase.name)\"  \r\n  \r\n   $rootWeb = $site.RootWeb\r\n   $web=$rootWeb;\r\n    \r\n   $JPLists=$web.Lists;\r\n   $JPListsCount=$JPLists.Count\r\n    \r\n   for ($i=0;$i -lt $JPListsCount;$i++)\r\n   { \r\n   $JPLib=$JPLists[$i];\r\n   $A_Lib_Count++;\r\n   $SkipLib=$true; #true\r\n    \r\n   if ( ($JPlib.BaseType -ne \"DocumentLibrary\") -or ($JPlib.hidden) )\r\n    {\r\n      # forget the rest and return to top\r\n      Write-Host -foregroundcolor green \"fast test skipping Library: $($JPlib)\";   \r\n      Add-Content $mylogfile \"Skipping Library: $($JPlib.title)`n\";   \r\n    }\r\n    elseif ($JPLib.Title -Match \"SitesAssets|Photo|Image|CustomizedsReports|Templates|Pages|Picture|cache|style|Slide\")\r\n    {\r\n      # forget the rest and return to top\r\n      Write-Host -foregroundcolor red \"fast test skipping Library because it mentions $Matches: $($JPlib)\";   \r\n    }\r\n    elseif ($JPLib.BaseTemplate -ne \"DocumentLibrary\")   #alternatively, only skip if -eq XMLForm\r\n    {\r\n      # forget the rest and return to top\r\n      Write-Host -foregroundcolor red \"fast skipping Library because it is not of base DocumentLibrary, it is BaseType:$($JPlib.basetemplate): $($JPlib.title)\";   \r\n    }\r\n    elseif (($JPLib.ThumbnailsEnabled) -or ($JPLib.DefaultView -eq \"AllSlides\"))\r\n    {\r\n \r\n      # forget any library with thumbnails, these are not normal doclibs, and return to top\r\n      Write-Host -foregroundcolor red \"fast test skipping Library because it has Thumbnails\/Slides $($JPlib)\";   \r\n    }\r\n    elseif (!$JPLib.ContentTypesEnabled)\r\n    {\r\n      Write-Host -foregroundcolor red \"skipping because it does not have CTs enabled: $($JPlib)\";   \r\n    }\r\n    else\r\n    {  $SkipLib=$false; }\r\n      \r\n    if (!$SkipLib)\r\n    {\r\n      write-Host -foregroundcolor green \"Processing Library: $($JPlib)\";   \r\n       \r\n      try\r\n      {\r\n      $ListView = $JPLib.Views[\"All Documents\"];  #This is the one view we are looking for\r\n      }\r\n      catch\r\n      {\r\n      $ListView=$null;\r\n      }\r\n       \r\n      if ($ListView -eq $null)  #let's not try to add a column to a non-existent view.\r\n      {\r\n        continue;\r\n      }\r\n       \r\n      $ListFields=$JPLib.Fields;\r\n      $ListViewFields=$ListView.ViewFields;\r\n       \r\n      #We might have duplicate entries; let's delete them all, then when we add the field, we know there's only one field\r\n      $stillDeleting=$true;\r\n      do {\r\n        try   {$ListViewfields.Delete(\"LinkFilename\")}   #note the use of the internal (static) name whenever deleting \r\n        catch {$stillDeleting=$false}\r\n      } while ($stillDeleting)\r\n       \r\n      $stillDeleting=$true;   #similar field, just to be sure\r\n      do {\r\n        try   {$ListViewfields.Delete(\"LinkFilenameNoMenu\")}\r\n        catch {$stillDeleting=$false}\r\n      } while ($stillDeleting)\r\n       \r\n       \r\n       \r\n      #Re-add field\r\n      $ListViewFields.add(\"LinkFilename\");\r\n      #Move Field to position #2\r\n      $ListViewFields.MoveFieldTo(\"LinkFilename\",1);\r\n       \r\n      $ListView.Update();  #note no update() method is required\r\n \r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Adding a field every SharePoint View Sometimes one needs to change a SharePoint library view across all libraries and sites. Here\u2019s 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 [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":561,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[26],"tags":[],"class_list":["post-556","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=556"}],"version-history":[{"count":1,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/556\/revisions"}],"predecessor-version":[{"id":568,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/556\/revisions\/568"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/561"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}