There is a good solution available at Github and SO thread about how to use C# lambda expressions equivalent in PowerShell.
As per the example is given, I managed to form the simple working lambda equivalent for my C# snippet as below:
ctx.Load(web, s => s.Id);
ctx.ExecuteQuery();
// 'ctx' is Microsoft.SharePoint.Client.ClientContext
// 'web' is ctx.Web -> Microsoft.SharePoint.Client.Web
When using in PowerShell → this is working fine.
Load-CSOMProperties -Object $clientContext.web -PropertyNames @("Id")
$clientContext.ExecuteQuery()
However, I am unable to convert the nested lambda expression, I tried some variations, but unable to figure it out what I am missing:
ctx.Load(web.Webs, w => w.Include(s => s.ServerRelativeUrl));
ctx.ExecuteQuery();
// web.Webs is WebCollection; s is Microsoft.SharePoint.Client.Web
When using in PowerShell
Load-CSOMProperties -ParentObject $clientContext.web -CollectionObject $clientContext.web.Webs -PropertyNames @("ServerRelativeUrl") -ParentPropertyName "Include"
$clientContext.ExecuteQuery()
I am getting the below exception:
An error occurred while enumerating through a collection: 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.. At D:\Test.ps1:122 char:17 + if ($collectionObject -ne $null) { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Microsoft.Share...int.Client.Web]:d__0) [], RuntimeException + FullyQualifiedErrorId : BadEnumeration Exception calling "Property" with "2" argument(s): "Instance property 'Include' is not defined for type 'Microsoft.SharePoint.Client.Web'" At D:\Test.ps1:137 char:13 + $collectionProperty = [System.Linq.Expressions.Expression ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentException Exception calling "Call" with "3" argument(s): "Value cannot be null. Parameter name: arguments" At D:\Test.ps1:144 char:13 + $callMethod = [System.Linq.Expressions.Expression]::Call( ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentNullException Exception calling "Invoke" with "2" argument(s): "Value cannot be null. Parameter name: body" At D:\Test.ps1:146 char:13 + $expression2 = $lambdaMethodGeneric2.Invoke($null, @($cal ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ArgumentNullException
My next few lines in PowerShell are:
foreach ($subsite in $clientContext.web.Webs) {
Write-Host "Subsite :" $subsite.ServerRelativeUrl
}
I think the nested lambdas can be broken into two ExecuteQuery()
, but not sure how.