ChiliYago
ChiliYago

Reputation: 12289

SharePoint CSOM PowerShell Does Not Return Web.Folders property

The goal here is to access the Web.Folders property starting with this PowerShell code.

Connect-SPOnline –Url https://mysite.sharepoint.com;
$web = Get-SPOWeb;
$web | Get-Member | sort name

From that list members I can see a "Folders" Property which has the following

Definition:
----------                                                                                                                                                  
Microsoft.SharePoint.Client.FolderCollection, 
Microsoft.SharePoint.Client, 
Version=16.1.0.0, 
Culture=neutral, 
PublicKeyToken=71e9bce111e9429c Folders {get;}

As such should'nt I be able to retreive the Folders Propery using the following snippet?

$ctx = Get-SPOContext;
$folders = $web.Folders;
$ctx.Load($folders);
$ctx.ExecuteQuery();
$folders;

$folders only returns the following uninitilized message.

format-default : The collection has not been initialized. It has not been requested or the request has not been
executed. It may need to be explicitly requested.
    + CategoryInfo          : NotSpecified: (:) [format-default], CollectionNotInitializedException
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException,Microsoft.PowerShell.Comma
   nds.FormatDefaultCommand

Upvotes: 2

Views: 1161

Answers (2)

B.C.
B.C.

Reputation: 83

The first answer gets it. Here is some more elaboration on why that is.

You have accessed the web.Folder property correctly. However, as the previous answer points out, the line:

$folders

is the problem. This is because Powershell works by printing out EVERY SINGLE property of an item when you enter that item as a command. (Powershell either uses reflection to go through EVERY SINGLE PROPERTY of an object, or something similar).

In the case of $folders, some of the properties -- such as format-default (as indicated by the error message) are not initialized. So when Powershell tries to print out every single property, it fails.

So, once you have initialized the $folders collection, the next step is to read a specific item and initialize properties on that item.

Understanding what Powershell is doing is key here.

Upvotes: 2

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59338

Don't let this error message to confuse you. Even though this error usually occurs when the client object (folder collection) has not been requested from the server, in this particular case it occurs due to the line:

$folders

So, using the specified snippet folders collection is getting retrieved properly from the server:

$web = $ctx.Web
$folders = $web.Folders
$ctx.Load($folders)
$ctx.ExecuteQuery()

In order to determine whether client object has been requested from the server or not utilize ClientObject.IsObjectPropertyInstantiated method, for example:

if ($web.IsObjectPropertyInstantiated('Folders') -eq $true)
{
   Write-Host "Folder collection has been loaded"
   #... 

}

The following examples demonstrate how to access a folder in collection and its properties.

Example 1. How to iterate folder collection and print its name:

$folders.GetEnumerator() | % { 
    Write-Host "Folder name: $($_.Name)" 
}

Example 2. How to access folder by index and print its name:

if($folders.Count -gt 0){
    $folder = $folders[0] #get first item
    Write-Host "Folder name: $($folder.Name)" 
} 

Upvotes: 2

Related Questions