2012-12-06 3 views
0

CRM 2011 용 실버 라이트 앱을 구축 중이며 CRM 시스템에서 데이터를 검색하는 가장 좋은 방법은 무엇인지 궁금합니다.Silverlight에서 CRM 데이터 쿼리

저는 조직에서 서비스 참조로 연결되어 있으며 액세스 할 수 있습니다. 나는 데이터를 검색하는 몇 가지 다른 방법을 보았지만 모두 다소 복잡해 보입니다. XMl 가져 오기 또는 간단한 Service.Retrieve 메소드와 같은 플러그인에서 사용할 수있는 것과 비슷한 것이 있습니까?

감사

난 당신이 Silvercrmsoap를 사용하는 것이 좋습니다 것입니다
+0

서비스를 사용하려고하면. 호출 검색, 어떻게됩니까? 오류가 있습니까? – Daryl

+0

Silverlight 앱이 WebResource로 배포됩니까? – Nicknow

+0

실제로 service.retrieve를 넣으려고하지는 않았지만 참조가 필요하거나 코드가 플러그인과 동일한 지 확실하지 않았습니다. 예, 리본 버튼에서 팝업으로 표시하려고합니다. – jimminybob

답변

0

, 그것은 아주 사용하기 쉽습니다. 나는 이것을 실버 프로젝트에서 사용했다.

1

프로젝트에 서비스 참조를 추가하면 LINQ를 사용하여 데이터 집합을 쿼리 할 수 ​​있습니다. 사용자 지정 영역 아래의 개발자 리소스에서 CSDL을 다운로드 할 수 있습니다.

private FelineSoftContext context; 
private System.String serverUrl; 
private DataServiceCollection<SalesOrder> _orders; 

public MainPage() 
    { 
     InitializeComponent(); 
     serverUrl = (String)GetContext().Invoke("getServerUrl"); 
     //Remove the trailing forward slash returned by CRM Online 
     //So that it is always consistent with CRM On Premises 
     if (serverUrl.EndsWith("/")) 
      serverUrl = serverUrl.Substring(0, serverUrl.Length - 1); 

     Uri ODataUri = new Uri(serverUrl + "/xrmservices/2011/organizationdata.svc/", UriKind.Absolute); 
     context = new FelineSoftContext(ODataUri) { IgnoreMissingProperties = true }; 

     var orders = from ord in context.SalesOrderSet 
        orderby ord.Name 
        select new SalesOrder 
        { 
         Name = ord.Name, 
         SalesOrderId = ord.SalesOrderId 
        }; 

     _orders = new DataServiceCollection<SalesOrder>(); 
     _orders.LoadCompleted += _orders_LoadCompleted; 
     _orders.LoadAsync(orders); 
    } 

    void _orders_LoadCompleted(object sender, LoadCompletedEventArgs e) 
    { 
     if (e.Error == null) 
     { 
      if (_orders.Continuation != null) 
      { 
       _orders.LoadNextPartialSetAsync(); 
      } 
      else 
      { 
       OrderLookup.ItemsSource = _orders; 
       OrderLookup.DisplayMemberPath = "Name"; 
       OrderLookup.SelectedValuePath = "Id"; 
      } 
     } 
    } 

는 또한 다음과 같이 클래스에 메소드를 추가해야합니다

private static ScriptObject GetContext() 
    { 
     ScriptObject xrmProperty = (ScriptObject)HtmlPage.Window.GetProperty("Xrm"); 
     if (null == xrmProperty) 
     { 
      //It may be that the global context should be used 
      try 
      { 

       ScriptObject globalContext = (ScriptObject)HtmlPage.Window.Invoke("GetGlobalContext"); 

       return globalContext; 
      } 
      catch (System.InvalidOperationException) 
      { 
       throw new InvalidOperationException("Property \"Xrm\" is null and the Global Context is not available."); 
      } 

     } 

     ScriptObject pageProperty = (ScriptObject)xrmProperty.GetProperty("Page"); 
     if (null == pageProperty) 
     { 
      throw new InvalidOperationException("Property \"Xrm.Page\" is null"); 
     } 

     ScriptObject contextProperty = (ScriptObject)pageProperty.GetProperty("context"); 
     if (null == contextProperty) 
     { 
      throw new InvalidOperationException("Property \"Xrm.Page.context\" is null"); 
     } 

     return contextProperty; 
    } 

당신은 추가 클래스 라이브러리를 추가하고 그 안에 다음 코드를 삽입해야합니다. 내 보낸 컨텍스트와 일치하도록 클래스 이름을 변경하십시오.

partial class FelineSoftContext 
{ 

    #region Methods 
    partial void OnContextCreated() 
    { 
     this.ReadingEntity += this.OnReadingEntity; 
     this.WritingEntity += this.OnWritingEntity; 
    } 
    #endregion 

    #region Event Handlers 
    private void OnReadingEntity(object sender, ReadingWritingEntityEventArgs e) 
    { 
     ODataEntity entity = e.Entity as ODataEntity; 
     if (null == entity) 
     { 
      return; 
     } 

     entity.ClearChangedProperties(); 
    } 

    private void OnWritingEntity(object sender, ReadingWritingEntityEventArgs e) 
    { 
     ODataEntity entity = e.Entity as ODataEntity; 
     if (null == entity) 
     { 
      return; 
     } 

     entity.RemoveUnchangedProperties(e.Data); 
     entity.ClearChangedProperties(); 
    } 
    #endregion 
} 

public abstract class ODataEntity 
{ 
    private readonly Collection<string> ChangedProperties = new Collection<string>(); 

    public ODataEntity() 
    { 
     EventInfo info = this.GetType().GetEvent("PropertyChanged"); 
     if (null != info) 
     { 
      PropertyChangedEventHandler method = new PropertyChangedEventHandler(this.OnEntityPropertyChanged); 

      //Ensure that the method is not attached and reattach it 
      info.RemoveEventHandler(this, method); 
      info.AddEventHandler(this, method); 
     } 
    } 

    #region Methods 
    public void ClearChangedProperties() 
    { 
     this.ChangedProperties.Clear(); 
    } 

    internal void RemoveUnchangedProperties(XElement element) 
    { 
     const string AtomNamespace = "http://www.w3.org/2005/Atom"; 
     const string DataServicesNamespace = "http://schemas.microsoft.com/ado/2007/08/dataservices"; 
     const string DataServicesMetadataNamespace = DataServicesNamespace + "/metadata"; 

     if (null == element) 
     { 
      throw new ArgumentNullException("element"); 
     } 

     List<XElement> properties = (from c in element.Elements(XName.Get("content", AtomNamespace) 
               ).Elements(XName.Get("properties", DataServicesMetadataNamespace)).Elements() 
            select c).ToList(); 

     foreach (XElement property in properties) 
     { 
      if (!this.ChangedProperties.Contains(property.Name.LocalName)) 
      { 
       property.Remove(); 
      } 
     } 
    } 

    private void OnEntityPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) 
    { 
     if (!this.ChangedProperties.Contains(e.PropertyName)) 
     { 
      this.ChangedProperties.Add(e.PropertyName); 
     } 
    } 
    #endregion 
} 
관련 문제