/// <summary> /// Returns results as xml string /// </summary> /// <param name="service"></param> /// <param name="_userquery"></param> /// <returns></returns> internal static string executeUserQuery(CrmServiceClient service, EntityReference _userquery) { ExecuteByIdUserQueryRequest executeUserQuery = new ExecuteByIdUserQueryRequest() { EntityId = _userquery }; ExecuteByIdUserQueryResponse executeUserQueryResponse = (ExecuteByIdUserQueryResponse)service.Execute(executeUserQuery); //<resultset> + <result>result1</result><result>result2</result> + ... + </resultset> return(executeUserQueryResponse.String); }
/// <summary> /// This method first creates a series of Accounts to query over, a user query /// that retrieves the names of all Accounts with a name of 'Coho Winery' and /// a system query that retrieves all Account names. Then it validates the system /// query, executes the system query and displays the results, and finally /// executes the user query and displays the results. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete) { try { //<snippetUserQueryAndSavedQuery1> // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); CreateRequiredRecords(); #region Validate saved query // Create the request ValidateSavedQueryRequest validateRequest = new ValidateSavedQueryRequest() { FetchXml = _savedQuery.FetchXml, QueryType = _savedQuery.QueryType.Value }; // Send the request Console.WriteLine(" Validating Saved Query"); try { // executing the request will throw an exception if the fetch xml is invalid var validateResponse = (ValidateSavedQueryResponse)_serviceProxy.Execute(validateRequest); Console.WriteLine(" Saved Query validated successfully"); } catch (Exception) { Console.WriteLine(" Invalid Saved Query"); throw; } #endregion #region Execute saved query // Create the request ExecuteByIdSavedQueryRequest executeSavedQueryRequest = new ExecuteByIdSavedQueryRequest() { EntityId = _savedQuery.Id }; // Execute the request Console.WriteLine(" Executing Saved Query"); ExecuteByIdSavedQueryResponse executeSavedQueryResponse = (ExecuteByIdSavedQueryResponse)_serviceProxy.Execute(executeSavedQueryRequest); // Check results if (String.IsNullOrEmpty(executeSavedQueryResponse.String)) { throw new Exception("Saved Query did not return any results"); } PrintResults(executeSavedQueryResponse.String); #endregion #region Execute user query // Create the request ExecuteByIdUserQueryRequest executeUserQuery = new ExecuteByIdUserQueryRequest() { EntityId = _userQuery.ToEntityReference() }; // Send the request Console.WriteLine(" Executing User Query"); ExecuteByIdUserQueryResponse executeUserQueryResponse = (ExecuteByIdUserQueryResponse)_serviceProxy.Execute(executeUserQuery); if (String.IsNullOrEmpty(executeUserQueryResponse.String)) { throw new Exception("User Query did not return any results"); } // validate results PrintResults(executeUserQueryResponse.String); #endregion DeleteRequiredRecords(promptforDelete); } //</snippetUserQueryAndSavedQuery1> } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { // You can handle an exception here or pass it back to the calling method. throw; } }