Update FetchXml and Grid layout of Entity View using C# in CRM

Update FetchXml and Grid layout of Entity View using C# in CRM

In this post, I am going to tell you how we can update Entity view programmatically using c#.

Before starting let’s cover entity view and their query types.

What is Entity View ?

Entity views are special SavedQuery that retrieve data by using a specific filter. They also contain information about how the data in the view should be displayed in the application.

An Entity view is different from a UserQuery. A user query, called a Saved view in the application, is owned by an individual user, can be assigned and shared with other users, and can be viewed by other users depending on the query’s access privileges.

Let’s create a new view in account entity i.e. My Custom Accounts



Here is default Grid Layout and FetchXml of this view



Now we write some code for getting fetchXml and layoutXml for this view.


QueryExpression qe = new QueryExpression(“savedquery“);
qe.Criteria.AddCondition(“returnedtypecode”, ConditionOperator.Equal, “account“);
qe.Criteria.AddCondition(“name”, ConditionOperator.Equal, “My Custom Accounts“);
qe.ColumnSet = new ColumnSet(true);
var viewData= _service.RetrieveMultiple(qe);


For our demo purpose we’re trying to add Account Number in FetchXml.

You can also add filter or some complex query which we want to add but can’t add using Advanced find.


//Update fetchxml of view
XmlDocument xml1 = new XmlDocument();
XmlNode entityNode= xml1.SelectSingleNode(“//entity”);
var newAttrnode= xml1.CreateElement(“attribute”);
var nameAttribute = xml1.CreateAttribute(“name”);
nameAttribute.Value = “accountnumber”;
dar.Entities[0][“fetchxml”] = xml1.OuterXml;


Using below code ,we’re trying to add accountnumber column in our view grid.


//Update Gridxml of view
XmlDocument gridxml = new XmlDocument();
XmlNode rowNode = gridxml.SelectSingleNode(“//row”);
var newrowAttrnode = gridxml.CreateElement(“cell”);
var accountname = gridxml.CreateAttribute(“name”);
accountname.Value = “accountnumber”;
var width = gridxml.CreateAttribute(“width”);
width.Value = “150”;
dar.Entities[0][“layoutxml”] = gridxml.OuterXml;


//Publish Account Entity
PublishXmlRequest puxml = new PublishXmlRequest()
ParameterXml = “<importexportxml><entities><entity>account</entity></entities></importexportxml>”


That’s it, our custom view successfully updated with Account Number Column. 🙂



Thanks for visiting my blog.

Do some creative stuff in CRM  !!!  🙂






Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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