{"id":522,"date":"2013-03-25T10:52:57","date_gmt":"2013-03-25T10:52:57","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=522"},"modified":"2024-04-26T12:56:15","modified_gmt":"2024-04-26T12:56:15","slug":"adding-a-sharepoint-view-to-every-library","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/adding-a-sharepoint-view-to-every-library\/","title":{"rendered":"Adding a SharePoint View to every library"},"content":{"rendered":"<h4>How to add a SharePoint View to every library<\/h4>\n<p>Sometimes one has to create a custom view and deploy it to every library. This example walks through a set of explicitly named site collections within a given Managed Path, and adds a custom view. To enable running it multiple times, the script first deletes every instance of that named view, if any exist. Otherwise we would have a new duplicate view in each library each time we ran this script. Note both filtering and sort order is specified by a CAML query. This example shows a very simple one (sort by filename) but it\u2019s possible to construct complex queries. The two approaches I like to use is use a tool called CAMLBuilder, or create a test view, and then navigate the object model and extract the CAML query. Lastly I add the fields using the static (internal) name in the preferred sequence.<\/p>\n<p>&nbsp;<\/p>\n<pre lang=\"php\">#adds \"Field\" to right  in Default View; deletes field first, in case it already exists, so this can be rerun safely\r\nAdd-PSSnapin \"Microsoft.SharePoint.PowerShell\" -ErrorAction SilentlyContinue\r\n \r\n# Script changes the default View. \r\n$envrun=\"Prod\"          # selects environment to run in\r\n \r\nif ($envrun -eq \"Dev\")\r\n{\r\n}\r\nelseif ($envrun -eq \"Prod\")\r\n \r\n{\r\n$siteUrl = \"http :\/\/SharePoint\/ManagedPath\/\"\r\n$LoopString = \"A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,09\"  #these are the explicitly named Site Collections\r\n$LoopStringArr = $LoopString.Split(\u201c,\u201d)\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 \r\n$myheader = \"STARTING: $(get-date)\"\r\n \r\nforeach ($letter in $LoopStringArr)\r\n{\r\n$SiteName=$siteurl+$letter\r\n \r\n$rootSite = New-Object Microsoft.SharePoint.SPSite($SiteName)\r\n \r\n \r\n $site=$rootSite  #skipping traversing all sites for now\r\n  write-host $site.Url\r\n \r\n  if ($true) #this is useful to uncomment as a filter\r\n#  if ($site.Url -like \"$siteurl\/personal\/plau*\")  \r\n  {\r\n   Write-Host -foregroundcolor darkblue \"$($site.id) - $($site.Url) - $($site.contentdatabase.id) - $($site.contentdatabase.name)\"  \r\n \r\n  \r\n   $rootWeb = $site.RootWeb\r\n   $web=$rootWeb;\r\n    \r\n   $JPLists=$web.Lists;\r\n   $JPListsCount=$JPLists.Count  #it much more efficient to resolve Lists object and count outside the loop\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       \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    }\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    }\r\n    elseif (($JPLib.ThumbnailsEnabled) -or ($JPLib.DefaultView -eq \"AllSlides\"))\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    }\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    }\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 \r\n#hah, don't even delete and add if found, just cycle\r\n$x=$null;\r\ntry\r\n{\r\n$x=$JPLib.Views.get_Item(\"NEW View\")\r\n}\r\ncatch {$x=$null}\r\nif ($x -ne $null)\r\n{\r\ncontinue;\r\n}\r\n \r\n \r\ntry\r\n{\r\n$x=$JPLib.Views.get_Item(\"NEW View\")\r\nif ($x.id -ne $null) #prevents duplicate entries\r\n{\r\n    $JPLib.Views.Delete($x.ID.ToString())\r\n}\r\n}\r\ncatch\r\n{}\r\n \r\nif ($JPLib.Views[\"NEW View\"] -eq $null) #prevents duplicate entries\r\n{\r\n  \r\n     $viewQuery = '&lt;OrderBy&gt;&lt;FieldRef Name=\"FileLeafRef\" \/&gt;&lt;\/OrderBy&gt;'  #This is any CAML query you construct\r\n      \r\n     #let's add the fields, by internal name, in the preferred sequence\r\n     $viewFields = New-Object System.Collections.Specialized.StringCollection\r\n     $viewFields.Add(\"DocIcon\") &gt; $null\r\n     $viewFields.Add(\"LinkFilename\") &gt; $null\r\n     $viewFields.Add(\"Title\") &gt; $null\r\n     $viewFields.Add(\"Modified\") &gt; $null\r\n     $viewFields.Add(\"Editor\") &gt; $null\r\n     $viewFields.Add(\"ProductCode\") &gt; $null\r\n     $viewFields.Add(\"ProductDescription\") &gt; $null\r\n     $viewFields.Add(\"DocumentType\") &gt; $null\r\n     $viewFields.Add(\"ReportSubType\") &gt; $null\r\n       \r\n     #RowLimit property\r\n     $viewRowLimit = 30\r\n     #Paged property\r\n     $viewPaged = $true\r\n     #DefaultView property\r\n     $viewDefaultView = $false\r\n     #$ViewQuery=$null;\r\n     $viewTitle=\"NEW View\"\r\n     $newview = $JPLib.Views.Add($viewTitle, $viewFields, $viewQuery, $viewRowLimit, $viewPaged, $viewDefaultView)\r\n   } #endif view doesn't exist\r\n \r\n    }\r\n    }\r\n \r\ntry\r\n{\r\n$rootweb.Dispose()\r\n$web.Dispose()\r\n}\r\ncatch{}\r\n \r\n} #if $true\/Siteurl is not null, if environment setup is valid\r\ntry\r\n{\r\n$rootSite.Dispose()\r\n$site.Dispose()\r\n}\r\ncatch{}\r\n} #foreach letter\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How to add a SharePoint View to every library Sometimes one has to create a custom view and deploy it to every library. This example walks through a set of explicitly named site collections within a given Managed Path, and adds a custom view. To enable running it multiple times, the script first deletes every [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":524,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[26],"tags":[],"class_list":["post-522","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\/522","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\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=522"}],"version-history":[{"count":4,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/522\/revisions"}],"predecessor-version":[{"id":542,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/522\/revisions\/542"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/524"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}