Get sharepoint lists with more than 5000 items Sharepoint Online Powershell - list

Can you please help me with a powershell script in sharepoint online that will get all the list with more than 5000 items ? TYIA!

The following PowerShell script for your reference:
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://xx.sharepoint.com/sites/lz"
$UserName="lz#xx.onmicrosoft.com"
$Password = "xx"
$listName ="customlist"
$rowLimit=100
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecurePassword)
#Bind to Site Collection
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$ctx.Credentials = $Creds
$list = $ctx.Web.Lists.GetByTitle($listName)
$ctx.Load($list)
# Prepare the query
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$query.ViewXml = "<View>
<RowLimit>$rowLimit</RowLimit>
</View>"
# An array to hold all of the ListItems
$items = #()
# Get Items from the List until we reach the end
do
{
$listItems = $list.getItems($query)
$ctx.Load($listItems)
$ctx.ExecuteQuery()
$query.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
foreach($item in $listItems)
{
Try
{
# Add each item
$items += $item
}
Catch [System.Exception]
{
# This shouldn't happen, but just in case
Write-Host $_.Exception.Message
}
}
}While($query.ListItemCollectionPosition -ne $null)

Related

Powershell Script to get all columns and type in a SPList

I want to retrieve all columns, and their type in a SharePoint list.
I have tried several scripts but the results are not what I am looking for.
Below are the scripts I have tried:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Configuration Parameters
$SiteURL="http://url.com/"
$ListName= "theList"
$CSVPath="C:\Temp\list.csv"
#Get the Web and List
$Web = Get-SPWeb $SiteURL
$List = $Web.Lists.TryGetList($ListName)
#Export List Fields to CSV file
$List.Fields | select Title, InternalName | Export-Csv -path $CSVPath -NoTypeInformation
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$url = "http://url.com/"
$listName = "theList"
$path ="C:\Temp\list.csv"
$web = Get-SPWeb $url
$list = $web.Lists.TryGetList($listName)
$fields = $list.ContentTypes | %{ $_.FieldLinks }
$items = #() #array to store objects representing list items
$list.items | %{
$item = $_;
$hash = #{}; #hash table to store field name-value pairs
$fields | %{ $hash[$_.DisplayName] = $item[$_.Name] };
$items += new-object psobject -Property $hash }
$items | Export-Csv -Path $path
Just adding the Type property.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Configuration Parameters
$SiteURL="http://sp:12001"
$ListName= "Customers"
$CSVPath="C:\Lee\script\list.csv"
#Get the Web and List
$Web = Get-SPWeb $SiteURL
$List = $Web.Lists.TryGetList($ListName)
#Export List Fields to CSV file
$List.Fields | select Title, InternalName,Type | Export-Csv -path $CSVPath -NoTypeInformation

Create multiple Folders on server then create network share for each as well

I am trying loop through a text file with a list of folder names I would like to create. Then also create a network share to the folder as well.
I am able to create the folder but I am getting stuck on creating the network share.
$folder ='\\networkserver\D$' #Root Directory to place the New folders in.
$routes = get-content 'C:\uncroutes.txt'
foreach ($routes in $routes) {
$newpath = Join-Path "$folder\" -ChildPath $routes
New-Item $newpath -type Directory
foreach ($newpath in $newpath) {
New-SmbShare -Name $newpath -Path $folder -FullAccess Administrator
}
}
This is the error message:
New-SmbShare -Name $newpath -Path $folder -FullAccess Adminis ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (MSFT_SMBShare:ROOT/Microsoft/Windows/SMB/MSFT_SMBShare) [New-SmbShare], CimException
+ FullyQualifiedErrorId : Windows System Error 5,New-SmbShare
Got it working with Help from #Adminofthings. Here is the working code.
$folder ='\\networkserver\D$' #Root Directory to place the New folders in.
$routes = get-content 'C:\uncroutes.txt' #list of folder Names
foreach ($route in $routes) {
$newpath = Join-Path "$folder\" -ChildPath $route
New-Item $newpath -type Directory
foreach ($ShareName in $ShareNames) {
$ShareName = ($route | sls -pattern "([0-9a-zA-Z-_ ]+)$").matches.value
$serverpath = "d:\$route"
New-SmbShare -Name $ShareName -Path $serverpath -FullAccess Administrator
}
}
If I assume correctly about uncroutes.txt and say that it contains a unc path on each line, then you can get the share name by using something like the code below:
$ShareName = ($route | sls -pattern "([0-9a-zA-Z-_ ]+)$").matches.value
Then pass $ShareName into your -name parameter.

Workflow subscription service does not return any value

