This sample shows how to build a QueryExpression to for use with RetrieveMultiple to find all the roles for a user. To do this, you have to build a query for roles where you join 'role' to 'systemuserroles', and then join systemuserroles' to 'systemuser' and add a condition where systemuser.systemuserid equals the attribute UserId.
1: //# Retrieve the Roles for a User #//
2:
3: // Retrieve the GUID of the logged on user.
4: WhoAmIRequest whoReq = new WhoAmIRequest();
5: WhoAmIResponse whoResp = (WhoAmIResponse) service.Execute(whoReq);
6: Guid userid = whoResp.UserId;
7:
8: // Create a QueryExpression.
9: QueryExpression qe = new QueryExpression();
10: qe.EntityName = "role";
11: // Be aware that using AllColumns may adversely affect
12: // performance and cause unwanted cascading in subsequent
13: // updates. A best practice is to retrieve the least amount of
14: // data required.
15: qe.ColumnSet = new AllColumns();
16:
17: // Set up the join between the role entity
18: // and the intersect table systemuserroles.
19: LinkEntity le = new LinkEntity();
20: le.LinkFromEntityName = "role";
21: le.LinkFromAttributeName = "roleid";
22: le.LinkToEntityName = "systemuserroles";
23: le.LinkToAttributeName = "roleid";
24:
25: // Set up the join between the intersect table
26: // systemuserroles and the systemuser entity.
27: LinkEntity le2 = new LinkEntity();
28: le2.LinkFromEntityName = "systemuserroles";
29: le2.LinkFromAttributeName = "systemuserid";
30: le2.LinkToEntityName = "systemuser";
31: le2.LinkToAttributeName = "systemuserid";
32:
33: // The condition is to find the user ID.
34: ConditionExpression ce = new ConditionExpression();
35: ce.AttributeName = "systemuserid";
36: ce.Operator = ConditionOperator.Equal;
37: ce.Values = new object[]{userid};
38:
39: le2.LinkCriteria = new FilterExpression();
40: le2.LinkCriteria.Conditions = new ConditionExpression[]{ce};
41:
42: le.LinkEntities = new LinkEntity[]{le2};
43: qe.LinkEntities = new LinkEntity[]{le};
44:
45: // Execute the query.
46: BusinessEntityCollection bec = service.RetrieveMultiple(qe);
No comments:
Post a Comment