Creating a Query 1
This sample creates a query that will find all the accounts for which the user has read access.
Class Reference
Schema Reference
- account.xsd
- savedquery.xsd
- Fetch XML Schema
Example
[C#]
public void CreateQuery()
{
// 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 is "select * from Account" and will return all
// accounts for which you have read access
String strFetchXml = String.Concat("<fetchxml><fetch><",
"entity name='Account'",
"><all-attributes /></entity",
"></fetch></fetchxml>");
// This is the XML data used for creating a query
StringBuilder queryXml = new StringBuilder("<savedquery>");
queryXml.Append("<name>My Query</name>");
queryXml.Append("<returnedtypecode>0</returnedtypecode>");
queryXml.Append("<iscustomizable>0</iscustomizable>");
queryXml.Append("<isdefault>0</isdefault>");
queryXml.Append("<isquickfindquery>0</isquickfindquery>");
queryXml.Append("<querytype>");
queryXml.Append
(Microsoft.Crm.Platform.Types.SAVED_QUERY_TYPE.MAIN_APPLICATION_VIEW.ToString());
queryXml.Append("</querytype>");
queryXml.Append("<isuserdefined>0</isuserdefined>");
queryXml.Append(fetchXml);
queryXml.Append("</savedquery>");
// Create the query
String queryId = query.Create(userAuth,
Microsoft.Crm.Platform.Proxy.ObjectType.otAccount,
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);
}
}