Let say we need to make use of CrmService to give us the name of the opportunity where owner’s fullname=’someName’;
Name is an attribute of Opportunity Entity.
FullName is an attribute of SystemUser Entity.
So we need to make use of LinkEntity class over here to link from opportunity entity to systemuser entity.
Now let’s use queryExpression for the same.
But plzzzz don’t write it yourself, use FetchXmlBuilder to write it for you.
Plzz understand it and download it from this link
http://www.stunnware.com/crm2/topic.aspx?id=FindingData6
(The best site for all the CrmDevelopers)
This is how the queryExpression code will look like
QueryExpression query = new QueryExpression();
query.EntityName = “opportunity”;
ColumnSet columns = new ColumnSet();
columns.Attributes = new string[] { “name” };
query.ColumnSet = columns;
LinkEntity linkEntity1 = new LinkEntity();
linkEntity1.JoinOperator = JoinOperator.Natural;
linkEntity1.LinkFromEntityName = “opportunity”;
linkEntity1.LinkFromAttributeName = “owninguser”;
linkEntity1.LinkToEntityName = “systemuser”;
linkEntity1.LinkToAttributeName = “systemuserid”;
linkEntity1.LinkCriteria = new FilterExpression();
linkEntity1.LinkCriteria.FilterOperator = LogicalOperator.And;
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = “fullname”;
condition1.Operator = ConditionOperator.Equal;
condition1.Values = new object[] { “Nishant Rana” };
linkEntity1.LinkCriteria.Conditions = new ConditionExpression[] { condition1 };
query.LinkEntities = new LinkEntity[] { linkEntity1 };
Now say we have a requirement where we need
The name of all the opportunity along with the fullname of the owner.
Let’s try doing it with a queryExpression class and using the FetchXmlBuilder.
This is the code which we’ll get
………….
LinkEntity linkEntity1 = new LinkEntity();
linkEntity1.JoinOperator = JoinOperator.Natural;
linkEntity1.LinkFromEntityName = “opportunity”;
linkEntity1.LinkFromAttributeName = “owninguser”;
linkEntity1.LinkToEntityName = “systemuser”;
linkEntity1.LinkToAttributeName = “systemuserid”;
//You have specified columns for this link-entity. This is not supported in query expressions
This means it’s not possible using queryExpression
So in this case we need to make use of FetchXml to fetch use the fullname of the owning user.
The fetch xml looks like this
string fetchXml = @”
<fetch mapping=””logical”” count=””50″”>
<entity name=””opportunity””>
<attribute name=””name”” />
<link-entity name=””systemuser”” from=””systemuserid”” to=””owninguser””>
<attribute name=””fullname”” />
</link-entity>
</entity>
</fetch>”;
// Finally the code
CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
string fetchXml = @”
<fetch mapping=””logical”” count=””50″”>
<entity name=””opportunity””>
<attribute name=””name”” />
<link-entity name=””systemuser”” from=””systemuserid”” to=””owninguser””>
<attribute name=””fullname”” />
</link-entity>
</entity>
</fetch>”;
String result = service.Fetch(fetchXml);
Now the only problem is we need to parse the xml on our own.
We can use something like this
if this is the result we are receiving
<resultset morerecords=”0″><result><new_id>GP0677</new_id></result></resultset>
to fetch the new_id we can do this
XmlDocument doc = new XmlDocument();
doc.LoadXml(result);
XmlNodeList xnodlist=doc.GetElementsByTagName(“new_id”);
XmlNode xnodRoot=xnodlist.Item(0);
string val=xnodRoot.InnerText;
Bye.