I am trying to create a scheduled task to export a SharePoint list that is hosted in another domain. I have the username and password for the account that has access to the that list, and I am looking for a script that would export the list using the provided account.

    $SiteUrl = "SiteURL"
    $ListName= "ListName"
    $ExportFile ="D:\ListItems.csv"

        $Credentials = New-Object System.Net.NetworkCredential("UserName", "password")
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
        $Ctx.Credentials = $Credentials


    #Get the List
    $List = $Ctx.web.Lists.GetByTitle($ListName)

    #Get All List Items
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $ListItems = $List.GetItems($Query)
    $FieldColl = $List.Fields

    #Array to Hold List Items
    $ListItemCollection = @()

    #Fetch each list item value to export to excel
     Foreach($Item in $ListItems)
        $ExportItem = New-Object PSObject
        Foreach($Field in $FieldColl)
                if($NULL -ne $Item[$Field.InternalName])
                    #Expand the value of Person or Lookup fields
                    $FieldType = $Item[$Field.InternalName].GetType().name
                    if (($FieldType -eq "FieldLookupValue") -or ($FieldType -eq "FieldUserValue"))
                        $FieldValue = $Item[$Field.InternalName].LookupValue
                        $FieldValue = $Item[$Field.InternalName]  
                $ExportItem | Add-Member -MemberType NoteProperty -name $Field.InternalName -value $FieldValue
        #Add the object with above properties to the Array
        $ListItemCollection += $ExportItem
    #Export the result Array to CSV file
    $ListItemCollection | Export-CSV $ExportFile -NoTypeInformation

    Write-host "List data Exported to CSV file successfully!"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.