Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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

Example

[C#]

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;
   try
   {
      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,
                                              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);
   }
}


© 2005 Microsoft Corporation. All rights reserved.


Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.