Creating a Query 2

CRM 1.0

This sample does not create a query, but executes a query to find all the accounts for which the user has read access.

Class Reference

Schema Reference



public void SimpleQuery()
   // 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 is "select * from Account" and will return
      // all accounts for which you have read access
      StringBuilder queryXml = new StringBuilder("<fetch mapping='logical'>");
      queryXml.Append("<entity name='Account'>");
      queryXml.Append("<all-attributes /></entity></fetch>");

      // This should 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.