Sample: Validate and execute a saved query
Applies To: Dynamics CRM 2013
This sample code is for Microsoft Dynamics CRM 2013 and Microsoft Dynamics CRM Online. Download the Microsoft Dynamics CRM SDK package. It can be found in the following location in the download package:
SampleCode\CS\GeneralProgramming\Queries\UserQueryAndSavedQuery.cs
SampleCode\VB\GeneralProgramming\Queries\UserQueryAndSavedQuery.vb
Requirements
For more information about the requirements for running the sample code provided in this SDK, see Use the sample and helper code.
Full Sample – Visual Basic .NET
Demonstrates
This sample shows how to use the IOrganizationService.ValidateSavedQueryRequest message to validate a FetchXML query, and then use the IOrganizationService.ExecuteByIdSavedQueryRequest message to execute the query.
Example
// Connect to the Organization service.
// The using statement assures that the service proxy will be properly disposed.
using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
{
// 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);
}
' Connect to the Organization service.
' The using statement assures that the service proxy will be properly disposed.
_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig)
Using _serviceProxy
' This statement is required to enable early-bound type support.
_serviceProxy.EnableProxyTypes()
CreateRequiredRecords()
' #Region "Validate saved query"
' Create the request
Dim validateRequest As New ValidateSavedQueryRequest() With
{
.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
Dim validateResponse = CType(_serviceProxy.Execute(validateRequest),
ValidateSavedQueryResponse)
Console.WriteLine(" Saved Query validated successfully")
Catch e1 As Exception
Console.WriteLine(" Invalid Saved Query")
Throw
End Try
' #End Region
' #Region "Execute saved query"
' Create the request
Dim executeSavedQueryRequest As New ExecuteByIdSavedQueryRequest() With
{
.EntityId = _savedQuery.Id
}
' Execute the request
Console.WriteLine(" Executing Saved Query")
Dim executeSavedQueryResponse As ExecuteByIdSavedQueryResponse =
CType(_serviceProxy.Execute(executeSavedQueryRequest),
ExecuteByIdSavedQueryResponse)
' Check results
If String.IsNullOrEmpty(executeSavedQueryResponse.String) Then
Throw New Exception("Saved Query did not return any results")
End If
PrintResults(executeSavedQueryResponse.String)
' #End Region
' #Region "Execute user query"
' Create the request
Dim executeUserQuery As New ExecuteByIdUserQueryRequest() With
{
.EntityId = _userQuery.ToEntityReference()
}
' Send the request
Console.WriteLine(" Executing User Query")
Dim executeUserQueryResponse As ExecuteByIdUserQueryResponse =
CType(_serviceProxy.Execute(executeUserQuery),
ExecuteByIdUserQueryResponse)
If String.IsNullOrEmpty(executeUserQueryResponse.String) Then
Throw New Exception("User Query did not return any results")
End If
' validate results
PrintResults(executeUserQueryResponse.String)
' #End Region
DeleteRequiredRecords(promptforDelete)
End Using
Full Sample – C#
using System;
using System.Collections.Generic;
using System.IO;
using System.ServiceModel;
using System.Text;
using System.Xml;
// These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly
// located in the SDK\bin folder of the SDK download.
using Microsoft.Xrm.Sdk.Client;
// This namespace is found in Microsoft.Crm.Sdk.Proxy.dll assembly
// found in the SDK\bin folder.
using Microsoft.Crm.Sdk.Messages;
namespace Microsoft.Crm.Sdk.Samples
{
/// <summary>
/// Demonstrates how to execute saved and user queries by id, and how to validate
/// a saved query.
/// </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 class UserQueryAndSavedQuery
{
#region Class Level Members
private OrganizationServiceProxy _serviceProxy;
private List<Account> _accounts = new List<Account>();
private SavedQuery _savedQuery;
private UserQuery _userQuery;
#endregion Class Level Members
#region How To Sample Code
/// <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
{
// Connect to the Organization service.
// The using statement assures that the service proxy will be properly disposed.
using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
{
// 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);
}
}
// 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;
}
}
#region Public methods
/// <summary>
/// Creates any entity records that this sample requires.
/// </summary>
public void CreateRequiredRecords()
{
#region Create Accounts to query over
Console.WriteLine(" Creating some sample accounts");
Account account = new Account()
{
Name = "Coho Vineyard"
};
account.Id = _serviceProxy.Create(account);
_accounts.Add(account);
Console.WriteLine(" Created Account {0}", account.Name);
account = new Account()
{
Name = "Coho Winery"
};
account.Id = _serviceProxy.Create(account);
_accounts.Add(account);
Console.WriteLine(" Created Account {0}", account.Name);
account = new Account()
{
Name = "Coho Vineyard & Winery"
};
account.Id = _serviceProxy.Create(account);
_accounts.Add(account);
Console.WriteLine(" Created Account {0}", account.Name);
#endregion
#region Create a Saved Query
Console.WriteLine(" Creating a Saved Query that retrieves all Account ids");
_savedQuery = new SavedQuery()
{
Name = "Fetch all Account ids",
ReturnedTypeCode = Account.EntityLogicalName,
FetchXml = @"
<fetch mapping='logical'>
<entity name='account'>
<attribute name='name' />
</entity>
</fetch>",
QueryType = 0,
};
_savedQuery.Id = _serviceProxy.Create(_savedQuery);
#endregion
#region Create a User Query
Console.WriteLine(
" Creating a User Query that retrieves all Account ids for Accounts with name 'Coho Winery'");
_userQuery = new UserQuery()
{
Name = "Fetch Coho Winery",
ReturnedTypeCode = Account.EntityLogicalName,
FetchXml = @"
<fetch mapping='logical'>
<entity name='account'>
<attribute name='name' />
<filter>
<condition attribute='name' operator='eq' value='Coho Winery' />
</filter>
</entity>
</fetch>",
QueryType = 0
};
_userQuery.Id = _serviceProxy.Create(_userQuery);
#endregion
}
/// <summary>
/// Deletes any entity records that were created for this sample.
/// <param name="prompt">Indicates whether to prompt the user
/// to delete the records created in this sample.</param>
/// </summary>
public void DeleteRequiredRecords(bool prompt)
{
bool toBeDeleted = true;
if (prompt)
{
// Ask the user if the created entities should be deleted.
Console.Write("\nDo you want these entity records deleted? (y/n) [y]: ");
String answer = Console.ReadLine();
if (answer.StartsWith("y") ||
answer.StartsWith("Y") ||
answer == String.Empty)
{
toBeDeleted = true;
}
else
{
toBeDeleted = false;
}
}
if (toBeDeleted)
{
_serviceProxy.Delete(SavedQuery.EntityLogicalName,
_savedQuery.Id);
_serviceProxy.Delete(UserQuery.EntityLogicalName,
_userQuery.Id);
foreach (Account a in _accounts)
_serviceProxy.Delete(Account.EntityLogicalName, a.Id);
Console.WriteLine("Entity records have been deleted.");
}
}
#endregion Public Methods
#region Helper methods
private void PrintResults(String response)
{
// Using XmlReader to format output
StringBuilder output = new StringBuilder();
using (XmlReader reader = XmlReader.Create(new StringReader(response)))
{
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true
};
using (XmlWriter writer = XmlWriter.Create(output, settings))
{
// Parse the file and display each of the nodes.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
writer.WriteStartElement(reader.Name);
break;
case XmlNodeType.Text:
writer.WriteString(reader.Value);
break;
case XmlNodeType.XmlDeclaration:
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(reader.Name, reader.Value);
break;
case XmlNodeType.Comment:
writer.WriteComment(reader.Value);
break;
case XmlNodeType.EndElement:
writer.WriteFullEndElement();
break;
}
}
}
}
Console.WriteLine(" Result of query:\r\n {0}", output.ToString());
Console.WriteLine();
}
#endregion
#endregion How To Sample Code
#region Main method
/// <summary>
/// Standard Main() method used by most SDK samples.
/// </summary>
/// <param name="args"></param>
static public void Main(string[] args)
{
try
{
// Obtain the target organization's Web address and client logon
// credentials from the user.
ServerConnection serverConnect = new ServerConnection();
ServerConnection.Configuration config = serverConnect.GetServerConfiguration();
UserQueryAndSavedQuery app = new UserQueryAndSavedQuery();
app.Run(config, true);
}
catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
{
Console.WriteLine("The application terminated with an error.");
Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp);
Console.WriteLine("Code: {0}", ex.Detail.ErrorCode);
Console.WriteLine("Message: {0}", ex.Detail.Message);
Console.WriteLine("Plugin Trace: {0}", ex.Detail.TraceText);
Console.WriteLine("Inner Fault: {0}",
null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
}
catch (System.TimeoutException ex)
{
Console.WriteLine("The application terminated with an error.");
Console.WriteLine("Message: {0}", ex.Message);
Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
Console.WriteLine("Inner Fault: {0}",
null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message);
}
catch (System.Exception ex)
{
Console.WriteLine("The application terminated with an error.");
Console.WriteLine(ex.Message);
// Display the details of the inner exception.
if (ex.InnerException != null)
{
Console.WriteLine(ex.InnerException.Message);
FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException
as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>;
if (fe != null)
{
Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp);
Console.WriteLine("Code: {0}", fe.Detail.ErrorCode);
Console.WriteLine("Message: {0}", fe.Detail.Message);
Console.WriteLine("Plugin Trace: {0}", fe.Detail.TraceText);
Console.WriteLine("Inner Fault: {0}",
null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
}
}
}
// Additional exceptions to catch: SecurityTokenValidationException, ExpiredSecurityTokenException,
// SecurityAccessDeniedException, MessageSecurityException, and SecurityNegotiationException.
finally
{
Console.WriteLine("Press <Enter> to exit.");
Console.ReadLine();
}
}
#endregion Main method
}
}
Full Sample – Visual Basic .NET
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.ServiceModel
Imports System.Text
Imports System.Xml
' These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly
' located in the SDK\bin folder of the SDK download.
Imports Microsoft.Xrm.Sdk.Client
' This namespace is found in Microsoft.Crm.Sdk.Proxy.dll assembly
' found in the SDK\bin folder.
Imports Microsoft.Crm.Sdk.Messages
Namespace Microsoft.Crm.Sdk.Samples
''' <summary>
''' Demonstrates how to execute saved and user queries by id, and how to validate
''' a saved query.
''' </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 Class UserQueryAndSavedQuery
#Region "Class Level Members"
Private _serviceProxy As OrganizationServiceProxy
Private _accounts As New List(Of Account)()
Private _savedQuery As SavedQuery
Private _userQuery As UserQuery
#End Region ' Class Level Members
#Region "How To Sample Code"
''' <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 Sub Run(ByVal serverConfig As ServerConnection.Configuration,
ByVal promptforDelete As Boolean)
Try
' Connect to the Organization service.
' The using statement assures that the service proxy will be properly disposed.
_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig)
Using _serviceProxy
' This statement is required to enable early-bound type support.
_serviceProxy.EnableProxyTypes()
CreateRequiredRecords()
' #Region "Validate saved query"
' Create the request
Dim validateRequest As New ValidateSavedQueryRequest() With
{
.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
Dim validateResponse = CType(_serviceProxy.Execute(validateRequest),
ValidateSavedQueryResponse)
Console.WriteLine(" Saved Query validated successfully")
Catch e1 As Exception
Console.WriteLine(" Invalid Saved Query")
Throw
End Try
' #End Region
' #Region "Execute saved query"
' Create the request
Dim executeSavedQueryRequest As New ExecuteByIdSavedQueryRequest() With
{
.EntityId = _savedQuery.Id
}
' Execute the request
Console.WriteLine(" Executing Saved Query")
Dim executeSavedQueryResponse As ExecuteByIdSavedQueryResponse =
CType(_serviceProxy.Execute(executeSavedQueryRequest),
ExecuteByIdSavedQueryResponse)
' Check results
If String.IsNullOrEmpty(executeSavedQueryResponse.String) Then
Throw New Exception("Saved Query did not return any results")
End If
PrintResults(executeSavedQueryResponse.String)
' #End Region
' #Region "Execute user query"
' Create the request
Dim executeUserQuery As New ExecuteByIdUserQueryRequest() With
{
.EntityId = _userQuery.ToEntityReference()
}
' Send the request
Console.WriteLine(" Executing User Query")
Dim executeUserQueryResponse As ExecuteByIdUserQueryResponse =
CType(_serviceProxy.Execute(executeUserQuery),
ExecuteByIdUserQueryResponse)
If String.IsNullOrEmpty(executeUserQueryResponse.String) Then
Throw New Exception("User Query did not return any results")
End If
' validate results
PrintResults(executeUserQueryResponse.String)
' #End Region
DeleteRequiredRecords(promptforDelete)
End Using
' Catch any service fault exceptions that Microsoft Dynamics CRM throws.
Catch fe As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault)
' You can handle an exception here or pass it back to the calling method.
Throw
End Try
End Sub
#Region "Public methods"
''' <summary>
''' Creates any entity records that this sample requires.
''' </summary>
Public Sub CreateRequiredRecords()
' #Region "Create Accounts to query over"
Console.WriteLine(" Creating some sample accounts")
Dim account_Renamed As New Account() With {.Name = "Coho Vineyard"}
account_Renamed.Id = _serviceProxy.Create(account_Renamed)
_accounts.Add(account_Renamed)
Console.WriteLine(" Created Account {0}", account_Renamed.Name)
account_Renamed = New Account() With {.Name = "Coho Winery"}
account_Renamed.Id = _serviceProxy.Create(account_Renamed)
_accounts.Add(account_Renamed)
Console.WriteLine(" Created Account {0}", account_Renamed.Name)
account_Renamed = New Account() With {.Name = "Coho Vineyard & Winery"}
account_Renamed.Id = _serviceProxy.Create(account_Renamed)
_accounts.Add(account_Renamed)
Console.WriteLine(" Created Account {0}", account_Renamed.Name)
' #End Region
' #Region "Create a Saved Query"
Console.WriteLine(" Creating a Saved Query that retrieves all Account ids")
_savedQuery = New SavedQuery() With
{
.Name = "Fetch all Account ids",
.ReturnedTypeCode = Account.EntityLogicalName,
.FetchXml = " <fetch mapping='logical'> <entity name='account'> <attribute name='name' /> </entity> </fetch>",
.QueryType = 0
}
_savedQuery.Id = _serviceProxy.Create(_savedQuery)
' #End Region
' #Region "Create a User Query"
Console.WriteLine(" Creating a User Query that retrieves all Account ids for Accounts with name 'Coho Winery'")
_userQuery = New UserQuery() With
{
.Name = "Fetch Coho Winery",
.ReturnedTypeCode = Account.EntityLogicalName,
.FetchXml = " <fetch mapping='logical'> <entity name='account'> <attribute name='name' /> <filter> <condition attribute='name' operator='eq' value='Coho Winery' /> </filter> </entity> </fetch>",
.QueryType = 0
}
_userQuery.Id = _serviceProxy.Create(_userQuery)
' #End Region
End Sub
''' <summary>
''' Deletes any entity records that were created for this sample.
''' <param name="prompt">Indicates whether to prompt the user
''' to delete the records created in this sample.</param>
''' </summary>
Public Sub DeleteRequiredRecords(ByVal prompt As Boolean)
Dim toBeDeleted As Boolean = True
If prompt Then
' Ask the user if the created entities should be deleted.
Console.Write(vbLf & "Do you want these entity records deleted? (y/n) [y]: ")
Dim answer As String = Console.ReadLine()
If answer.StartsWith("y") OrElse
answer.StartsWith("Y") OrElse
answer = String.Empty Then
toBeDeleted = True
Else
toBeDeleted = False
End If
End If
If toBeDeleted Then
_serviceProxy.Delete(SavedQuery.EntityLogicalName, _savedQuery.Id)
_serviceProxy.Delete(UserQuery.EntityLogicalName, _userQuery.Id)
For Each a As Account In _accounts
_serviceProxy.Delete(Account.EntityLogicalName, a.Id)
Next a
Console.WriteLine("Entity records have been deleted.")
End If
End Sub
#End Region ' Public Methods
#Region "Helper methods"
Private Sub PrintResults(ByVal response As String)
' Using XmlReader to format output
Dim output As New StringBuilder()
Using reader As XmlReader = XmlReader.Create(New StringReader(response))
Dim settings As XmlWriterSettings =
New XmlWriterSettings With {.Indent = True, .OmitXmlDeclaration = True}
Using writer As XmlWriter = XmlWriter.Create(output, settings)
' Parse the file and display each of the nodes.
Do While reader.Read()
Select Case reader.NodeType
Case XmlNodeType.Element
writer.WriteStartElement(reader.Name)
Case XmlNodeType.Text
writer.WriteString(reader.Value)
Case XmlNodeType.XmlDeclaration, XmlNodeType.ProcessingInstruction
writer.WriteProcessingInstruction(reader.Name, reader.Value)
Case XmlNodeType.Comment
writer.WriteComment(reader.Value)
Case XmlNodeType.EndElement
writer.WriteFullEndElement()
End Select
Loop
End Using
End Using
Console.WriteLine(" Result of query:" & vbCrLf & " {0}", output.ToString())
Console.WriteLine()
End Sub
#End Region
#End Region ' How To Sample Code
#Region "Main method"
''' <summary>
''' Standard Main() method used by most SDK samples.
''' </summary>
''' <param name="args"></param>
Public Shared Sub Main(ByVal args() As String)
Try
' Obtain the target organization's Web address and client logon
' credentials from the user.
Dim serverConnect As New ServerConnection()
Dim config As ServerConnection.Configuration =
serverConnect.GetServerConfiguration()
Dim app As New UserQueryAndSavedQuery()
app.Run(config, True)
Catch ex As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault)
Console.WriteLine("The application terminated with an error.")
Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp)
Console.WriteLine("Code: {0}", ex.Detail.ErrorCode)
Console.WriteLine("Message: {0}", ex.Detail.Message)
Console.WriteLine("Plugin Trace: {0}", ex.Detail.TraceText)
Console.WriteLine("Inner Fault: {0}",
If(Nothing Is ex.Detail.InnerFault, "No Inner Fault", "Has Inner Fault"))
Catch ex As System.TimeoutException
Console.WriteLine("The application terminated with an error.")
Console.WriteLine("Message: {0}", ex.Message)
Console.WriteLine("Stack Trace: {0}", ex.StackTrace)
Console.WriteLine("Inner Fault: {0}",
If(Nothing Is ex.InnerException.Message, "No Inner Fault", ex.InnerException.Message))
Catch ex As System.Exception
Console.WriteLine("The application terminated with an error.")
Console.WriteLine(ex.Message)
' Display the details of the inner exception.
If ex.InnerException IsNot Nothing Then
Console.WriteLine(ex.InnerException.Message)
Dim fe As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault) =
TryCast(ex.InnerException,
FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault))
If fe IsNot Nothing Then
Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp)
Console.WriteLine("Code: {0}", fe.Detail.ErrorCode)
Console.WriteLine("Message: {0}", fe.Detail.Message)
Console.WriteLine("Plugin Trace: {0}", fe.Detail.TraceText)
Console.WriteLine("Inner Fault: {0}",
If(Nothing Is fe.Detail.InnerFault, "No Inner Fault", "Has Inner Fault"))
End If
End If
' Additional exceptions to catch: SecurityTokenValidationException, ExpiredSecurityTokenException,
' SecurityAccessDeniedException, MessageSecurityException, and SecurityNegotiationException.
Finally
Console.WriteLine("Press <Enter> to exit.")
Console.ReadLine()
End Try
End Sub
#End Region ' Main method
End Class
End Namespace
See Also
IOrganizationService
ValidateSavedQueryRequest
ExecuteByIdSavedQueryRequest
Build queries with FetchXML
Use the early bound entity classes in code