CAML查询以使用Powershell从Sharepoint下载特定文件夹

我有以下脚本,其中我试图从sharepoint下载sharepoint库文档文件夹的子文件夹ZIP中存在的所有zip文件。我陷入了CAML查询中,无法调整为指向特定的子文件夹“ ZIP”。如果我尝试以下脚本 显示的消息是子文件夹ZIP中存在的所有22个文件的这个消息:

您不能在空值表达式上调用方法。

#Load SharePoint CSOM Assemblies
    #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"
    cls
    $fileName = "File_Downloading_Report" #'yyyyMMddhhmm   yyyyMMdd
    $enddate = (Get-Date).tostring("yyyyMMddhhmmss")
    #$filename =  $enddate + '_VMReport.doc'  
    $logFileName = $fileName +"_"+ $enddate+"_Log.txt"   
    $invocation = (Get-Variable Myinvocation).Value  
    $directoryPath = Split-Path $invocation.MyCommand.Path

     $directoryPathForLog=$directoryPath+"\"+"LogFiles"
     if(!(Test-Path -path $directoryPathForLog))  
        {  
            New-Item -ItemType directory -Path $directoryPathForLog
            #Write-Host "Please Provide Proper Log Path" -ForegroundColor Red   
        }   


#$logPath = $directoryPath + "\" + $logFileName 
$logPath = $directoryPathForLog + "\" + $logFileName   
$isLogFileCreated = $False 



#DLL location

$directoryPathForDLL=$directoryPath+"\"+"Dependency Files"
if(!(Test-Path -path $directoryPathForDLL))  
        {  
            New-Item -ItemType directory -Path $directoryPathForDLL
            #Write-Host "Please Provide Proper Log Path" -ForegroundColor Red   
        } 

#DLL location

$clientDLL=$directoryPathForDLL+"\"+"microsoft.SharePoint.Client.dll"
$clientDLLRuntime=$directoryPathForDLL+"\"+"microsoft.SharePoint.Client.dll"

Add-Type -Path $clientDLL
Add-Type -Path $clientDLLRuntime


#File Download location

$directoryPathForFileDownloadLocation=$directoryPath+"\"+"Downloaded Files"
if(!(Test-Path -path $directoryPathForFileDownloadLocation))  
        {  
            New-Item -ItemType directory -Path $directoryPathForFileDownloadLocation
            #Write-Host "Please Provide Proper Log Path" -ForegroundColor Red   
        } 

#File Download location



function Write-Log([string]$logMsg)  
{   
    if(!$isLogFileCreated){   
        Write-Host "Creating Log File..."   
        if(!(Test-Path -path $directoryPath))  
        {  
            Write-Host "Please Provide Proper Log Path" -ForegroundColor Red   
        }   
        else   
        {   
            $script:isLogFileCreated = $True   
            Write-Host "Log File ($logFileName) Created..."   
            [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}",$logMsg)   
            Add-Content -Path $logPath -Value $logMessage   
        }   
    }   
    else   
    {   
        [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}",$logMsg)   
        Add-Content -Path $logPath -Value $logMessage   
    }   
} 



#The below function will download the file from SharePoint Online library.

Function FileDownLoadFromSPOnlinelibrary()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SPOSiteURL,[Parameter(Mandatory=$true)] [string] $SourceFilePath,[Parameter(Mandatory=$true)] [string] $TargetFilePath,[Parameter(Mandatory=$true)] [string] $username,[Parameter(Mandatory=$true)] [string] $Password
    )

    Try 
    {



        $securePassword= $Password | ConvertTo-SecureString -AsPlainText -Force  
        #Setup the Context
        $ctx = New-Object microsoft.SharePoint.Client.ClientContext($SPOSiteURL)
        $ctx.Credentials = New-Object microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$securePassword)


        #sharepoint online powershell download file from library
        $fileInfo = [microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx,$SourceFilePath)
        $writeStream = [System.IO.File]::Open($TargetFilePath,[System.IO.FileMode]::Create)
        $fileInfo.Stream.CopyTo($writeStream)
        $writeStream.Close()

        Write-host -f Green "File '$SourceFilePath' has been downloaded to '$TargetFilePath' successfully!"
    }
    Catch 
    {

            $ErrorMessage = $_.Exception.Message +"in Downloading File!: " +$SourceFilePath
            Write-Host $ErrorMessage -BackgroundColor Red
            Write-Log $ErrorMessage 


    }
}


