【问题标题】:Getting a FieldLookupValue from Sharepoint in Powershell从 Powershell 中的 Sharepoint 获取 FieldLookupValue
【发布时间】:2018-04-06 18:21:05
【问题描述】:

在网上看,我想出了这 2 个函数来使用这 3 个带有 powershell 的 DLL 请求 Sharepoint:

Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Taxonomy.dll
Microsoft.SharePoint.Client.Runtime.dll

此函数用于从列表中获取所有项目

Function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
    $list = $Context.Web.Lists.GetByTitle($listTitle)
    $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    $items = $list.GetItems($qry)
    $Context.Load($items)
    $Context.ExecuteQuery()
    return $items
}

而这个工作清单:

Function getChangeListsFromSharepoint(){
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
    $items = Get-ListItems -Context $context -ListTitle $listName 
    foreach($item in $items)
    {
        /** Working HERE **/
    }
    $context.Dispose()
}

现在,当我使用 Write-Host $item.Fields 显示所有项目内容时,我得到了这样的结果:

[Title,"blabla"]
...
[SpecialField,Microsoft.SharePoint.Client.FieldLookupValue[]]
[OtherField,Microsoft.SharePoint.Client.FieldLookupValue]

我正在尝试获取 SpecialField 和 OtherField 的值。为此,我使用$item["SpecialField"].LookupValue。而且我没有问题。但是当我在OtherField 上执行此操作时,该值是空的。如果我尝试使用$item["OtherField"].LookupID,则该值不为空,并且我有一个 ID。我怎样才能得到这个 ID 背后的价值? FieldLookupValue[]FieldLookupValue 有什么区别?

【问题讨论】:

  • AFAIK,有延迟加载的属性。也许您必须使用它的 ID 来请求自己的价值?
  • 不确定它是否属于 SharePoint Stack,因为它是用 PowerShell 编写的。我如何使用 ID 请求他们?仅供参考,ID 是他所在列表中项目的位置。 OtherField 是 13 个元素的列表,因此 ID 介于 1 和 13 之间。
  • PowerShell 是一种众所周知的 SharePoint 管理工具,而不仅仅是一种编程语言 ;) 问问自己:如果用任何其他语言编写,它会有什么不同吗?但是,是的,它与 SO 并非完全无关;)我不知道。如果我愿意,我已经发布了答案
  • 我想我可能已经发现了问题:FieldChoice 是由同一站点上的另一个列表组成的。并且我正在使用的用户不允许访问此列表。这就是为什么我无法请求价值但我仍然获得 ID 的原因。我正在申请权利,然后我会发布解决方案。

标签: powershell sharepoint csom


【解决方案1】:

FieldLookupValue[] 是一个多值查找列。

它的属性Allow multiple values 设置为true,并包含一个项目数组。

所以,$item["OtherField"].LookupValue 在这里不起作用,您需要按如下方式对其进行迭代:

#multi-value lookup
$mvLookup = [Microsoft.SharePoint.Client.FieldLookupValue[]] $item["OtherField"]        

$mvLookup |% { "Lookup Value: $($_.LookupId):$($_.LookupValue)" }

结果如下:

FieldLookupValue 是单值查找列。它的 Allow multiple values 设置为 false 并包含一个项目。所以,$item["SpecialField"].LookupValue 将在这里工作

【讨论】:

  • 你的方法不对:Microsoft.SharePoint.Client.FieldLookupValue[].LookupValue => 工作。 Microsoft.SharePoint.Client.FieldLookupValue.LookupValue => 不工作
  • 这很奇怪,我测试了它工作的代码并为我提供了价值。我在列表中只有一个项目,它具有多值类型的列。所以,我就是这样找回的。
  • 我只是在列表中获取 ID。 (就像我有 4 个选择,所以 ID 将介于 1 和 4 之间)。我可以在 powershell 中对列表和值进行硬编码,但我希望更加动态。也许我可以查询这个特定列表并使用 ID 获取值,但我不知道如何。
  • 供参考:sharepoint 上的 fieldType 是一个 SPFieldChoice。如果有帮助的话。
  • 我想我可能已经发现了问题:FieldChoice 是由同一站点上的另一个列表组成的。并且我正在使用的用户不允许访问此列表。这就是为什么我无法请求价值但我仍然得到 ID。
【解决方案2】:

所以解决方案很简单:我的用户无权探索(字段)列表,因为它来自(共享点)列表,他没有任何权利。奇怪的是,在网站上该值是可见的,但在使用 powershell 时却不是。

【讨论】:

    猜你喜欢
    • 2015-01-15
    • 2017-11-01
    • 2021-11-01
    • 2020-09-17
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    相关资源
    最近更新 更多