Retrieve CRM Views Records using c# Plugin

Hi ,

If we need to retrieve CRM Records based on Entity Views in Plugin (c#).

  • In CRM we have 2 tables , saved Query , User Query.

In Saved Query

  • Default Entity Based query
  • [ Eg: Account Entity à Views Active Views , In Active Views ,Etc.].
  • My Personal Views [Eg: User Personal Views ]

In User Query

  • User based views , Team Based Views.
  • In both the entity [ saved Query , User Query ] we have following fields
  • View Id = Unique View Id for both entity [ saved Query , User Query]
  • View Name = Name of the view
  • Fetchxml = A string field which stores the fetch xml query where we need add condition etc.
  • Query Type = Type of the query [eg 0 , 2]
  • Sample Code . .

Retrieve  saved Query Records

Guid ViewId = new Guid("GUID OF VIEW");

var savedQueryFetchXml = (from q in orgContext.CreateQuery("savedquery")

where (Guid)q["savedqueryid"] == ViewId // We can pass Name of the view [Eg:(string)q["name"] = "VIEW NAME"
&& (string)q["fetchxml"] != null

select q["fetchxml"]).FirstOrDefault();



Retrieve  User Query Records

var UserQueryFetchXml = (from q in orgContext.CreateQuery("userquery")

where (Guid)q["userqueryid"] == ViewId
&& (string)q["fetchxml"] != null

select q["fetchxml"]).FirstOrDefault();

Thanks 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s