#Parameters
$siteURL="xxx"
$listName = 'Documents'
$fromDate="2019-10-28"
$toDate="9999-11-10"
$downloadLocation=$directoryPathForFileDownloadLocation;
$username = "xxx"
$password = "xxx"
$securePassword= $password | ConvertTo-SecureString -AsPlainText -Force
#$batchSize =1000

#Parameters ends here.


#Setup the Context
$ctx = New-Object microsoft.SharePoint.Client.ClientContext($siteURL)
$ctx.Credentials = New-Object microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$securePassword)

#Get the List
$list = $ctx.Web.Lists.GetByTitle($listName)
$ctx.Load($list)
$ctx.ExecuteQuery()
$emptyString = ""
$BatchSize="1000"
#Define CAML Query to get Files from the list in batches
$Query = New-Object microsoft.SharePoint.Client.CamlQuery


#Here in the below two line "T13:35:58Z" and "T13:36:34Z" are hard coded static value - because while we construct this camel query thru the camel query builder these values gets appended to the date value,so we need this.
$startDateVar=$fromDate+"T13:35:58Z"  
$endDateVar=$toDate+"T13:36:34Z"
#Here in the below two line "T13:35:58Z" and "T13:36:34Z" are hard coded static value - ends here.


$Query.ViewXml = "@<Where>
      <And>
         <BeginsWith>
            <FieldRef Name='ContentTypeId' />
            <Value Type='Text'>0x0120</Value>
         </BeginsWith>
         <Eq>
            <FieldRef Name='Title' />
            <Value Type='Text'>ZIP</Value>
         </Eq>
      </And>
   </Where>"

#Read more: https://www.sharepointdiary.com/2017/10/sharepoint-online-fix-attempted-operation-is-prohibited-because-it-exceeds-list-view-threshold.html#ixzz64yKeidYG



$count =0

#Get List Items in Batches
Do
{


    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()
    $ListItems.Count

    #Update Postion of the ListItemCollectionPosition
    $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
    $Query.ListItemCollectionPosition

    If ($ListItems.Count -eq 0) 
    { 
    Break
    }

    $downloadItemCount=1;

    #Extract the each list item from the List Items collection.
    ForEach($Item in $ListItems)
    {                  
          #Example to Item metadata - this can be used if we want to download based on some item metadata condition.
          #$documentStatus=$Item["documentStatusColumnName"]         
            try
            {

                $Ctx.Load($Item.File)
                $Ctx.ExecuteQuery()
                #$etagVal=$Item.File.etag



                #$SiteURL=$SiteURL

                #https://globalsharepoint.sharepoint.com/sites/TestSite/Shared%20Documents/LegalDoc.docx        
                $SourceFile=$Item.File.ServerRelativeUrl;
                #$TargetFile="C:\PowerShell\DownLoadFilesFromSPOnline\Downloaded Files\LegalDoc.docx" 
                $TargetFile=$downloadLocation+"\"+$Item.File.Name; 


                if($SourceFile.Contains(".zip"))
                {
                $count++;            

                #Call the function to download file
                FileDownLoadFromSPOnlinelibrary -SPOSiteURL $SiteURL -SourceFilePath $SourceFile -TargetFilePath $TargetFile -username $username -Password $Password

                }

                $fileDownloadingMessage=$downloadItemCount.ToString()+": "+$Item.File.Name; 
                Write-Host $fileDownloadingMessage -BackgroundColor DarkGreen
                Write-Log $fileDownloadingMessage

        $downloadItemCount++

        }
        catch
        { 
            $ErrorMessage = $_.Exception.Message +"in: " +$Item.File.Name
            Write-Host $ErrorMessage -BackgroundColor Red
            Write-Log $ErrorMessage 

        }


    }
    Write-Host "============================================================="
    Write-Host $count
    Write-Host "============================================================="

}While ($Query.ListItemCollectionPosition -ne $null)
fxangin 回答:CAML查询以使用Powershell从Sharepoint下载特定文件夹

您可以在查询中添加is项(文件夹/文件)路径,然后将查询范围设置为RecursiveAll,否则只会查询根目录。

本文链接:https://www.f2er.com/3119182.html

大家都在问