SharePoint CSOM KeywordQuery - 字段或属性“QueryText”不存在

问题描述 投票:0回答:1

我正在尝试搜索 SharePoint 中保存的文件和文件夹以查找用户输入的短语,但始终出现以下异常:

Microsoft.SharePoint.Client.ServerException
  HResult=0x80131500
  Message=Field or property "QueryText" does not exist.
  Source=Microsoft.SharePoint.Client.Runtime
  StackTrace:
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
   at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
   at Microsoft.SharePoint.Client.ClientRequest.<ExecuteQueryToServerAsync>d__53.MoveNext()
   at Microsoft.SharePoint.Client.ClientRequest.<ExecuteQueryAsync>d__39.MoveNext()
   at Microsoft.SharePoint.Client.ClientRuntimeContext.<ExecuteQueryAsync>d__57.MoveNext()
   at Microsoft.SharePoint.Client.ClientContext.<ExecuteQueryAsync>d__23.MoveNext()
   at SharePointAccessTest.Program.<Main>d__0.MoveNext() in C:\Repos\POC\SharePointAccessTest\Program.cs:line 78

我正在使用 PnP.Framework 的最新版本 (1.11.0),它使用 SharePoint 客户端对象模型 (CSOM)。

我很想使用更新的 PnP.Core 包甚至 Microsoft Graph 包,但我的客户设置 SharePoint 的方式他们不愿意为应用程序提供必要的图形权限!

这是我使用 Fiddler Classic 设法捕获的发送到 SharePoint 的请求:

POST https://**redacted**.sharepoint.com/sites/Site/SubSite/_vti_bin/client.svc/ProcessQuery HTTP/1.1
Host: **redacted**.sharepoint.com
Authorization: Bearer ***redacted***
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Content-Type: text/xml
Content-Length: 1112

<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName=".NET Library" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="2" ObjectPathId="1" /><ObjectPath Id="4" ObjectPathId="3" /><ObjectPath Id="6" ObjectPathId="5" /><ObjectPath Id="8" ObjectPathId="7" /><ObjectIdentityQuery Id="9" ObjectPathId="7" /><SetProperty Id="10" ObjectPathId="7" Name="QueryText"><Parameter Type="String">test</Parameter></SetProperty><ObjectPath Id="12" ObjectPathId="11" /><Method Name="ExecuteQuery" Id="13" ObjectPathId="11"><Parameters><Parameter ObjectPathId="7" /></Parameters></Method></Actions><ObjectPaths><StaticProperty Id="1" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" /><Property Id="3" ParentId="1" Name="Web" /><Property Id="5" ParentId="3" Name="Lists" /><Method Id="7" ParentId="5" Name="GetByTitle"><Parameters><Parameter Type="String">Published Documents</Parameter></Parameters></Method><Constructor Id="11" TypeId="{8d2ac302-db2f-46fe-9015-872b35f15098}" /></ObjectPaths></Request>

这是格式化的 XML:

<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0"
    ApplicationName=".NET Library" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">
    <Actions>
        <ObjectPath Id="2" ObjectPathId="1" />
        <ObjectPath Id="4" ObjectPathId="3" />
        <ObjectPath Id="6" ObjectPathId="5" />
        <ObjectPath Id="8" ObjectPathId="7" />
        <ObjectIdentityQuery Id="9" ObjectPathId="7" />
        <SetProperty Id="10" ObjectPathId="7" Name="QueryText">
            <Parameter Type="String">test</Parameter>
        </SetProperty>
        <ObjectPath Id="12" ObjectPathId="11" />
        <Method Name="ExecuteQuery" Id="13" ObjectPathId="11">
            <Parameters>
                <Parameter ObjectPathId="7" />
            </Parameters>
        </Method>
    </Actions>
    <ObjectPaths>
        <StaticProperty Id="1" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" />
        <Property Id="3" ParentId="1" Name="Web" />
        <Property Id="5" ParentId="3" Name="Lists" />
        <Method Id="7" ParentId="5" Name="GetByTitle">
            <Parameters>
                <Parameter Type="String">Published Documents</Parameter>
            </Parameters>
        </Method>
        <Constructor Id="11" TypeId="{8d2ac302-db2f-46fe-9015-872b35f15098}" />
    </ObjectPaths>
</Request>

以及格式化的响应:

[
  {
    "SchemaVersion": "15.0.0.0",
    "LibraryVersion": "16.0.23501.12002",
    "ErrorInfo": {
      "ErrorMessage": "Field or property \"QueryText\" does not exist.",
      "ErrorValue": null,
      "TraceCorrelationId": "8f13a0a0-70f0-6000-48fe-78b17a865d0d",
      "ErrorCode": -1,
      "ErrorTypeName": "Microsoft.SharePoint.Client.InvalidClientQueryException"
    },
    "TraceCorrelationId": "8f13a0a0-70f0-6000-48fe-78b17a865d0d"
  }
]

最后是我试图让它在 .NET 6 控制台应用程序中使用的代码。该应用程序使用客户端凭据(客户端 ID 和密码)进行身份验证:

var siteUrl = $"https://{tenantName}.sharepoint.com/sites/Site/SubSite";
using var clientContext = new AuthenticationManager()
    .GetACSAppOnlyContext(siteUrl, clientId, clientSecret))

var documents = clientContext.Web.Lists.GetByTitle("Published Documents");
var query = new KeywordQuery(clientContext, documents.Path)
{
    QueryText = "test",
};

var searchResults = new SearchExecutor(clientContext).ExecuteQuery(query);

await clientContext.ExecuteQueryAsync();

我尝试使用最新的预发布版本(1.11.129-nightly)和其他低至 1.5.0 的发布版本,但没有成功。

我已经设法让这个工作的其他方面,可以成功地导航文件夹和文件,并检索文件内容,但我真的坚持这个搜索功能。

我是 SharePoint 开发的新手,可能错过了一些对其他人来说非常明显的东西,但我找不到与此错误相关的任何内容!

c# sharepoint-online csom sharepoint-search keywordquery
1个回答
0
投票

我想我明白问题出在哪里了。问题似乎是我试图通过传递集合的对象路径来查询特定的网站集:

var siteUrl = $"https://{tenantName}.sharepoint.com/sites/Site/SubSite";
using var clientContext = new AuthenticationManager()
    .GetACSAppOnlyContext(siteUrl, clientId, clientSecret))

var documents = clientContext.Web.Lists.GetByTitle("Published Documents");
var query = new KeywordQuery(clientContext, documents.Path);

siteUrl
keywordQuery
中删除这些似乎可以阻止错误。

var siteUrl = $"https://{tenantName}.sharepoint.com/";
using var clientContext = new AuthenticationManager()
    .GetACSAppOnlyContext(siteUrl, clientId, clientSecret))

var query = new KeywordQuery(clientContext);

虽然现在这将搜索租户中的所有站点和列表。通过添加路径过滤器,它似乎将结果限制在正确的列表中。

var query = new KeywordQuery(clientContext)
{
    QueryText = $@"test path:""https://{tenantName}.sharepoint.com/sites/Site/Subsite/Published Documents""",
};

警告: 不幸的是,我现在遇到权限错误,但这是我正在与客户一起解决的另一个问题。

© www.soinside.com 2019 - 2024. All rights reserved.