Share via


Creating a Query 3

This sample creates a more complex query against the account object.

Class Reference

Schema Reference

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 = "https://" + strServer + "/" + strVirtualDirectory + "/";

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

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

   string strErrorMsg;
   string strAccounts;
   try
   {
      Microsoft.CRM.Proxy.CUserAuth oUserAuth = oBizUser.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
      string strQuery = "<fetch mapping='logical'><entity name='account'>";
      strQuery += "<attribute name = 'accountid'/>";
      strQuery += "<attribute name = 'name'/>";
      strQuery += "<link-entity name='systemuser' to='ownerid'>";
      strQuery += "<filter type='and'>";
      strQuery += "<condition attribute = 'lastname' ";
      strQuery += "operator='ne' value=Cannon/>";
      strQuery += "<condition attribute = 'lastname' ";
      strQuery += "operator='not-null'/>";
      strQuery += "</filter>";
      strQuery += "</link-entity>";
      strQuery += "</entity></fetch>";

      // This will return a resultset with all the accounts you can see
      strAccounts = oQuery.ExecuteQuery(oUserAuth,strQuery);
   }
   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 + "Source: " + err.Source );
   }
}

© 2003 Microsoft Corporation. All rights reserved.