none
How to get the view of a SP list in PS code RRS feed

  • Question

  • Hello,

    I have this code:

    $List = $Context.web.Lists.GetByTitle("MyList")
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $ListItems = $List.GetItems($Query)
    $context.Load($ListItems)
    $context.ExecuteQuery() 
    $ListItemCollection = @() 
    $ListItems | foreach {
    $ExportItem = New-Object PSObject
    $ExportItem | Add-Member -MemberType NoteProperty -name "Datum" -value $_["Datum"].ToLocalTime().ToString("dd-MM-yyyy");
    $ExportItem | Add-Member -MemberType NoteProperty -name "00:00 - 07:59" -value $_["00:00 - 07:59"]
    $ExportItem | Add-Member -MemberType NoteProperty -name "08:00 - 16:59" -value $_["08:00 - 16:59"]
    $ExportItem | Add-Member -MemberType NoteProperty -name "17:00 - 23:59" -value $_["17:00 - 23:59"]
    $ExportItem | Add-Member -MemberType NoteProperty -name "Opmerkingen" -value $_["Opmerkingen"]
    $ListItemCollection += $ExportItem
    }
    $ListItemCollection | epcsv "c:\Temp\MyList" -NoT

    I found this but I don't know how to implement it:

    #Get the Target List
    $list = $web.Lists["test"]
    #Get the View
    $view = $list.Views["Today"] 
    
    #Get All items from the view
    $items = $list.GetItems($view)

    Gr . P

    Wednesday, January 23, 2019 8:05 AM

Answers

  • Nether piece of code is correct or usable as written.

    Also learn to format code correctly.  The methods you are using are old PS v2 methods and make things harder.

    $list = $Context.web.Lists.GetByTitle('MyList')
    $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
    $camlQuery.ViewXml = @'
    <View>
        <Query>
            <Where>
                <Geq>
                    <FieldRef Name='ID'/>
                    <Value Type='Number'>10</Value>
                </Geq>
            </Where>
        </Query>
        <RowLimit>100</RowLimit>
    </View>
    '@
    $listItems = $List.GetItems($camlQuery)
    $context.Load($listItems)
    $context.ExecuteQuery()
    
    $ListItems | 
        ForEach-Object {
            [pscustomobject]@{
                Datum = $_['Datum'].ToLocalTime().ToString('dd-MM-yyyy');
                '00:00 - 07:59' =  $_['00:00 - 07:59']
                '08:00 - 16:59'= $_['08:00 - 16:59']
                '17:00 - 23:59' = $_['17:00 - 23:59']
                'Opmerkingen' = $_['Opmerkingen']
            }
        } | 
        Export-Csv 'c:\Temp\MyList' -NoTypeInformation

    This way eliminates the internal pipeline.

    The "camlQuery" is just an example and it has to be adjusted to your list.  Note that it defines the "Viwe" object.

    For more specific information post in the SharePoint developers forum.


    \_(ツ)_/


    • Edited by jrv Wednesday, January 23, 2019 8:46 AM
    • Marked as answer by PeetK Wednesday, January 23, 2019 9:13 AM
    Wednesday, January 23, 2019 8:44 AM

All replies

  • Nether piece of code is correct or usable as written.

    Also learn to format code correctly.  The methods you are using are old PS v2 methods and make things harder.

    $list = $Context.web.Lists.GetByTitle('MyList')
    $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
    $camlQuery.ViewXml = @'
    <View>
        <Query>
            <Where>
                <Geq>
                    <FieldRef Name='ID'/>
                    <Value Type='Number'>10</Value>
                </Geq>
            </Where>
        </Query>
        <RowLimit>100</RowLimit>
    </View>
    '@
    $listItems = $List.GetItems($camlQuery)
    $context.Load($listItems)
    $context.ExecuteQuery()
    
    $ListItems | 
        ForEach-Object {
            [pscustomobject]@{
                Datum = $_['Datum'].ToLocalTime().ToString('dd-MM-yyyy');
                '00:00 - 07:59' =  $_['00:00 - 07:59']
                '08:00 - 16:59'= $_['08:00 - 16:59']
                '17:00 - 23:59' = $_['17:00 - 23:59']
                'Opmerkingen' = $_['Opmerkingen']
            }
        } | 
        Export-Csv 'c:\Temp\MyList' -NoTypeInformation

    This way eliminates the internal pipeline.

    The "camlQuery" is just an example and it has to be adjusted to your list.  Note that it defines the "Viwe" object.

    For more specific information post in the SharePoint developers forum.


    \_(ツ)_/


    • Edited by jrv Wednesday, January 23, 2019 8:46 AM
    • Marked as answer by PeetK Wednesday, January 23, 2019 9:13 AM
    Wednesday, January 23, 2019 8:44 AM
  • Thank you P.
    Wednesday, January 23, 2019 9:13 AM