This sample shows how to write code to perform the function of the following APIs that are deprecated.
- RetrieveByPrincipal
- RetrieveByOrganization
- RetrieveBySubContacts
1: //# [Build Query Expressions]
2: using System;
3: using System.Web.Services.Protocols;
4: using Microsoft.Crm.Sdk.Utility;
5:
6: namespace Microsoft.Crm.Sdk.HowTo
7: {
8: // Using following Microsoft Dynamics CRM namespace(s) in the sample.
9: using CrmSdk;
10:
11: /// <summary>
12: /// This Sample shows how to perform retrieveby methods that have been deprecated using RetrieveMultiple
13: /// -use RetrieveMultiple to carry out a RetrieveByPrincipal command
14: /// -use RetrieveMultiple to carry out a RetrieveByOrganization command
15: /// -use RetrieveMultiple to carry out a RetrieveBySubContacts command.
16: /// </summary>
17: public class HowToRetrieveMultiple
18: {
19: static void Main(string[] args)
20: {
21: try
22: {
23: // TODO: Change the server URL and Organization to match your
24: // CRM Server and CRM Organization
25: HowToRetrieveMultiple.Run("http://localhost:5555", "CRM_Organization");
26: Console.WriteLine("RetrieveByPrincipal, RetrieveByOrganization" +
27: "and RetrieveBySubContacts messages retrieved successfully.");
28: }
29: catch (SoapException ex)
30: {
31: Console.WriteLine("The application terminated with an error.");
32: Console.WriteLine(ex.Message);
33: Console.WriteLine(ex.Detail.InnerText);
34: }
35: catch (System.Exception ex)
36: {
37: Console.WriteLine("The application terminated with an error.");
38: Console.WriteLine(ex.Message);
39:
40: // Display the details of the inner exception.
41: if (ex.InnerException != null)
42: {
43: Console.WriteLine(ex.InnerException.Message);
44:
45: SoapException se = ex.InnerException as SoapException;
46: if (se != null)
47: Console.WriteLine(se.Detail.InnerText);
48: }
49: }
50: finally
51: {
52: Console.WriteLine("Press <Enter> to exit.");
53: Console.ReadLine();
54: }
55: }
56:
57: public static bool Run(string crmServerUrl, string orgName)
58: {
59: // Set up the CRM Service.
60: CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);
61: // Cache credentials so each request does not have to be re-authenticated.
62: service.PreAuthenticate = true;
63:
64: #region Setup Data Required for this Sample
65:
66: bool success = false;
67:
68: // Get user information.
69: WhoAmIRequest userRequest = new WhoAmIRequest();
70: WhoAmIResponse user = (WhoAmIResponse)service.Execute(userRequest);
71:
72: // Get the organizationId.
73: TargetRetrieveBusinessUnit targetGetBusinessUnit = new TargetRetrieveBusinessUnit();
74: targetGetBusinessUnit.EntityId = user.BusinessUnitId;
75:
76: RetrieveRequest getBusinessUnit = new RetrieveRequest();
77: getBusinessUnit.Target = targetGetBusinessUnit;
78: // Be aware that using AllColumns may adversely affect
79: // performance and cause unwanted cascading in subsequent
80: // updates. A best practice is to retrieve the least amount of
81: // data required.
82: getBusinessUnit.ColumnSet = new AllColumns();
83:
84: RetrieveResponse businessUnitResponse = (RetrieveResponse)service.Execute(getBusinessUnit);
85: Lookup org = ((businessunit)businessUnitResponse.BusinessEntity).organizationid;
86:
87: Guid organizationId = org.Value;
88:
89: // Create a sample account.
90: account accountCreate = new account();
91: accountCreate.name = "Fourth Coffee";
92:
93: TargetCreateAccount targetCreate = new TargetCreateAccount();
94: targetCreate.Account = accountCreate;
95:
96: CreateRequest create = new CreateRequest();
97: create.Target = targetCreate;
98:
99: CreateResponse created = (CreateResponse)service.Execute(create);
100:
101: Guid accountId = created.id;
102:
103: //Create contact
104: contact contactCreate = new contact();
105: contactCreate.firstname = "Oliveira";
106: contactCreate.lastname = "Leonardo";
107: //Set the account created above to be the parent account for the contact
108: Customer parentCustomer = new Customer();
109: parentCustomer.type = "account";
110: parentCustomer.Value = accountId;
111: contactCreate.parentcustomerid = parentCustomer;
112:
113: TargetCreateContact targetCreateContact = new TargetCreateContact();
114: targetCreateContact.Contact = contactCreate;
115:
116: CreateRequest createContactRequest = new CreateRequest();
117: createContactRequest.Target = targetCreateContact;
118:
119: CreateResponse createdContact = (CreateResponse)service.Execute(createContactRequest);
120:
121: #endregion
122:
123: #region RetrieveByPrincipal
124: // The following code example demonstrates how to use RetrieveMultiple
125: // to carry out a RetrieveByPrincipal.
126:
127: // Sets the principalId to be the principal.
128: //SDK: Guid principalId = new Guid("2aa418c2-82ff-dd11-95c2-00155da4c706");
129: Guid principalId = user.UserId;
130:
131: // Create a column set holding the names of the columns to be retrieved.
132: ColumnSet colsPrincipal = new ColumnSet();
133:
134: // Set the properties of the column set.
135: colsPrincipal.Attributes = new string [] {"name", "accountid"};
136:
137: // Create a ConditionExpression.
138: ConditionExpression conditionPrincipal = new ConditionExpression();
139:
140: // Set the ConditionExpressions properties so that the condition is true when the
141: // ownerid of the account equals the principalId.
142: conditionPrincipal.AttributeName = "ownerid";
143: conditionPrincipal.Operator = ConditionOperator.Equal;
144: conditionPrincipal.Values = new object [1];
145: conditionPrincipal.Values[0] = principalId;
146:
147: // Create the FilterExpression.
148: FilterExpression filterPrincipal = new FilterExpression();
149:
150: // Set the properties of the FilterExpression.
151: filterPrincipal.FilterOperator = LogicalOperator.And;
152: filterPrincipal.Conditions = new ConditionExpression[] {conditionPrincipal};
153:
154: // Create the QueryExpression.
155: QueryExpression queryPrincipal = new QueryExpression();
156:
157: // Set the properties of the QueryExpression.
158: queryPrincipal.EntityName = EntityName.account.ToString();
159: queryPrincipal.ColumnSet = colsPrincipal;
160: queryPrincipal.Criteria = filterPrincipal;
161:
162: // Create the request object.
163: RetrieveMultipleRequest retrievePrincipal = new RetrieveMultipleRequest();
164:
165: // Set the properties of the request object.
166: retrievePrincipal.Query = queryPrincipal;
167:
168: // Execute the request.
169: RetrieveMultipleResponse principalResponse =
170: (RetrieveMultipleResponse) service.Execute(retrievePrincipal);
171: #endregion
172:
173: #region RetrieveByOrganization
174: // The following code example demonstrates how to use RetrieveMultiple
175: // to carry out a RetrieveByOrganization.
176:
177: // Create a column set that holds the names of the columns to be retrieved.
178: ColumnSet colsOrganization = new ColumnSet();
179: colsOrganization.Attributes = new string [] {"fullname", "systemuserid"};
180:
181: // Create a ConditionExpression.
182: ConditionExpression conditionUsersInOrganization = new ConditionExpression();
183:
184: // Set the ConditionExpressions properties so that the condition is true when
185: // the organizationid of the user is equal to the organizationId.
186: conditionUsersInOrganization.AttributeName = "organizationid";
187: conditionUsersInOrganization.Operator = ConditionOperator.Equal;
188: conditionUsersInOrganization.Values = new object [1];
189: // SDK: conditionUsersInOrganization.Values[0] = new Guid("435547a3-10e4-dd11-acef-00155da4c70}");
190: conditionUsersInOrganization.Values[0] = organizationId;
191:
192: // Create the FilterExpression.
193: FilterExpression filterUsersInOrganization = new FilterExpression();
194:
195: // Set the properties of the FilterExpression.
196: filterUsersInOrganization.FilterOperator = LogicalOperator.And;
197: filterUsersInOrganization.Conditions = new ConditionExpression[] {conditionUsersInOrganization};
198:
199: // Create the QueryExpression.
200: QueryExpression queryUsersInOrganization = new QueryExpression();
201:
202: // Set the properties of the QueryExpression.
203: queryUsersInOrganization.EntityName = EntityName.systemuser.ToString();
204: queryUsersInOrganization.ColumnSet = colsOrganization;
205: queryUsersInOrganization.Criteria = filterUsersInOrganization;
206:
207: // Create the request object.
208: RetrieveMultipleRequest retrieveUsersInOrganization = new RetrieveMultipleRequest();
209:
210: // Set the properties of the request object.
211: retrieveUsersInOrganization.Query = queryUsersInOrganization;
212:
213: // Execute the request.
214: RetrieveMultipleResponse retrievedUsers =
215: (RetrieveMultipleResponse) service.Execute(retrieveUsersInOrganization);
216: #endregion
217:
218: #region RetrieveBySubContacts
219: // The following code example demonstrates how to use RetrieveMultiple
220: // to carry out a RetrieveBySubContacts command.
221:
222: // Create a column set that holds the names of the columns to be retrieved.
223: ColumnSet colsContact = new ColumnSet();
224: colsContact.Attributes = new string [] {"fullname", "contactid"};
225:
226: // Create the ConditionExpression.
227: ConditionExpression conditionContact = new ConditionExpression();
228:
229: // Set the ConditionExpressions Properties so that the condition is true when
230: // the accountid or the contact is equal to accountId.
231: conditionContact.AttributeName = "accountid";
232: conditionContact.Operator = ConditionOperator.Equal;
233: // SDK: conditionContact.Values = new string [] {"f0df7fbf-b51f-de11-892f-00155da4c706"};
234: conditionContact.Values = new string [] {accountId.ToString()};
235:
236: // Create the FilterExpression.
237: FilterExpression filterContact = new FilterExpression();
238:
239: // Set the properties of the FilterExpression.
240: filterContact.FilterOperator = LogicalOperator.And;
241: filterContact.Conditions = new ConditionExpression[] {conditionContact};
242:
243: // Create the QueryExpression.
244: QueryExpression queryContact = new QueryExpression();
245:
246: // Set the properties of the QueryExpression.
247: queryContact.EntityName = EntityName.contact.ToString();
248: queryContact.ColumnSet = colsContact;
249: queryContact.Criteria = filterContact;
250:
251: // Retrieve the contacts.
252: BusinessEntityCollection contacts = service.RetrieveMultiple(queryContact);
253: #endregion
254:
255: #region check success
256:
257: if ((principalResponse.BusinessEntityCollection.EntityName.ToLower().Equals("account")) &&
258: (retrievedUsers.BusinessEntityCollection.EntityName.ToLower().Equals("systemuser")) &&
259: (contacts.EntityName.ToLower().Equals("contact")))
260: {
261: success = true;
262: }
263: #endregion
264:
265: #region Remove Data Required for this Sample
266:
267: service.Delete(EntityName.contact.ToString(), createdContact.id);
268: service.Delete(EntityName.account.ToString(), accountId);
269:
270: #endregion
271:
272:
273: return success;
274: }
275: }
276: }
No comments:
Post a Comment