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

1

4

Here is default Grid Layout and FetchXml of this view

2

5.PNG

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();
xml1.LoadXml(viewData.Entities[0][“fetchxml”].ToString());
XmlNode entityNode= xml1.SelectSingleNode(“//entity”);
var newAttrnode= xml1.CreateElement(“attribute”);
var nameAttribute = xml1.CreateAttribute(“name”);
nameAttribute.Value = “accountnumber”;
newAttrnode.Attributes.Append(nameAttribute);
entityNode.AppendChild(newAttrnode);
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();
gridxml.LoadXml(viewData.Entities[0][“layoutxml”].ToString());
XmlNode rowNode = gridxml.SelectSingleNode(“//row”);
var newrowAttrnode = gridxml.CreateElement(“cell”);
var accountname = gridxml.CreateAttribute(“name”);
accountname.Value = “accountnumber”;
newrowAttrnode.Attributes.Append(accountname);
var width = gridxml.CreateAttribute(“width”);
width.Value = “150”;
newrowAttrnode.Attributes.Append(width);
rowNode.AppendChild(newrowAttrnode);
dar.Entities[0][“layoutxml”] = gridxml.OuterXml;
_service.Update(dar.Entities[0]);

 

//Publish Account Entity
PublishXmlRequest puxml = new PublishXmlRequest()
{
ParameterXml = “<importexportxml><entities><entity>account</entity></entities></importexportxml>”
};
_service.Execute(puxml);

 

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

7

6

Thanks for visiting my blog.

Do some creative stuff in CRM  !!!  🙂

 

 

 

 

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s