2015-10-22 71 views
0

我試圖通過RetrieveMultiple方法和查詢表達式獲取systemuser的所有約會。例如:Dynamics CRM:爲系統用戶檢索多個約會

WhoAmIRequest userRequest = new WhoAmIRequest(); 
WhoAmIResponse userResponse = (WhoAmIResponse)_serviceProxy.Execute(userRequest); 

QueryExpression qe = new QueryExpression(); 
qe.EntityName = "systemuser"; 

... 
slos.RetrieveMultiple(qe); 

我可以從systemuser實體檢索systemuser(所有者,組織者,參加者需要,可選的與會者)的所有的約會?

或者是否必須檢索CRM的所有約會並添加條件以瞭解用戶是所有者,組織者,必需的還是可選的與會者?

最後,我使用SOAP Logger,它是生成SOAP請求的最佳方式嗎?

回答

2

您需要使用appointmentsystemuser之間的關係實體activitypointer。正如你在我的例子中看到的那樣,這會讓事情變得複雜一些。

至少有2種可能的方式來建立你所需的查詢:

var qe = new QueryExpression 
{ 
    EntityName = "appointment", 
    ColumnSet = new ColumnSet("subject"), 
    LinkEntities = 
    { 
     new LinkEntity 
     { 
      EntityAlias = "ap", 
      JoinOperator = JoinOperator.Inner, 
      Columns = new ColumnSet(false), 
      LinkFromEntityName = "appointment", 
      LinkFromAttributeName = "activityid", 
      LinkToEntityName = "activityparty", 
      LinkToAttributeName = "activityid", 
      LinkCriteria = new FilterExpression 
      { 
       Conditions = 
       { 
        new ConditionExpression("partyid", ConditionOperator.Equal, userid), 
       }, 
      }, 
     }, 
    }, 
}; 

2)您可以通過查詢systemuser:

1)正如你已經想通,您可以通過systemuserid篩選約會systemuserid並添加任命爲鏈接的實體(如在一個SQL查詢JOIN):

var qe2 = new QueryExpression 
{ 
    EntityName = "systemuser", 
    ColumnSet = new ColumnSet(false), 
    LinkEntities = 
    { 
     new LinkEntity 
     { 
      EntityAlias = "ap", 
      Columns = new ColumnSet(false), 
      JoinOperator = JoinOperator.Inner, 
      LinkFromEntityName = "systemuser", 
      LinkFromAttributeName = "systemuserid", 
      LinkToEntityName = "activityparty", 
      LinkToAttributeName = "partyid", 
      LinkEntities = 
      { 
       new LinkEntity 
       { 
        EntityAlias = "a", 
        Columns = new ColumnSet("subject"), 
        JoinOperator = JoinOperator.Inner, 
        LinkFromEntityName = "activityparty", 
        LinkFromAttributeName = "activityid", 
        LinkToEntityName = "appointment", 
        LinkToAttributeName = "activityid", 
       }, 
      }, 
     }, 
    }, 
    Criteria = new FilterExpression 
    { 
     Conditions = 
     { 
      new ConditionExpression("systemuserid", ConditionOperator.Equal, userid), 
     }, 
    }, 
}; 

,關於對參與角色的過濾器,你就必須在participationtypemask添加一個條件在activitypointer

// user is Organizer, Owner, required or optional Attendee 
ConditionExpression("participationtypemask", ConditionOperator.In, new int[] { 5, 6, 7, 9 }), 
+0

非常感謝您的回答,這樣可以節省我的一天。還有一件事,如果在ColumnSet中設置了「組織者」,它將檢索包含所有字段的EntityCollection。是否可以在組織者中指定我想要的字段? – bidou88

+1

當然這是可能的 - 而不是像我的expample中那樣只指定*「subject」*,你可以簡單地設置'new ColumnSet(true)'來檢索所有的字段。如果您需要根據條件構建QueryExpression,請將其分開並根據需要設置屬性。我傾向於以所示的方式構造QueryExpression,因爲它使它們更像SQL查詢的可讀性。 – Filburt

+0

謝謝,我沒有說好我的問題。如果在我的ColumnSet中添加組織者,它將檢索組織者,但所有字段都附加到組織者。我想要做的只是在組織者中只有特定的領域。我不知道是否清楚? – bidou88

0

有了這個表達,我沒有收到任何的任命現在:

QueryExpression qe = new QueryExpression 
{ 
    EntityName = "appointment", 
    ColumnSet = new ColumnSet("activityid", "subject", "scheduledstart", "scheduledend", "location", "description"), 
    Criteria = new FilterExpression 
    { 
     FilterOperator = LogicalOperator.And, 
     Conditions = 
     { 
      new ConditionExpression("scheduledend", ConditionOperator.GreaterThan, startTime), 
      new ConditionExpression("scheduledstart", ConditionOperator.LessThan, endTime) 
     } 
    }, 
    LinkEntities = 
    { 
     new LinkEntity 
     { 
      LinkFromEntityName = "activitypointer", 
      LinkFromAttributeName = "activityid", 
      LinkToEntityName = "activityparty", 
      LinkToAttributeName = "activityid", 
      LinkCriteria = new FilterExpression 
      { 
       FilterOperator = LogicalOperator.And, 
       Conditions = 
       { 
        new ConditionExpression("participationtypemask", ConditionOperator.In, new int[] { 5, 6, 7, 9 }), 
        new ConditionExpression("partyid", ConditionOperator.Equal, userResponse.UserId) 

       } 
      } 
     }, 
     new LinkEntity 
     { 
      EntityAlias = "requiredattendees", 
      Columns = new ColumnSet(false), 
      JoinOperator = JoinOperator.Inner, 
      LinkFromEntityName = "activitypointer", 
      LinkFromAttributeName = "activityid", 
      LinkToEntityName = "activityparty", 
      LinkToAttributeName = "activityid", 
      LinkCriteria = new FilterExpression 
      { 
       Conditions = 
       { 
        new ConditionExpression("participationtypemask", ConditionOperator.Equal, 5) 

       } 
      }, 
      LinkEntities = 
      { 
       new LinkEntity 
       { 
        EntityAlias = "contact", 
        Columns = new ColumnSet("fullname"), 
        JoinOperator = JoinOperator.Inner, 
        LinkFromEntityName = "activityparty", 
        LinkFromAttributeName = "activityid", 
        LinkToEntityName = "contact", 
        LinkToAttributeName = "contactid" 
       }, 
      } 
     } 
    } 
}; 

qe.Distinct = true; 

slos.RetrieveMultiple(qe); 
+0

乍一看我會說你的問題是現在你指定'JoinOperator.Inner'在你的* requiredattendees *和* contact * LinkEntities(應該是JoinOperator.LeftOuter)。如果我發現自己有這樣的意外結果,我嘗試使用「高級查找」來重建查詢並檢查它後面的FetchXml。這通常會很快清理事情。附註:您可能希望將此「答案」的內容作爲新問題移至原始帖子或帖子。 – Filburt

相關問題