【问题标题】:Breeze - Get SharePoint AttachmentFiles from List using Expand?Breeze - 使用展开从列表中获取 SharePoint 附件文件?
【发布时间】:2015-06-17 02:55:39
【问题描述】:

我将 Breeze 与支持附件的 SharePoint Online 列表一起使用。 Breeze 查询正确调用了 expand ,我可以通过 Browser 和 Fiddler 看到 JSON 与附件数据一起返回。但是,在 Breeze JavaScript 中创建的实体不包含 AttachmentFiles 的数据。它始终为空。这是 SP 适配器不支持的东西,还是我错误地定义了 AttachmentFiles 和 List 实体。我已经尝试了多种方法来定义关系,使用复杂类型和导航,使用自动生成 ID 和不使用自动生成 ID,但我遇到了困难。

Rest 请求如下所示: /_api/web/lists/getbytitle('Contacts')/items?$expand=Author/Id,AttachmentFiles&$select=Id,AuthorId,FirstName,Title,Email,Author,AttachmentFiles ,作者/用户名

附件是返回的 JSON 视图,以及映射的当前 Breeze 实体。

来自实体的片段

    //attachments
addType({
  name: 'AttachmentFiles',
  dataProperties: {
    ServerRelativeUrl: {
      nullable: false
    },
    FileName: {
      nullable: false
    }

  }
});

// create entity for contacts
addType({
  name: 'Contacts',
  defaultResourceName: 'getbytitle(\'Contacts\')/items',
  dataProperties: {
    Id: {
      type: breeze.DataType.Int32
    },
    AuthorId: {
      type: breeze.DataType.Int32
    },
    FirstName: {
      nullable: false
    },
    Title: {
      nullable: false
    }, // this is actually the last name field in the list
    Email: {
      nullable: false,
      validators: [breeze.Validator.emailAddress()]
    },
    Author: {
      complexTypeName: "Author"
    }
  },
  // Let model.validation add the requireReferenceEntity validators
  navigationProperties: {
    AttachmentFiles: {
      type: 'AttachmentFiles',
      hasMany: true
    }
  }
});

查看下方的 JSON 响应

{
"d": {
    "results": [
        {
            "__metadata": {
                "id": "388762a5-a565-4033-af90-5a2ea4cb1894",
                "uri": "https://foo-616175366e015f.sharepoint.com/sites/DevSIte/BreezeSP2013Sample/_api/Web/Lists(guid'df211835-f6df-4bb0-b7f0-0995c6ff8562')/Items(1)",
                "etag": "\"3\"",
                "type": "SP.Data.ContactsListItem"
            },
            "AttachmentFiles": {
                "results": [
                    {
                        "__metadata": {
                            "id": "https://foo-616175366e015f.sharepoint.com/sites/DevSIte/BreezeSP2013Sample/_api/Web/Lists(guid'df211835-f6df-4bb0-b7f0-0995c6ff8562')/Items(1)/AttachmentFiles('8328787dc792224f625c9645a45d01aa.jpg')",
                            "uri": "https://foo-616175366e015f.sharepoint.com/sites/DevSIte/BreezeSP2013Sample/_api/Web/Lists(guid'df211835-f6df-4bb0-b7f0-0995c6ff8562')/Items(1)/AttachmentFiles('8328787dc792224f625c9645a45d01aa.jpg')",
                            "type": "SP.Attachment"
                        },
                        "FileName": "8328787dc792224f625c9645a45d01aa.jpg",
                        "ServerRelativeUrl": "/sites/DevSIte/BreezeSP2013Sample/Lists/Contacts/Attachments/1/8328787dc792224f625c9645a45d01aa.jpg"
                    },
                    {
                        "__metadata": {
                            "id": "https://foo-616175366e015f.sharepoint.com/sites/DevSIte/BreezeSP2013Sample/_api/Web/Lists(guid'df211835-f6df-4bb0-b7f0-0995c6ff8562')/Items(1)/AttachmentFiles('Screen Shot 2015-04-23 at 10.28.35 AM.png')",
                            "uri": "https://foo-616175366e015f.sharepoint.com/sites/DevSIte/BreezeSP2013Sample/_api/Web/Lists(guid'df211835-f6df-4bb0-b7f0-0995c6ff8562')/Items(1)/AttachmentFiles('Screen%20Shot%202015-04-23%20at%2010.28.35%20AM.png')",
                            "type": "SP.Attachment"
                        },
                        "FileName": "Screen Shot 2015-04-23 at 10.28.35 AM.png",
                        "ServerRelativeUrl": "/sites/DevSIte/BreezeSP2013Sample/Lists/Contacts/Attachments/1/Screen Shot 2015-04-23 at 10.28.35 AM.png"
                    }
                ]
            },
            "Author": {
                "__metadata": {
                    "id": "1fdc1547-d439-42bd-b3aa-b41e92c8ec6f",
                    "type": "SP.Data.UserInfoItem"
                },
                "UserName": "user@contoso.com"
            },
            "Id": 1,
            "ID": 1,
            "Title": "Alanso",
            "AuthorId": 1073741823,
            "FirstName": "Fernando",
            "Email": "fernando.alonso@ferrari.it"
        }
    ]
}

}

