Skip to content

Instantly share code, notes, and snippets.

@ryanshripat
Last active December 12, 2017 15:59
Show Gist options
  • Save ryanshripat/5fca2526ba6bf18ca298e3159721acfe to your computer and use it in GitHub Desktop.
Save ryanshripat/5fca2526ba6bf18ca298e3159721acfe to your computer and use it in GitHub Desktop.
Modifies the view of a SharePoint Online list
#Add-PSSnapin "Microsoft.SharePoint.PowerShell"
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$siteUrl = Read-Host -Prompt "Enter Site URL"
$username = Read-Host -Prompt "Enter username"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$listTitle = Read-Host -Prompt "Enter List Title"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
$list = $ctx.get_web().get_lists().getByTitle($listTitle);
$DefaultView = $list.DefaultView
$DefaultView.ViewFields.Add("NIB SmartCard Number")
$DefaultView.ViewFields.Add("First Name")
$DefaultView.ViewFields.Add("Last Name")
$DefaultView.ViewFields.Add("Selected Facility")
$DefaultView.ViewFields.Add("Selected Physician")
#$DefaultView.Query = '<OrderBy><FieldRef Name="Created" Ascending="True" /></OrderBy>' #doesn't work
$DefaultView.Update()
$ctx.ExecuteQuery()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment