There are times we write utilities to retrieve CRM data/information using C#. My favorite is a .NET console application which I always create for my projects. In this console app I dump useful code snippets which makes my work very productive.
In this blog I will provide C# code to retrieve System and Custom views.
Code:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.Configuration;
using System.Linq;
using System.ServiceModel;
namespace ConsoleUtilities
{
class GetViews
{
public static void Main(string[] args)
{
try
{
CrmServiceClient crmConn = new
CrmServiceClient(ConfigurationManager.ConnectionStrings["CRMConnectionString"].ConnectionString);
IOrganizationService crmService = crmConn.OrganizationServiceProxy;
OrganizationServiceContext orgContext = new OrganizationServiceContext(crmService);
var objCRMViews = from crmView in orgContext.CreateQuery("savedquery")
select new
{
savedQuery_SavedQueryId = crmView["savedqueryid"],
savedQuery_Name = crmView["name"]
};
foreach (var localObjCRMViews in objCRMViews)
{
System.Console.WriteLine("savedQuery_savedqueryid: "
+ localObjCRMViews.savedQuery_SavedQueryId.ToString());
System.Console.WriteLine("savedQuery_name: "
+ localObjCRMViews.savedQuery_Name.ToString());
System.Console.WriteLine();
}
// exit
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
catch (FaultException<OrganizationServiceFault> ex)
{
string message = ex.Message;
throw;
}
}
}
}
Explanation:
1) Use the following types in a namespace.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSdeocIXC8dBbJa5k7fWKda9OVcizRF_tErweL-20Wqt9FexCORMBbYxxaffyhoNH1CzBeq-Ej120Vwxz7RkdRAOkjsQXDMt1dMOnqKq3Pf8XXXuvB_E9ZSWLjX8TrbNuyshbxwy_xNVI/s1600/17.PNG) |
"using" Directive - Namespace |
2) Connect to Dynamics 365 V 9.0 online or on premise. Write a LINQ query to retrieve all views. Loop through and display all the views. At the end exit the console app.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvDZD_ZU0SDI4Kj2N7q6mc-sLXV2jO2stJjzLQEtyvvfqRGtfUIbf9pEMm7DequlGWX8SEb_axLDUMpEGfueek8Jr1AUaZkM7kqBGgE0wMm_nHgqwTZUHgYsLZusMUbEm6BJcj4u07tmM/s640/18.PNG) |
LINQ Query to Retrieve All Views |
You can also add a "Where" clause to the LINQ query to retrieve only one view. In my case I have added the where clause to retrieve a view named "Payments with No Taxes".
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiA6MBKqENJDabblcbU0XrH89pDsSZ9FA2O6tn8DmmJA0jz5RO9gPfLNW78vFKsIIK_RXSsu5g2ze-Ewfz1e7PYnKcD0nRLcarMZy788nmMXKKJOyFtzg9vtqLRtaGMv75VR4OvqeixJhQ/s1600/19.PNG) |
LINQ Query to Retrieve One View |
3) Add Try/Catch to handle any exceptions.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiywxitl79NTeZBbmD3KfrZbuudvm6hE06ajpDxnSEva4KS0zrpmA_A2D6hTxbkG_9yb8Fz_9lItPkR8aA-SwhulUcdb6qHqkRi8_6WqQ_hCZei_M_E4JEOv_29EJX7Q9_7-Xcp-_oBP2o/s1600/20.PNG) |
Try/Catch |
You can use this utility to get a list of all views or get information on just one view. This is useful when developing and customizing a CRM application.