Share via


CRMQuery.ExecuteQuery Method

The ExecuteQuery method executes a query in the Fetch XML format.

Note For any given object, using the ExecuteQuery method is slower than using the Retrieve method because the ExecuteQuery method requires parsing and validation of the Fetch XML. You can use the Retrieve method for an object for better performance.

Syntax

[Visual Basic .NET]
Public Function ExecuteQuery(
  ByVal Caller As CUserAuth,
  ByVal QueryXML As String
) As String
[C#]
public string ExecuteQuery(
  CUserAuth  Caller,
  string  QueryXML
);
[C++]
public: String* ExecuteQuery(
  CUserAuth*  Caller,
  String*  QueryXML
);

Parameters

Caller

Specifies the identity of the caller. The caller must have the READ privilege for the object type being queried to perform this action. See CUserAuth.

QueryXML

Specifies the Fetch XML string representing the query. See Fetch XML Schema for more information.

Return Value

Returns a String type that specifies the XML data containing the results of the query. See ObjectSchemas for more information.

Remarks

If there is an error, SOAP throws an exception and the error message is reported in System.Web.Services.Protocols.SoapException.Detail.OuterXml.

All IDs passed to the platform are GUIDs wrapped in braces. For example: {6522D89A-A752-4455-A2B0-51494C6957C3}

Example

[C#]
// strServer should be set with the name of the platform Web server
string strServer = "mystrServername";

// virtualDirectory should be set with the name of the Microsoft CRM
// virtual directory on the platform Web server
string virtualDirectory = "mscrmservices";
string strDir = "https://" + strServer + "/" + virtualDirectory + "/";

// BizUser proxy object
Microsoft.CRM.Proxy.BizUser bizUser = new Microsoft.CRM.Proxy.BizUser ();
bizUser.Credentials = System.Net.CredentialCache.DefaultCredentials;
bizUser.Url = strDir + "BizUser.srf";

// CRMQuery proxy object
Microsoft.CRM.Proxy.CRMQuery query = new Microsoft.CRM.Proxy.CRMQuery();
query.Credentials = System.Net.CredentialCache.DefaultCredentials;
query.Url = strDir + "CRMQuery.srf";

String strErrorMsg;
try
{
   Microsoft.CRM.Proxy.CUserAuth userAuth = bizUser.WhoAmI();

   // Set up the Fetch XML to retrieve all of the account information 
   string strQueryXML = "<fetch mapping='logical'><entity name='account'><all-attributes/></entity></fetch>";

   // Execute the query
   string strQueryResultXml = query.ExecuteQuery(userAuth, strQueryXML);
}
catch (System.Web.Services.Protocols.SoapException err)
{
   // Process the platform error here
   strErrorMsg = ("ErrorMessage: " + err.Message + " " + err.Detail.OuterXml + " Source: " + err.Source );
}
catch (Exception err)
{
   // Process other errors here
   strErrorMsg = ("ErrorMessage: " + err.Message );
}

Requirements

Namespace: Microsoft.CRM.Proxy

Assembly: microsoft.crm.proxy.dll

See Also

© 2003 Microsoft Corporation. All rights reserved.