Creating a Query 3
This sample creates a more complex query against the account object.
Class Reference
Schema Reference
- account.xsd
- savedquery.xsd
- Fetch XML Schema
Example
[C#]
public void IntermediateQuery()
{
// strServer should be set with the name of the platform Web server
String strServer = "MyServerName";
// strVirtualDirectory should be set with the name of the Microsoft CRM
// virtual directory on the platform Web server
String strVirtualDirectory = "mscrmservices";
String strDir = String.Concat("https://", strServer, "/",
strVirtualDirectory, "/");
// BizUser proxy object
Microsoft.Crm.Platform.Proxy.BizUser bizUser
= new Microsoft.Crm.Platform.Proxy.BizUser ();
bizUser.Credentials = System.Net.CredentialCache.DefaultCredentials;
bizUser.Url = String.Concat(strDir, "BizUser.srf");
// Query proxy object
Microsoft.Crm.Platform.Proxy.CRMQuery query
= new Microsoft.Crm.Platform.Proxy.CRMQuery ();
query.Credentials = System.Net.CredentialCache.DefaultCredentials;
query.Url = String.Concat(strDir, "CRMQuery.srf");
String strErrorMsg;
try
{
Microsoft.Crm.Platform.Proxy.CUserAuth userAuth = bizUser.WhoAmI();
// This query is "select accountid and name from Accounts where
// the owner's last name is not ‘Cannon’ and it is not null"
// and will return all accounts for which you have read access
StringBuilder queryXml = new StringBuilder("<fetch>");
queryXml.Append("<entity name='Account'>");
queryXml.Append("<attribute name = 'accountid'/>");
queryXml.Append("<attribute name = 'name'/>");
queryXml.Append("<link-entity name='SystemUser' to='ownerid'>");
queryXml.Append("<filter type='and'>");
queryXml.Append("<condition attribute = 'lastname' ");
queryXml.Append("operator='ne' value='Cannon'/>");
queryXml.Append("<condition attribute = 'lastname' ");
queryXml.Append("operator='not-null'/>");
queryXml.Append("</filter>");
queryXml.Append("</link-entity>");
queryXml.Append("</entity></fetch>");
// This will return a resultset with all the accounts you can see
String accountsXml = query.ExecuteQuery(userAuth,
queryXml.ToString());
}
catch (System.Web.Services.Protocols.SoapException err)
{
// Process the platform error here
strErrorMsg = String.Concat("ErrorMessage: ", err.Message, " ",
err.Detail.OuterXml, " Source: ", err.Source);
}
}