PowerShell examples - Get-SPOList and Get-SPOListItems

Silver Contributor

Does anyone have any suggestions about where i can find some good examples using the pnp Get-SPOList and Get-SPOListItems cmdlets?  The examples in github are very basic and I'm struggling with the best way to get field values so that i can use them in other commands.

 

not being able to search the old yammer forum is very frustrating (:

14 Replies

Hi @Dean Gross

 

Have a look at this:

 

http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/EBooks/09212016073021AM/PdfFile/basic-o...

 

What are you trying to do?

 

the basic examples are:

 

$web = Get-SPOWeb

$lists = Get-SPOList -web $web 

$list = $lists['ListName']

$listitems = Get-SPOListItem -web $web -list $list 

 

 

Thanks, i just trying to read some values from a list so that I can use them to create some new site collections. The fields are getting returned without any values,
I tried the example from the above document, and I get the following
Supply values for the following parameters:
Title :
Description :
BaseTemplate ID :
Created Date :
List ID :
ImageUrl :
ItemCount :
OnQuickLaunch :

Other spo cmdlets seem to be working fine. I can't figure out what would be causing me to get no values.

 

I figure out that i need to include \Lists\ in the listname parameter.

 

Now I just need to figure out how to get the value out of a column that returns Microsoft.SharePoint.Client.FieldUrlValue

 

I relearned that this can be done by using the following syntax

$listItem["SPOUrl"].Url


@Dean Gross wrote:

Thanks, i just trying to read some values from a list so that I can use them to create some new site collections. The fields are getting returned without any values,
I tried the example from the above document, and I get the following
Supply values for the following parameters:
Title :
Description :
BaseTemplate ID :
Created Date :
List ID :
ImageUrl :
ItemCount :
OnQuickLaunch :

Other spo cmdlets seem to be working fine. I can't figure out what would be causing me to get no values.

 

I figure out that i need to include \Lists\ in the listname parameter.

 

Now I just need to figure out how to get the value out of a column that returns Microsoft.SharePoint.Client.FieldUrlValue

 

I relearned that this can be done by using the following syntax

$listItem["SPOUrl"].Url


Hi I have the same issue with the Author "Created By"column as this Client.FieldUserValue .   I thought I could use but it doesn't quite work Smiley Mad

 

 

Get-SPOListItem  $listName  -Fields "FileLeafRef", "Modified", "Author" | %{new-object psobject -property  @{Id = $_.Id; Name = $_["FileLeafRef"]; Modified = $_["Modified"]; Author = $_["Author"]  }} | select Id, Name, Modified, Author

 

I can see you have used another approach and probably get-spoContext

Hi @Daniel Westerdale, @Dean Gross

 

Sorry I missed something in my previous  example.

 

this is the correct code:

 

$web = Get-SPOWeb

$lists = Get-SPOList -web $web
$list = $lists |where {$_.Title -eq "Sales list"}
$items = Get-SPOListItem -web $web -List $list
$items[1].FieldValues

HI @Pieter Veenstra @Dean Gross

 

Thanks,  I might end up using that.  I did something similar a while back but I thought I need to do 

SPOExecute as we do for standard CSOM to populate our client side objects.  

 

Anyway I digress,  I intended to bring back some standard field for each list item  with one line of PS code using a pipeline but it doesn't look like it  

 

 

 

Hi @Daniel Westerdale,

 

The one line option Smiley Happy :

 

(Get-SPOListItem -web (Get-SPOWeb) -List ((Get-SPOList -web (Get-SPOWeb))|where {$_.Title -eq "Sales list"}))[1].FieldValues


@Pieter Veenstra wrote:

Hi @Daniel Westerdale,

 

The one line option Smiley Happy :

 

(Get-SPOListItem -web (Get-SPOWeb) -List ((Get-SPOList -web (Get-SPOWeb))|where {$_.Title -eq "Sales list"}))[1].FieldValues


@Pieter Veenstra 

 

Thanks - I like it but I have discoved the same issue as I have  my code snippet  

 

I need those names for auditing purposes

 

Id Name    Modified            Author                                    
-- ----    --------            ------                                    
24 24_.000 17/11/2016 14:44:39 Microsoft.SharePoint.Client.FieldUserValue
25 25_.000 17/11/2016 15:01:47 Microsoft.SharePoint.Client.FieldUserValue
27 27_.000 17/11/2016 14:47:39 Microsoft.SharePoint.Client.FieldUserValue

to get the Author details you could do this:

 

(Get-SPOListItem -web (Get-SPOWeb) -List ((Get-SPOList -web (Get-SPOWeb))|where {$_.Title -eq "Sales list"}))[1].FieldValues.Author

 

(Get-SPOListItem -web (Get-SPOWeb) -List ((Get-SPOList -web (Get-SPOWeb))|where {$_.Title -eq "Sales list"}))[1].FieldValues.Author.Email

 

(Get-SPOListItem -web (Get-SPOWeb) -List ((Get-SPOList -web (Get-SPOWeb))|where {$_.Title -eq "Sales list"}))[1].FieldValues.Author.LookupValue

 

 

Thanks very much I use that.

@Pieter Veenstra wrote:

to get the Author details you could do this:

 

(Get-SPOListItem -web (Get-SPOWeb) -List ((Get-SPOList -web (Get-SPOWeb))|where {$_.Title -eq "Sales list"}))[1].FieldValues.Author

 

(Get-SPOListItem -web (Get-SPOWeb) -List ((Get-SPOList -web (Get-SPOWeb))|where {$_.Title -eq "Sales list"}))[1].FieldValues.Author.Email

 

(Get-SPOListItem -web (Get-SPOWeb) -List ((Get-SPOList -web (Get-SPOWeb))|where {$_.Title -eq "Sales list"}))[1].FieldValues.Author.LookupValue

 

 


Everytime you use Get-SPO.... it triggers a call to the server. for perfomance enhancements (and since the ListTitle is already hardcoded) use something like:

$url = "http://localhost"

Connect-SPOnline –Url $url -CurrentCredentials

(Get-SPOListItem -List "Sales list")[1].FieldValues.Author.LookupValue

 

It saves in this case 50% of the calls to the server. or better:

$url = "http://localhost"

Connect-SPOnline –Url $url -CurrentCredentials

$SalesListItems = Get-SPOListItem -List "Sales list"

$SalesListItems[1].FieldValues.Author

$SalesListItems[1].FieldValues.Author.Email

$SalesListItems[1].FieldValues.Author.LookupValue

 

Only 2 calls to the server in this case, instead of 12 and saves 3 Where-clauses on the clientside, since the server is much faster. You can also add CAML to filter through items

$query = "<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>123</Value></Eq></Where></Query></View>"

$SalesListItems = Get-SPOListItem -List "Sales list" -Query $query


@Kwok-Ho Lam wrote:

 

Only 2 calls to the server in this case, instead of 12 and saves 3 Where-clauses on the clientside, since the server is much faster. You can also add CAML to filter through items

$query = "<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>123</Value></Eq></Where></Query></View>"

$SalesListItems = Get-SPOListItem -List "Sales list" -Query $query


On the subject of using a query , Get-PnPListItem seems limited, if we want to bring the non default fieds.  Say I view all FIles in libraries and or Folders in  one or more Team Sites that were modified in the last month

 

$startDate = Get-Date "01/04/2017" 
$startDatestring = $startDate.ToString("yyyy-MM-ddTHH:mm:ssZ")
$endDate = Get-Date "01/05/2017"
$endDatestring = $endDate.ToString("yyyy-MM-ddTHH:mm:ssZ")

$query =   `
 "<View><Query><Where>  `
   <And> `
      <Geq> `
         <FieldRef Name='Modifed' /> <Value IncludeTimeValue='FALSE' `
          Type='DateTime'>" + $startDatestring + "</Value> `
      </Geq> `
      <Leq> `
        <FieldRef Name='Modifed' /> `
         <Value IncludeTimeValue='FALSE' Type='DateTime'>" + $endDatestring + "</Value> `
     </Leq> `
    </And> `
   </Where></Query></View>"

# THIS WON'T WORK AS YOU NEED -Fields "FileLeafRef", "Modified" IF YOU WANT TO GET THIS INCLUDED
# IN THE OUTPUT BUT THS MEANS -QUERY IS IGNORED Get-PnPListItem -List Document -Query $query -Web $locationWeb

If the above is not possible then it is back to bringing everything back....

 

Foreach($item in $items) {
write-host $item.Modified.ToString()
// check Modified date range here

}

Just seems not the quickest way to get the results I am looking for ;-( . Is the query with extended attributes something I should request?

@Pieter Veenstra

 

 

Good morning.  I have previously written PS to get file data on every library and every site without issues. Today, I am asked to report on the output from a Nintex form and Workflow I created last year:

 

The ps below works ( even though I hate the syntax) .  However, I now want to bring back multiple fields such as FieldValues.Last_x0020_Name etc, I awas hoping to do this with a single line - this is what I am struggling with:

 

 

(Get-PnPListItem -web (Get-PnPWeb) -List ((Get-PnPList -web (Get-PnPWeb))|where {$_.Title -eq $listNewJoiners})).FieldValues.First_x0020_Name

 

 

How about using a select

 

so for example you could run

dir | select Name, Length

to display files in a folder, but just display their name and length.

 

 

Similar your example:

 

(Get-PnPListItem -web (Get-PnPWeb) -List ((Get-PnPList -web (Get-PnPWeb))|where {$_.Title -eq $listNewJoiners})).FieldValues  | select First_x0020_Name, Last_x0020_Name

 

@Pieter Veenstra

 

Thanks , all I needed was to pipe the Select statement and then field nivarna .  Thanks again

 

 

Daniel