Reputation: 151
I followed this link "https://prabirchoudhury.wordpress.com/2013/06/13/retrieve-microsoft-crm-2011-data-using-fetchxml-and-web-service/" and connected to CRM.
On that link below is the code to connect to user created View.
string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' count='100'>
<entity name='account'>
<attribute name='ownerid' />
<attribute name='name' />
<attribute name='accountnumber' />
<attribute name='accountid' />
<order attribute='accountnumber' descending='true' />
<link-entity name='systemuser' to='owninguser' alias='mainuser'>
<attribute name='systemuserid'/>
<attribute name='domainname' />
<attribute name='fullname' />
</link-entity >
</entity>
</fetch>";
If you see I had to pass XML code to get data from CRM, however I just want to pass view name and get the data in the form of csv file instead of passing XML code. I have done the same in Powershell but I need to do the same in C# code.
The reason I want to just pass view name is because tomorrow user will do any modification in the view and I don't have to touch the code if the user makes any changes so I need to make this dynamic, secondly after getting the data how can I create csv file on buffer(without creating physically on machine)and then export to any cloud based service.
Upvotes: 0
Views: 692
Reputation: 3935
Here's a method that will get you the FetchXML from a saved view, based on the name of the view and the name of the entity:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
using System.Linq;
private string getFetchXml(IOrganizationService svc)
{
var query = new QueryExpression
{
EntityName = "userquery",
ColumnSet = new ColumnSet("userqueryid", "name", "fetchxml"),
Criteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "name",
Operator = ConditionOperator.Equal,
Values = {"My View"}
},
new ConditionExpression
{
AttributeName = "returnedtypecode",
Operator = ConditionOperator.Equal,
Values = { "account" }
}
}
}
};
var result = svc.RetrieveMultiple(query);
var view = result.Entities.FirstOrDefault();
var fetchXml = view.GetAttributeValue<string>("fetchxml");
return fetchXml;
}
Upvotes: 2