有一个很好的解决方案 at Github 和 SO thread ,关于如何在PowerShell中使用等效的C#lambda表达式。
根据示例,我设法为我的C#片段创建了简单的工作lambda等效,如下所示:
ctx.Load(web, s => s.Id);
ctx.ExecuteQuery();
// 'ctx' is Microsoft.SharePoint.Client.ClientContext
// 'web' is ctx.Web -> Microsoft.SharePoint.Client.Web
在PowerShell中使用时→此功能正常。
Load-CSOMProperties -Object $clientContext.web -PropertyNames @("Id")
$clientContext.ExecuteQuery()
但是,我无法转换嵌套的lambda表达式,我尝试了一些变体,但无法弄清楚我错过了什么:
ctx.Load(web.Webs, w => w.Include(s => s.ServerRelativeUrl));
ctx.ExecuteQuery();
// web.Webs is WebCollection; s is Microsoft.SharePoint.Client.Web
在PowerShell中使用时
Load-CSOMProperties -ParentObject $clientContext.web -CollectionObject $clientContext.web.Webs -PropertyNames @("ServerRelativeUrl") -ParentPropertyName "Include"
$clientContext.ExecuteQuery()
我收到以下异常:
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
PowerShell的下几行是:
foreach ($subsite in $clientContext.web.Webs) {
Write-Host "Subsite :" $subsite.ServerRelativeUrl
}
我认为嵌套的lambdas可以分为两个ExecuteQuery()
,但不确定如何。