【问题讨论】:

    标签: sharepoint breeze


    【解决方案1】:

    我确实看到了这里的问题......你可以治愈。

    首先,我想提请您注意“Debugging query results”文档,其中有许多提示可以帮助您诊断正在发生的事情。并非所有这些都适用,但它们是其中的宝贵线索。

    我很好奇您的EntityManager 是否有任何类型为“AttachmentFiles”的实体。我比较怀疑。你可以检查一下。

    不管怎样,我突然想到了两个观察结果:

    1. AttachmentFiles 类型没有键属性,该属性唯一标识此类型的实例。每个EntityType 必须至少有一个键属性。 MetadataHelper 可以按照约定检测一些键,但在您的 AttachmentFiles 中没有明显的候选者。如果有(但我没有看到),您必须指定它。

    我假设您使用 Breeze Labs breeze.metadata-helper.js 中定义的 MetadataHelper 来编写元数据,而不是为此目的使用原始 Breeze API。看起来你正在这样做......这是一件好事。

    1. 您的Contacts 类型标识了到AttachmentFiles 的一对多导航,但没有在AttachmentFiles 中指定将AttachmentFiles 的实例链接回Contacts 的外键关系。因此,即使 Breeze 从这个 JSON 有效负载创建了 AttachmentFiles 实例,它也无法将它们与 Contacts 的任何实例相关联

    我在数据中没有看到任何支持AttachmentFiles 实际上是实体的概念。它们似乎嵌入在 Contact 类型中。

    有一种方法可以表示这一点。但在我走这条路之前,我想要一些确认。在我们提出后续步骤之前,也许您可​​以澄清我在这里提出的一些观点。

    【讨论】:

    • 感谢您的回复。 AttachmenFiles 的 JSON 响应不包含 ID 列。它有 FileName 和 ServerRelativeURL。我尝试添加一个 autogen 列,但这似乎没有任何作用。我还尝试将 FK 从 Contacts 实体添加到 AttachmentFiles 实体,这似乎也没有做任何事情。也许我的语法不正确,但我不这么认为。我正在使用 Breeze 的 SharePoint 适配器。调试时,我在 Contacts 对象上有那个 AttachmenFiles 对象(属性),但它为空。我将通过您的调试链接进行工作。感谢分享。
    • AttachmentFiles 在 SharePoint 中是否有对 Contacts 的 FK?如果是这样,我们只需要暴露它并从那里进行调整。如果没有,那么我们可以做一些柔术,在联系人数据到达时将 AF 数据添加到联系人数据中,或者使用更多的 Breeze 魔法变得疯狂。我有窍门。
    • 不,它没有 FK。它只是结果集中的一个子项。此时,我想我需要更改我的应用程序的逻辑,所以我没有将 AttachmentFiles 作为 Contact 项目的一部分,而是单独调用 AttachmentFiles Rest 查询
    • 你确定吗? OData expand 似乎暗示通过某种 FK 相关的实体,即使您如何公开它并不明显。你怎么能在没有 FK 的情况下“单独调用 AF Rest 查询”。我希望我了解更多 SharePoint。也许我们需要 Andrew Connell。如果它真的是一个嵌套的“子项”,你打算更新它吗?如果没有,我有一个快速而肮脏的解决方案给你(我不想经常使用)。
    • 确实 AttachmenFiles 中没有 FK。您可以通过查询联系人项目 ID 来获取项目的所有 AttachmentFile,但我永远无法在 AttachmenFiles 响应中看到项目 ID。例如_api/web/lists/getbytitle('Classifieds')/items('1')/AttachmentFiles 将获取 id 为 1 的联系人项目的所有附件。我不打算更新数据,只是阅读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多