Add Column to Default View Using PowerShell

Recently I had a request from a client to add a custom site collection column to all document libraries. This is pretty easy though the SharePoint UI of course, by creating a custom Site Column in the collection and adding it to the ‘Document’ content type.

However, in doing this the column is not added to the default view for any document libraries. This can be easily done using PowerShell using the script below. This adds the column to the default view in every document library throughout the entire site collection (referenced here).

$site = Get-SPSite “http://sharepoint.com”
$column = “Column”

$site | Get-SPWeb -limit all | ForEach-Object {

# Get all document libraries
$lists = $_.Lists | where  {$_.BaseType -eq “DocumentLibrary”}

# Loop libraries
for ($i = 0; $i -lt $lists.Count; $i++)
{

try
{

# Get current view
$view = $lists[$i].DefaultView

if($view)
{

# Delete if already exist
while($view.ViewFields.ToStringCollection().Contains($column))
{

$view.ViewFields.delete($column)
$view.Update()

}

# Add column
if(!$view.ViewFields.ToStringCollection().Contains($column))
{

$view.ViewFields.add($column)
$view.Update()

}

}

}
catch [Exception]
{

write-output (”  Error: ” + $_.Exception.ToString())

}

}

}

$site.Dispose()

You can also add views to a view by URL and view name for one specific view or list (a nice tip I found here). Here is another option by using GetViewFromUrl:

$spWeb = Get-SPWeb -Identity "http://mySharePoint"
$spView = $spWeb.GetViewFromUrl("/Lists/MyList/AllItems.aspx")
$spField = $spList.Fields["MyField"]
$spView.ViewFields.Add($spField)
$spView.Update()

You can also try to use SPList object as below:

$spList = Get-SPList -Url "http://mySharePoint/Lists/MyList"
$spView = $spList.Views["All Items"]
$spField = $spList.Fields["MyField"]
$spView.ViewFields.Add($spField)
$spView.Update()

Adding multiple columns to a view can also be done with PowerShell, and even create a new view and set it as the default view. This is really awesome if you want to leave the default ‘All Items’ view in tact, but create a new custom view and set it as default (found here):

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$siteURL = "http://dev1share"
 $site = Get-SPSite($siteURL)
foreach($web in $site.AllWebs) {
$listCounter = $web.Lists.Count
 for($i=0;$i -le $listCounter;$i++) {
    $list = $web.Lists[$i]
     if($list.BaseType -eq "DocumentLibrary") {
     $newList = $web.Lists.item($list.ID);
  $viewfields = New-Object System.Collections.Specialized.StringCollection
   $viewfields.Add("DocIcon")
   $viewfields.Add("LinkFilename")
   $viewfields.Add("_UIVersionString")
   $viewfields.Add("Modified")
   $viewfields.Add("Created")
   $viewfields.Add("Editor")
   $viewfields.Add("FileSizeDisplay")
  [void]$newList.Views.Add("Detailed", $viewfields, "", 100, $true, $true)
   $newList.Update();
  $view=$newList.Views["Detailed"]
   $view.DefaultView = $true
   $view.Update()
 }
 }
$web.Dispose();
 }
 $site.Dispose();

 

SharePoint PowerShell Create View Based on Existing View

The following PowerShell script crawls the specified lists, copies the fields from the All Items views and creates a new view named "Created By Me" and sets it as default. Tested with SharePoint 2010 but should work with 2013 as well. Tested with list but not with Document Libraries.

I used this script as a base.

$ver = $host | select version

if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 

{

 Add-PSSnapin "Microsoft.SharePoint.PowerShell"

}

 

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 

 

$web = Get-SPWeb -Identity "http://portal/sites/Requests"

$lists=$web.Lists["User Request", "Hardware Request",  "Employee Request"]

$SourceView="All Items"

$NewViewName="Created By Me"

$NewViewDefault=$true

 

 

foreach($list in $lists) {

 

 $view = $list.Views[$SourceView]

 $Viewfields = $list.Views[$SourceView].ViewFields.ToStringCollection()

 $viewRowLimit="100"

 $viewPaged=$true

 $viewDefaultView=$NewViewDefault

 

# Setting the Query for the View

 $viewQuery = "<ORDERBY><FIELDREF name="" false="" ascending=""></FIELDREF></ORDERBY><WHERE><EQ><FIELDREF author="" name=""><VALUE type="" integer=""><USERID type="" integer=""></USERID></VALUE></FIELDREF></EQ></WHERE>"

 $viewName = $NewViewName

 

# Finally – Provisioning the View

 $myListView = $list.Views.Add($viewName, $viewFields, $viewQuery, 100, $True, $False, "HTML", $False)

 

# You need to Update the View for changes made to the view

# Updating the List is not enough

 $myListView.DefaultView = $True

 $myListView.Update()

 $list.Update()

}

$web.Dispose()

How to create List using Power shell Script in SharePoint 2013

In this post we are going to see how to create Custom List from Power Shell in SharePoint 2013.

Create a SharePoint Custom List  Student Info with Columns
SNo    -  Number
SName  -  Text
Gender -  Choice
Photo  -  URL

#To which site u want to create the list
$spWeb=Get-SPWeb -Identity http://mySharePoint.ca

#List type or template
$spTemplate = $spWeb.ListTemplates["Custom List"]

#Get all the lists to the listcollection
$spListCollection=$spWeb.Lists

#adding the new list to the list collection
$spListCollection.Add("Studentlist","Studentlist",$spTemplate)

#get the path of subsite and sitecollecion
$path = $spWeb.url.trim()

#get the list to the list object
$spList = $spWeb.GetList("$path/Lists/Studentlist")

#adding the field type(Number) to the list
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Number
$spList.Fields.Add("SNo",$spFieldType,$false)

#adding the field type(Text) to the list
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Text
$spList.Fields.Add("SName",$spFieldType,$false)

#adding the field type(choice) to the list
$choices = New-Object System.Collections.Specialized.StringCollection
$choices.Add("Female")
$choices.Add("Male")
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Choice
$spList.Fields.Add("Gender",$spFieldType,$false,$false,$choices)

#adding the field type(url) to the list
$spList.Fields.Add("Photo","URL",$false)

$Views = $spList.Views["All Items"]
$Views.ViewFields.Add("SNo")
$Views.ViewFields.Add("SName")
$Views.ViewFields.Add("Gender")
$Views.ViewFields.Add("Photo")

$Views.Update() - See more at: http://www.dotnetsharepoint.com/2013/06/how-to-create-list-using-power-shell.html#.U9_gPu90y9I