다음을 통해 공유


FetchXML을 사용하여 쿼리 구성

 

게시 날짜: 2017년 1월

적용 대상: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

Microsoft Dynamics 365 및 Microsoft Dynamics 365(온라인)에서 FetchXML 쿼리를 실행하려면 XML 쿼리 문자열을 먼저 작성해야 합니다. 쿼리 문자열을 만든 후 IOrganizationService.RetrieveMultiple 메서드를 사용하여 쿼리 문자열을 실행합니다. 로그온한 사용자의 권한은 반환되는 레코드 집합에 영향을 줍니다. 로그온한 사용자에게 읽기 액세스 권한이 있는 레코드만 반환됩니다.

FetchXML 쿼리 문자열은 FetchXML 언어에 대한 스키마 정의를 준수해야 합니다. 자세한 내용은 FetchXML schema을 참조하십시오.

샘플: 저장된 쿼리 유효성 검사 및 실행에서 설명된 것처럼 SavedQuery 레코드를 만들어 쿼리를 저장할 수 있습니다.link-entity 노드에서 visiblefalse로 설정하여 상세하기 찾기 사용자 인터페이스에서 연결된 엔터티를 숨깁니다. 해당 엔터티는 계속 쿼리 실행에 참여하고 적절한 결과를 반환합니다.

경고

성능에 나쁜 영향을 주므로 쿼리에서 모든 특성을 검색하지는 마십시오. 쿼리가 업데이트 요청에 대한 매개 변수로 사용되는 경우 특히 그렇습니다. 업데이트에서 모든 특성이 이 집합에 포함되어 있을 경우 모든 필드 값은 변경되지 않더라도 하위 레코드에 대한 연속 변경 업데이트를 종종 트리거합니다.

쿼리 문자열 만들기

다음 예제에서 FetchXML 문은 모든 거래처를 검색합니다.

<fetch mapping='logical'> 
   <entity name='account'>
      <attribute name='accountid'/> 
      <attribute name='name'/> 
</entity>
</fetch>

다음 예제에서 FetchXML 문은 담당 사용자의 성이 Cannon이 아닌 모든 거래처를 검색합니다.

<fetch mapping='logical'>
   <entity name='account'> 
      <attribute name='accountid'/> 
      <attribute name='name'/> 
      <link-entity name='systemuser' to='owninguser'> 
         <filter type='and'> 
            <condition attribute='lastname' operator='ne' value='Cannon' /> 
          </filter> 
      </link-entity> 
   </entity> 
</fetch>  

다음 예제에서 FetchXML 문은 쿼리에서 반환되는 최대 레코드 수를 설정하기 위해 개수를 사용합니다. 이 서비스 케이스에서 첫 번째 3개 거래처가 쿼리에서 반환됩니다.

<fetch mapping='logical' count='3'>
  <entity name='account'>
   <attribute name='name' alias='name'/>
  </entity></fetch>

이 예제는 EntityMapID가 일치하는 EntityMap 및 AttributeMap 간의 내부 조인을 보여줍니다.

<fetch version='1.0' mapping='logical' distinct='false'>
   <entity name='entitymap'>
      <attribute name='sourceentityname'/>
      <attribute name='targetentityname'/>
      <link-entity name='attributemap' alias='attributemap' to='entitymapid' from='entitymapid' link-type='inner'>
         <attribute name='sourceattributename'/>
         <attribute name='targetattributename'/>
      </link-entity>
   </entity>
 </fetch>

디자인 실행

다음 코드는 FetchXML 쿼리를 실행하는 방법을 보여 줍니다.

// Retrieve all accounts owned by the user with read access rights to the accounts and 
// where the last name of the user is not Cannon. 
string fetch2 = @"
   <fetch mapping='logical'>
     <entity name='account'> 
        <attribute name='accountid'/> 
        <attribute name='name'/> 
        <link-entity name='systemuser' to='owninguser'> 
           <filter type='and'> 
              <condition attribute='lastname' operator='ne' value='Cannon' /> 
           </filter> 
        </link-entity> 
     </entity> 
   </fetch> "; 

EntityCollection result = _serviceProxy.RetrieveMultiple(new FetchExpression(fetch2));foreach (var c in result.Entities)   {   System.Console.WriteLine(c.Attributes["name"]);   }

쿼리 결과

RetrieveMultiple 메서드를 사용하여 FetchXML 쿼리를 실행하면 반환 값은 쿼리 결과가 포함된 EntityCollection입니다. 그런 다음 엔터티 컬렉션을 반복할 수 있습니다. 앞의 예제에서 foreach 루프를 사용하여 FetchXML 쿼리의 결과 모음을 반복합니다.

참고 항목

FetchXML을 사용하여 쿼리 작성
FetchXML 집계 사용
FetchXML schema

Microsoft Dynamics 365

© 2017 Microsoft. All rights reserved. 저작권 정보