简体   繁体   English

Dynamics CRM WEB API-从实体参考中选择字段

[英]Dynamics CRM WEB API- Select field from entity reference

I am trying to select a field from another CRM entity by using an expand with my odata query. 我试图通过使用我的odata查询扩展从另一个CRM实体中选择一个字段。

Query string: studios$?Select=studioid,studioname,titleid&$Expand= 查询字符串: studios$?Select=studioid,studioname,titleid&$Expand=

Title id is a field in custom entity called title. 标题id是名为title的自定义实体中的字段。 How do I write an expand clause to expand title and select titleid? 如何编写expand子句来扩展title并选择titleid?

Any help is appreciated! 任何帮助表示赞赏!

first you can refer to this article from docs.microsoft explaining in detail the $expand feature, however for simplicity is followed by the relation name with targeted entity then the fields you want to select in parentheses 首先,你可以参考docs.microsoft的这篇文章,详细解释$ expand特性,但为了简单起见,后面跟着目标实体的关系名称,然后是你想在括号中选择的字段

for ex. 对于前 $expand={relation_name}($select=filed1,field2)

which might be something like this 这可能是这样的
$expand=new_new_entity1_new_entity2($select=new_name,createdon)

Notice: the values you get from expand relation will be actually navigation URL to actual data, so you don't expect [new_name,createdon] from new_entity2 to be retrieved, what you will get will be a url to OData those values and for that you might below helping function 1 will do it for you 注意:从扩展关系中获得的值实际上是实际数据的导航URL,因此您不希望检索来自new_entity2的[new_name,createdon],您将得到的将是OData这些值的URL和你可以在下面帮助功能1为你做这件事

Also you can use XRM Rest Builder that's an amazing solution when you install on the crm you will find it's button in solutions view. 您还可以使用XRM Rest Builder ,这是一个非常棒的解决方案,当您在crm上安装时,您会在解决方案视图中找到它的按钮。 that solution is a tool helps you to design your OData query with nice GUI so that you don't need to write and refine your OData query which is faster and easier. 该解决方案是一个工具,可以帮助您使用漂亮的GUI设计OData查询,这样您就不需要编写和优化OData查询,这样更快更容易。

Helping function 1 帮助功能1

` `

function OdataExpand(expandUrl) {
    var result = null;
    var req = new XMLHttpRequest();
    req.open("GET", expandUrl, false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);
                result = results;
            } else {
                console.log("OdataExpand Error : ");
                console.log(this);
            }
        }
    };
    req.send();
    return result;
}

` `

but if you want to get all the data with one call and maybe you want to expand to more than one entity i recommend that you use Fetch XML simply design the fetch you need using advanced find tool to extract the fetchXML then pass it to Helping Function 2 that should return the full data but be aware that max fetch length in this approach is limited to 2,048 characters [Max Length for GET Request]. 但是如果你想通过一次调用获得所有数据并且你想要扩展到多个实体我建议你使用Fetch XML,只需使用高级查找工具设计你需要的提取来提取fetchXML然后将其传递给Helping Function 2应返回完整数据,但要注意此方法中的最大提取长度限制为2,048个字符[GET请求的最大长度]。

Helping Function 2 帮助功能2
` `

function ExecuteFetchXMLQuery(dataSet, fetchQuery) {
    var encodedFetchXML = encodeURIComponent(fetchQuery);
    var result;
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/" + dataSet + "?fetchXml=" + encodedFetchXML, false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200 || (this.status >= 200 && this.status <= 206)) {
                console.log("Success Executing Fetch");
                var results = JSON.parse(this.response);
                result = results;
            }
            else {
                console.log("Error Executing Fetch");
                console.log(this);
                console.log(this.statusText);
            }
        }
    };
    req.send();
    return result;}

` `

Try this: 尝试这个:

studios?$select=studioid,studioname,titleid&$expand=titleid($select=titleid,name)

Also it's recommended to use CRM REST Builder to compose & test CRM web api queries. 此外,建议使用CRM REST Builder来编写和测试CRM web api查询。

Read more 阅读更多

Note: Check your entity & field names, they should look like new_studio & new_studioid 注意:检查您的实体和字段名称,它们应该看起来像new_studio和new_studioid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Dynamics CRM 创建帖子 Web API - Creating Post with Dynamics CRM Web API 通过 API 在 Microsoft Dynamics CRM 中为联系人实体创建注释 - Create annotation to a contact entity in Microsoft Dynamics CRM by API 计算 Dynamics CRM Online web api (ODATA) 中的所有行 - Counting ALL rows in Dynamics CRM Online web api (ODATA) Dynamics CRM Online 和 Web API:通过 POST 的方法隧道 - Dynamics CRM Online and Web API : Method Tunneling through POST 在 Dynamics 365 XRM Web API 中创建记录时无法设置多选选项字段 - Cannot set multi-select option field when creating record in Dynamics 365 XRM Web API 将 MSCRM.SuppressDuplicateDetection 标头添加到 CRM Dynamics Web API 请求会提供 HTTP 412 代码 - Adding MSCRM.SuppressDuplicateDetection header to CRM Dynamics Web API request gives HTTP 412 code Dynamics CRM 365 Web Api使用哪个C#OData客户端库? - Which C# OData Client library to use for Dynamics CRM 365 Web Api? 将API与Dynamics CRM集成的最佳实践/方法 - Best Practices / Approaches for integrating API with Dynamics CRM 以 json 格式从 Dynamics CRM 365 获取数据 - Getting data from Dynamics CRM 365 in json 是否可以在Dynamics中使用Web API原子地创建两个实体,然后创建第三个链接实体? - Can I create two entities and then a third linking entity atomically using Web API in Dynamics?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM