none
Create new view powershell OrderBy Sort

    Question

  • Hello everybody.

    I am creating a whole structute of spaces with PowerShell.

    When I create a new view with powershell I use this code.

    $viewFields = New-Object System.Collections.Specialized.StringCollection
    $viewFields.Add("Type")
    $viewFields.Add("Name")
    $viewFields.Add("Modified")
    $viewFields.Add("Modified By")
    $query = "<OrderBy><FieldRef Name='Last Updated' Ascending='false' /></OrderBy>"
    $view = $list.Views.Add("WebPartView", $viewFields, $query, 3, $false, $false)
    $view.TabularView = $false
    $view.Update()

    Which is correct and working fine.
    The question is about the Sort opcion. When I apply the sorting through the query, it is being stored there. I can check the item on PowerShell and the query is stored in the $view.Query property.
    And after that, when I go to edit the view, there is no sorting applied (it is somehow inside the view, on the query level) so if someday I would like to change that sorting, it would not be possible.

    How can I apply the sorting option with powershell, using the normal system (the one that later could be modified by editing the view) and not the query style?

    Thanks all.

    Monday, January 28, 2013 10:29 AM

Answers

  • Hi Javi P,

    For this issue to create a list view with sort, I have a test with the following code snippet, it works as expected, sort the list, and when you open the view settings page, the sort option is shown as the query OrderBy settings, you can have a try:

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
    
    $site = new-object Microsoft.SharePoint.SPSite("http://sitename") # is a legit url
    
    $web = $site.RootWeb
    
    $list=$web.Lists["custom list"]
    
    # 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("testcolumn")
    
    #
    
    # View Name
    
    $viewName = “MyViewTest”
    
    # 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()
    
    $Site.Dispose()
    

    Thanks,


    Qiao Wei
    TechNet Community Support

    Friday, February 01, 2013 6:33 AM
    Moderator

All replies

  • Hi Javi,

    Do you only have a problem with not seeing the correct sorting options in the Edit View page, or do you also not have the actual sorting applied when you open the list view?

    A few suggestions - try using all capital letters for specifying false in the ascending option like so:

    $query = "<OrderBy><FieldRef Name='Last Updated' Ascending='FALSE' /></OrderBy>"
    

    Also, you should make sure that there is a column Last Updated in your list, and you are referring to it by its internal name, not display name.

    The easiest approach that I use to troubleshoot such issues is to create the desired view from the browser, then open the list with SharePoint Manager, and look at the generated CAML for the view.

    Regards,


    Teodora

    Tuesday, January 29, 2013 7:49 AM
  • Thank you Teodora.

    I am sorry, I cannot use other software like Sharepoint Manager, or even Sharepoint Designer.

    Every view I create with that code, returns this error when trying to display the view, so I guess it is not working as I thought.

    Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Microsoft SharePoint Foundation-compatible HTML editor such as Microsoft SharePoint Designer. If the problem persists, contact your Web server administrator.

    And I tried sorting by an not problematic field like Name. Is there any other way to obtain the internal names for the fields, apart from sharepoint manager?

    Thank you again Teodora.
    Tuesday, January 29, 2013 10:09 AM
  • Hi Javi,

    In PowerShell you can do this:

    Write-Host $list.Fields["Last Updated"].InternalName

    Hope this helps,


    Teodora


    Tuesday, January 29, 2013 10:34 AM
  • Hi Teodora.

    I can get the Internal name of all the fields (which by the way, most of them where different, thanks) but now, when I create the view, it doesnt appear on the view list of the library. I can not select it, if I try to access it from the address bar of the navigator, the page does not exists.

    However, when I request the $list.Views object the view is created correctly. Any idea about this?

    Thanks.

    Tuesday, January 29, 2013 10:53 AM
  • Hi Javi P,

    For this issue to create a list view with sort, I have a test with the following code snippet, it works as expected, sort the list, and when you open the view settings page, the sort option is shown as the query OrderBy settings, you can have a try:

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 
    
    $site = new-object Microsoft.SharePoint.SPSite("http://sitename") # is a legit url
    
    $web = $site.RootWeb
    
    $list=$web.Lists["custom list"]
    
    # 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("testcolumn")
    
    #
    
    # View Name
    
    $viewName = “MyViewTest”
    
    # 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()
    
    $Site.Dispose()
    

    Thanks,


    Qiao Wei
    TechNet Community Support

    Friday, February 01, 2013 6:33 AM
    Moderator
  • Please refer my post to create list, fields and view SharePoint list using powershell script:
    http://sharepointquicksolutions.blogspot.in/2014/06/create-sharepoint-list-using-powershell.html

    http://sharepointquicksolutions.blogspot.in/2014/06/create-view-in-list-using-powershell.html

    Please provide your valuable feedback.
    Saturday, June 21, 2014 6:17 PM