I am trying to start the SharePoint 2013 workflow thru the powershell csom model. I have browsed and got many links with the same promising coding. But in my case it is not working. I have tried both in on premises sharepoint 2013 and sharepoint online but none them worked out. Below is the code:
Set-ExecutionPolicy Unrestricted
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"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"
# Specify tenant admin and site URL
$SiteUrl = "https://spoffice365.sharepoint.com/"
$ListName = "Test"
$UserName = "myusername"
$SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString
# Connect to site
$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$ClientContext.Credentials = $credentials
$ClientContext.ExecuteQuery()
# Get List and List Items
$List = $ClientContext.Web.Lists.GetByTitle($ListName)
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$ClientContext.Load($List)
$ClientContext.Load($ListItems)
$ClientContext.ExecuteQuery()
# Retrieve WorkflowService related objects
$WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($ClientContext, $ClientContext.Web)
$WorkflowSubscriptionService = $WorkflowServicesManager.GetWorkflowSubscriptionService()
$WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService()
$ClientContext.Load($WorkflowServicesManager)
$ClientContext.Load($WorkflowSubscriptionService)
$ClientContext.Load($WorkflowInstanceService)
$ClientContext.ExecuteQuery()
# Get WorkflowAssociations with List
$WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
$ClientContext.Load($WorkflowAssociations)
$ClientContext.ExecuteQuery()
# Prepare Start Workflow Payload
$Dict = New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]'
# Loop List Items to Start Workflow
For ($j=0; $j -lt $ListItems.Count; $j++){
$msg = [string]::Format("Starting workflow {0}, on ListItemId {1}", $WorkflowAssociations[0].Name, $ListItems[$j].Id)
Write-Host $msg
#Start Workflow on List Item
$Action = $WorkflowInstanceService.StartWorkflowOnListItem($WorkflowAssociations[0], $ListItems[$j].Id, $Dict)
$ClientContext.ExecuteQuery()
}
The below $WorkflowAssociations does not return any value both in sharepoint 2013 and sharepoint online always comes as empty, even though the $List.Id passed correctly and verified that list has approver workflow configured.
$WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
$ClientContext.Load($WorkflowAssociations)
$ClientContext.ExecuteQuery()
And also would like to learn how to pass approver name to the below method:
$WorkflowInstanceService.StartWorkflowOnListItem($WorkflowAssociations[0], $ListItems[$j].Id, $Dict)

Access next webpage after clicking

Requirement : After clicking on webpage named in $ie.Navigate below. I Need to access HTML / OuterHTML source of Web-page which opens next.
Ex: When I open https://www.healthkartplus.com/search/all?name=Sporanox (by setting $control = Sporanox), below code simply clicks on first matching link. After link is clicked, I need to access HTML of resulting page.
Update : referred another SO question and learned that we can search appropriate window. Code seems to be working for some scenarios but not for all. For $ie2 I get problem accessing Document property.
function getStringMatch
{
# Loop through all 2 digit combinations in the $path directory
foreach ($control In $controls)
{
$ie = New-Object -COMObject InternetExplorer.Application
$ie.visible = $true
$site = $ie.Navigate("https://www.healthkartplus.com/search/all?name=$control")
$ie.ReadyState
while ($ie.Busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }
$link = $null
$link = $ie.Document.get_links() | where-object {$_.innerText -eq "$control"}
$link.click()
while ($ie.Busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }
$ie2 = (New-Object -COM 'Shell.Application').Windows() | ? {
$_.Name -eq 'Windows Internet Explorer' -and $_.LocationName -match "^$control"
}
# NEED outerHTML of new page. CURRENTLY it is working for some.
$ie.Document.body.outerHTML > d:\med$control.txt
}
}
$controls = "Sporanox"
getStringMatch
I think the issue is when you look for the links in the first page.
The link innerText is not equal to $control, it contains $control i.e. innerText is "Sporanox (100mg)".
The following might help:
$link = $ie.Document.get_links() | where-object {if ($_.innerText){$_.innerText.contains($control)}}
EDIT
Here is the complete code I'm using:
function getStringMatch
{
# Loop through all 2 digit combinations in the $path directory
foreach ($control In $controls)
{
$ie = New-Object -COMObject InternetExplorer.Application
$ie.visible = $true
$site = $ie.Navigate("https://www.healthkartplus.com/search/all?name=$control")
$ie.ReadyState
while ($ie.Busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }
$link = $null
$link = $ie.Document.get_links() | where-object {if ($_.innerText){$_.innerText.contains($control)}}
$link.click()
while ($ie.Busy)
{
sleep -Milliseconds 100
}
# NEED outerHTML of new page. CURRENTLY it is working for some.
$ie.Document.body.outerHTML > d:\med$control.txt
}
}
$controls = "Sporanox"
getStringMatch

How I can show and add items to Sharepoint List via Powershell?

hi I want to show and add items in Sharepoint via Powershell (I use for testing a list and a libary)
The name is Powershell-Test
the Url is
http//intranet/departments/it/abt/Lists/Powershell-Test/AllItems.aspx
I enable remote on the Sharepoint Server and make a remote connection to this from my localhost.
Enter-PSSession -ComputerName [sharepoint server] -Authentication Negotiate -Credential [Domain\name]
if((Get-PSSnapin -Name Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.Sharepoint.Powershell
}
$webURL = "http//intranet/departments/it/abt/Lists/"
$listname = "Powershell-Test"
$web = Get-SPWeb $webURL
$web.lists | format-table title
$list = $web.LIsts[$listName]
$list.items | foreach { $_[„Title“] + „`n“ + $_[„Body“] }
The Problem is that I don#t get the connection to the list :(
How I can to this?
I presume you have permission to access the site.
I also presume you don't have a site or subsite with the name "Lists".
I don't think you can use Get-SPWeb with the path "/Lists/" within it (unless the site was named "lists").
Could you try:
if ((Get-PSSnapin -Name Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.Powershell
}
$siteURL = "http//intranet/departments/it/abt/"
$web = Get-SPWeb $siteURL
$list = $web.Lists["Powershell-Test"]
$list.Items | foreach { $_["Title"] + "`n" + $_["Body"] }