0

I have created powershell script to get File/Folder Size details of Sharepoint site.

However, I am not able to get all fieldValues of listItems. I am getting values for $listItem.FieldValues["FileRef"], $listItem.FieldValues["FileRef"] etc. whereas When I am trying $listItem.FieldValues["File_x0020_Size"] or $listItem.FieldValues["EncodedAbsUrl"], I am getting nothing. In CSOM, We can include required property in .Load, Not sure how to do it in Powershell. Thank You in Advance

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
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"

function retrieveListItems($_web)
{

try
{
    Write-host "Processing Web :"$_web.URL
    $listCollection=$_web.Lists;
    $ctx.Load($listCollection);
    $ctx.ExecuteQuery();


    foreach ($list in $listCollection)
    {
        $ctx.Load($list.RootFolder)
        $ctx.ExecuteQuery()
        $weburl= $ctx.Web.ServerRelativeUrl
        $listUrl =$("{0}{1}" -f $ctx.Web.Url.Replace($ctx.Web.ServerRelativeUrl,''), $list.RootFolder.ServerRelativeUrl)


       if($list.Hidden -ne "true" -and $list.IsCatalog -ne "true" -and  $list.BaseType -eq "DocumentLibrary")
       {          

            "{0}`t{1}`t{2}`t{3}`t{4}`t{5}`t{6}" -f $list.Title, $list.BaseType, $list.RootFolder.ServerRelativeUrl,$list.Title,"EnterSize", $list.LastItemModifiedDate, $list.ItemCount | Out-File -FilePath $outputFile -Append 
             Write-host $list.Title

            $listItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
            $ctx.Load($listItems);
            $ctx.ExecuteQuery();


            foreach($listItem in $listItems)
            {

               # $sizeMB = "{0:N2}" -f (($listitem["File_x0020_Size"])/1MB)  
                 #Here I am getting $sizeMB=0
               $length=$listItem.File.Length
                 #Here I am getting $length=0

               $sizeN=$listItem.FieldValues["File_x0020_Size"]
                #Here I am getting $sizeN=0

               #check if ListItem is Folder or File
                if($listItem["FSObjType"] -eq 1)
                {                    
                    "{0}`t{1}`t{2}`t{3}`t{4}`t{5}`t{6}" -f $listItem["FileLeafRef"],'Folder',$listItem["FileRef"],$list.Title,$sizeMB,$listItem["Modified"],$listItem["ItemChildCount"] | Out-File -FilePath $outputFile -Append
                }
                else
                {
                    "{0}`t{1}`t{2}`t{3}`t{4}`t{5}`t{6}" -f $listItem["FileLeafRef"],'ListItem',$listItem["FileRef"],$list.Title,$sizeMB,$listItem["Modified"],$listItem["ItemChildCount"] | Out-File -FilePath $outputFile -Append
                }
            }   

        }     
    }
    if($_web.Webs.Count -gt 0) 
    {
         foreach($subSite in $_web.Webs)
        { 

           $ctx.Load($subSite)
           $ctx.Load($subSite.Webs)
           $ctx.ExecuteQuery()

         $size = GetWebSize($subSite)  
         $sizeMB = "{0:N2}" -f (($size)/1MB)

           "{0}`t{1}`t{2}`t{3}`t{4}`t{5}`t{6}" -f $subSite.Title, 'Site', $subSite.ServerRelative,'',$sizeMB, $subSite.LastItemModifiedDate, $subSite.ItemCount | Out-File -FilePath $outputFile -Append 
           retrieveListItems($subSite) 
         }
     }
   $ctx.Dispose()
}
catch
{
    $_.Exception.Message
    $_ | Out-File -FilePath $logFile -Append
    exit
}

}


function GetWebSize ($web)      
{  
$ctx.Load($web.Folders) 
$ctx.ExecuteQuery() 
[long]$total = 0  
foreach ($folder in $web.Folders) {  
    $total += GetFolderSize -Folder $folder  
}          
return $total  
}  

