The following example demonstrates how to retrieve list items using SharePoint CSOM API in PowerShell:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
Function Get-SPOContext([string]$Url,[string]$UserName,[string]$Password)
{
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
return $context
}
Function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
$list = $Context.Web.Lists.GetByTitle($listTitle)
$qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$items = $list.GetItems($qry)
$Context.Load($items)
$Context.ExecuteQuery()
return $items
}
$UserName = "jdoe@contoso.onmicrosoft.com"
$Password = Read-Host -Prompt "Enter the password"
$Url = "https://contoso.sharepoint.com/"
$context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
$items = Get-ListItems -Context $context -ListTitle "Tasks"
foreach($item in $items)
{
#...
}
$context.Dispose()
SharePoint Online: PowerShell to Get All Lists
Here is the PowerShell script to get all lists and libraries from given SharePoint online site
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
Add -Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add -Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$SiteUrl = "https://crescent.sharepoint.com/sites/sales"
$UserName = "salaudeen@crescent.com"
$Password = "Password goes here"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials( $UserName ,( ConvertTo-SecureString $Password -AsPlainText -Force ))
Try {
$Context = New-Object Microsoft.SharePoint.Client.ClientContext( $SiteUrl )
$Context .Credentials = $credentials
$Lists = $Context .web.Lists
$Context .Load( $Lists )
$Context .ExecuteQuery()
ForEach ( $List in $Lists )
{
Write-host $List .Title
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
|
SharePoint Online: PowerShell Script to Iterate through all lists and libraries in a site collection
Now, Lets make this code into a re-usable function and get list and library from all sites and subsites of given site.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
Add -Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add -Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$SiteUrl = "https://crescent.sharepoint.com/sites/sales"
$UserName = "salaudeen@crescent.com"
$Password = "Password goes here"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials( $UserName ,( ConvertTo-SecureString $Password -AsPlainText -Force ))
Try {
Function Get -SPOList ( $Web )
{
$Lists = $Web .Lists
$Context .Load( $Lists )
$Context .ExecuteQuery()
ForEach ( $List in $Lists )
{
Write-host $List .Title
}
}
Function Get -SPOWeb ( $WebURL )
{
$Context = New-Object Microsoft.SharePoint.Client.ClientContext( $WebURL )
$Context .Credentials = $Credentials
$Web = $context .Web
$Context .Load( $web )
$Context .Load( $web .Webs)
$Context .executeQuery()
Write-host "Processing Web :" $Web .URL
Get -SPOList $Web
foreach ( $Subweb in $web .Webs)
{
Get -SPOWeb ( $SubWeb .URL)
}
}
Get -SPOWeb $SiteUrl
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
|
Script Output:
#Read more: http://www.sharepointdiary.com/2015/08/sharepoint-online-get-all-lists-using-powershell.html#ixzz5PesVQOkQ
No comments:
Post a Comment