如何在 Umbraco 7 后台获取项目的文档字段值

How do I get document field values for an item in Umbraco 7 backoffice

我已经创建了一个自定义的 Umbraco 7 仪表板。在其中,我想从我的 Umbraco CMS 中已知类型文档类型的实例中获取特定字段详细信息。

我已经使用 entityResource.getChildren(1386, "Document") 成功获得了特定类型的所有文档的列表,随后对每个此类文档调用 entityResource.getById(item.id, "Document")。我可以看到每个文档实例的许多详细信息,但看不到编辑器在创建它时输入的数据。我如何使用 Umbraco 的 Angular services/API 获取此信息?

仪表板代码示例:

// get community alerts
entityResource.getChildren(1386, "Document")
    .then(function (ent)
    {
        angular.forEach(ent, function (item)
        {
            let communityalert = {
                umbracoItem: item,
                title: '',     // I want data from each document to put here
                topic: ''
            };
            entityResource
                .getById(item.id, "Document")
                .then(function (entity)
                {
                    console.log('entity', entity);
                    communityalert.title = entity.name;
                    communityalert.umbracoItem = entity;

                    entityResource
                        .getUrl(item.id, "Document")
                        .then(function (url)
                        {
                            communityalert.url = url;

                            // finally push collected data to VM.
                            vm.CommunityAlerts.push(communityalert);
                        });
                });
        });
    });

好的,经过一番摸索,我找到了答案; 使用可注入的 contentResource 的 getById() 方法。这会获取内容项的 all 详细信息,相比之下,entityResource 的 getById() 只获取内容树的公共详细信息,后者的资源方法会获取所有内容项的详细信息;包括输入的字段数据。

contentResource.getById(item.id)
    .then((data) =>
    {
        //console.log('contentResource.getById data', data);
        angular.forEach(data.properties, (property) =>
        {
            //do something with the document's content...
        });
    });

因此总结使用可注入资源 entityResource 中的方法来获取详细信息的子集,但要获取文档的所有详细信息,请使用可注入 contentResource 资源。

当您阅读足够多的文档时很简单!