function GetFolderSize ($folder)  
{  
$ctx.Load($folder.Files) 
$ctx.Load($folder.Folders) 
$ctx.ExecuteQuery(); 
[long]$folderSize = 0   
foreach ($file in $folder.Files) { 
    $folderSize += $file.Length;  
}  
foreach ($fd in $folder.Folders) {  
    $folderSize += GetFolderSize -Folder $fd  
}  
return $folderSize  
}  


function generateSiteDataReports
{
try
{

    $siteCollectionUrl= Read-Host -Prompt "Enter Site Collection URL"

    $Username =  Read-Host -Prompt "Enter userName: ";

    $password = Read-Host -Prompt "Enter password: " -AsSecureString ;        

    $logFile = "$PSScriptRoot\log.txt"

    if(Test-Path $logFile)
    {
        Remove-Item $logFile
    }

    $outputFile = "$PSScriptRoot\SiteReport.csv"



    # Remove existing output file
    if(Test-Path $outputFile)
    {
         Remove-Item $outputFile
    }


    "{0}`t{1}`t{2}`t{3}`t{4}`t{5}`t{6}" -f "Title", "Object Type", "URL","ListName","Size", "Modified", "ItemCount" | Out-File -FilePath $outputFile -Append
    $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($SiteCollectionUrl);
    $ctx.Credentials=New-Object System.Net.NetworkCredential($Username, $password);

    $ettkundWeb=$ctx.Web
    $ctx.Load($ettkundWeb)
    $ctx.Load($ettkundWeb.Webs)
    $ctx.ExecuteQuery()

    #Get Web Size
   # $size = GetWebSize($ettkundWeb)  
    $sizeMB = "{0:N2}" -f (($size)/1MB)  
   # $sizeGB = "{0:N2}" -f (($size)/1GB) 

    "{0}`t{1}`t{2}`t{3}`t{4}`t{5}`t{6}" -f $ettkundWeb.Title, 'Site', $ettkundWeb.ServerRelativeUrl,'',"Enter FileSize", $ettkundWeb.LastItemModifiedDate, $subSite.ItemCount | Out-File -FilePath $outputFile -Append 

    Write-host "Processing Web :"   $ettkundWeb.URL
    retrieveListItems($ettkundWeb)

}
catch
{
    $_.Exception.Message
    $_ | Out-File -FilePath $logFile -Append
    exit
}

}

generateSiteDataReports
0

Try following code:

foreach($listItem in $listItems)
         {
             if($listItem.FileSystemObjectType -eq "File")
             {
                 $file = $listItem.File
                 $fileProps = $file.ListItemAllFields
                 $context.Load($file)
                 $context.Load($fileProps)


                 $context.ExecuteQuery()    
                 $sizeN=$fItem["File_x0020_Size"]
                 #Here I am getting $sizeN=0

                 #check if ListItem is Folder or File
                 if($listItem["FSObjType"] -eq 1)
                 {                    
                     "{0}`t{1}`t{2}`t{3}`t{4}`t{5}`t{6}" -f $listItem["FileLeafRef"],'Folder',$listItem["FileRef"],$list.Title,$sizeMB,$listItem["Modified"],$listItem["ItemChildCount"] | Out-File -FilePath $outputFile -Append
                 }
                 else
                 {
                     "{0}`t{1}`t{2}`t{3}`t{4}`t{5}`t{6}" -f $listItem["FileLeafRef"],'ListItem',$listItem["FileRef"],$list.Title,$sizeMB,$listItem["Modified"],$listItem["ItemChildCount"] | Out-File -FilePath $outputFile -Append
                 }   
             }
         }

Refer following link: https://www.sharepointpals.com/post/how-to-get-the-size-of-the-document-without-version-in-sharepoint-using-powershell-script-client-side-object-model/

  • getting Exception "You cannot call a method on a null-valued expression" on ExecuteQuery. – Vishal_chorghe Oct 1 at 14:49
  • Thank you for Hitendra for link. – Vishal_chorghe Oct 2 at 6:14
0

I had to replace CAML Query. Now I am able to get All fieldValues. Following code change for reference:

$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$camlQuery.ViewXml ="<View Scope='RecursiveAll' />";
$listItems=$list.GetItems($camlQuery)           
$ctx.Load($listItems);
$ctx.ExecuteQuery();

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.