powershell : create list + add columns + set new default view


Had to create custom list using powershell + add columns + create a new view + set that as a default view. For the longest time, the default view just would not be set – no errors, till it dawned that I need to update the view once it is set to default (and not just update the list).

I could also not find a relevant example to add declared columns to a new view (within msdn or google), so including that as well. Check the two lines in bold below.

Here is an example:


#
$webURL = “http://spurl/site/”
$spSite = [Microsoft.SharePoint.SPSite]($webURL)
$spWeb = $spSite.OpenWeb()
#
# Create a custom list (ID is 100)
$myListGuid = $spWeb.Lists.Add(“CustInfo”, “Customer Information for Northern Projects”, 100)
$myList = $spweb.Lists[$myListGuid]
#
# Update the List Title
$myListObj = $spWeb.Lists[“CustInfo”]
$myListObj.Title = “Customer Information”
$myListObj.Update()
#
# Adding fields
$myFieldCustomerName = $myList.Fields.Add(“CustomerName”, “Text”, 0)
$myList.Fields[$myFieldCustomerName].Title = “Customer Name”
$myList.Fields[$myFieldCustomerName].Update()
#
# Adding fields
$myFieldCustomerAvailable = $myList.Fields.Add(“CustomerAvailable”, “Boolean”, 0)
$myList.Fields[$myFieldCustomerAvailable].Title = “Customer Available”
$myList.Fields[$myFieldCustomerAvailable].Update()
#
# Adding fields
$myFieldCustomerDate = $myList.Fields.Add(“CustomerDate”, “DateTime”, 0)
$myList.Fields[$myFieldCustomerDate].Title = “Customer Date”
$myList.Fields[$myFieldCustomerDate].DisplayFormat = “DateOnly”
$myList.Fields[$myFieldCustomerDate].Update()
#
# Setting the Query for the View
$viewQuery = “<OrderBy><FieldRef Name=””Modified”” Ascending=””False”” /></OrderBy>”
#
# Adding fields to the view
$viewFields = New-Object System.Collections.Specialized.StringCollection
$viewFields.Add(“Attachments”)
$viewFields.Add(“LinkTitle”)
$viewFields.Add($myFieldCustomerName)
$viewFields.Add($myFieldCustomerDate)
#
# View Name
$viewName = “view_name_here”
#
# Finally – Provisioning the View
$myListView = $myList.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()
#
$myList.OnQuickLaunch = $True
$myList.Update()
#
$spWeb.Dispose()
$spSite.Dispose()

Advertisements

4 thoughts on “powershell : create list + add columns + set new default view

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s