Creating a Query 3

CRM 1.0

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

Class Reference

Schema Reference



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("http://", 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;
      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' ");

      // This will return a resultset with all the accounts you can see
      String accountsXml = query.ExecuteQuery(userAuth,
   catch (System.Web.Services.Protocols.SoapException err)
      // Process the platform error here
      strErrorMsg = String.Concat("ErrorMessage: ", err.Message, " ",
                      err.Detail.OuterXml, " Source: ", err.Source);

© 2005 Microsoft Corporation. All rights reserved.