通用提取XML从Microsoft Dynamics CRM 2011中的任何实体获取所有属性?
答案 0 :(得分:0)
如果你想要它是通用的,你必须使用反射来遍历成员并动态构建查询xml。 类似的东西:
Type type = TypeOf(Contact);
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
/////here you chain the members for the xml
Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(obj, null));
}
答案 1 :(得分:0)
要从任何实体获取详细信息,请使用以下方法。 在使用此通用Fetchxml生成器之前,请阅读注释和参数说明。
Note : fieldToQuery, operatorForCondition, fieldQueryValue Should have Array Same count to work this function and well Mappep with respective to each other to get desired result parameter name = "entityName" = Name of Entity of which details to fetch. parameter name = "fieldsToSearch" = What all fields you want to fetch, if Sent Null, by default all fields are fetched from entity parameter name = "filterType" = "AND" or "OR" parameter name = "fieldToQuery" = Array of Field name on which to query parameter name = "operatorForCondition" = Array of operator between fieldToQuery(Field Name) and fieldQueryValue(Field Value) like "eq, like" parameter name = "fieldQueryValue" = Array of Field Value respective to fieldToQuery (Field Name) by which to query
此处的功能统计
`
public static Microsoft.Xrm.Sdk.EntityCollection RetrieveEntityDetailsByFetchXml(string entityName,string [] fieldsToSearch,string filterType,string [] fieldToQuery,string [] operatorForCondition,string [] fieldQueryValue)
{
string _fetchDetailsXml = @"<fetch version='1.0' mapping='logical' output-format='xml-platform'> <entity name='" + entityName + "'> ";
_fetchDetailsXml = GenerateFetchXMLForAttributes(fieldsToSearch, _fetchDetailsXml);
_fetchDetailsXml += "<filter type='" + filterType + "'>";
if (fieldQueryValue.Count() != fieldToQuery.Count() && fieldToQuery.Count() != operatorForCondition.Count())
{
throw new ApplicationException("FieldtoQuery and FieldQueryValue are not mapped correctly fieldToQuery : " + fieldToQuery.Count() + " fieldQueryValue : " + fieldQueryValue.Count() + ".");
}
_fetchDetailsXml = GenerateFetchXMLForConditions(fieldToQuery, operatorForCondition, fieldQueryValue, _fetchDetailsXml);
_fetchDetailsXml += "</filter> </entity> </fetch>";
Microsoft.Xrm.Sdk.EntityCollection _entityDetailsColl = CrmConnectionsManager.OrganizationServiceProxy.RetrieveMultiple(new FetchExpression(_fetchDetailsXml));
// Note : "_entityDetailsColl" cast this object to respective Entity object
if (_entityDetailsColl != null && _entityDetailsColl.Entities.Count() > 0)
{
return _entityDetailsColl;
}
return null;
} `
`
public static string GenerateFetchXMLForAttributes(string[] fieldsToSearch, string fetchXml)
{
if (fieldsToSearch != null && fieldsToSearch.Count() > 0)
{
foreach (string field in fieldsToSearch)
{
fetchXml += "<attribute name='" + field + "' />";
}
}
else
{
fetchXml += "<all-attributes/>";
}
return fetchXml;
}
public static string GenerateFetchXMLForConditions(string[]fieldToQuery,string[] operatorForCondition, string[] fieldQueryValue, string _fetchDetailsXml)
{
if (fieldToQuery != null && fieldToQuery.Count() > 0)
{
for (int count = 0; count < fieldToQuery.Count(); count++)
{
_fetchDetailsXml += "<condition attribute='" + fieldToQuery[count] + "' operator='" + operatorForCondition[count] + "' value='" + fieldQueryValue[count] + "' uiname='' uitype='' /> ";
}
}
else
{
_fetchDetailsXml += "<all-attributes/>";
}
return _fetchDetailsXml;
}
`
答案 2 :(得分:0)
检索“所有属性”在计算上比仅检索您需要的那些(也考虑IO成本)更昂贵。如果您正在寻找一种查看属性的方法,那么请为您需要的代码编写代码,试试这个:
在CRM网络GUI中:
1.导航到显示相关记录的视图 2.单击功能区栏中的“高级查找”按钮 3.配置“查找”,直到显示您要查找的记录 4.单击功能区栏中的“下载获取XML”按钮 5.使用文本查看器(或您喜欢的开发工具)打开文件
如果您想直接使用此XML,可以考虑:
使用FetchXML构建查询
http://msdn.microsoft.com/en-us/library/gg328117.aspx