AXForum  
Вернуться   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 03.05.2009, 16:05   #1  
Blog bot is offline
Blog bot
Участник
 
25,626 / 848 (80) +++++++
Регистрация: 28.10.2006
mscrm4ever: CRM 4.0 Using attribute mapping in code
Источник: http://mscrm4ever.blogspot.com/2009/...g-in-code.html
==============



This is a quick and dirty way of getting CRM attribute mapping. So what exactly can you do with it? One option would be to use it in order to retrieve parent context information when you use CrmService to create new entities. For example, when you create a new contact you might want to get the account information that would normally map to contact when used from the CRM UI.


One of the challenges of using SDK is to mimic some of the functionalities that exist in the UI e.g. required field and attribute mapping when a child entity is created in the context of a parent entity. This type of mapping does not exist out of the box and requires you to write custom code.


The following example is a generic way to extract mapping information from CRM. So what are the pros and cons of using this technique?
The biggest advantage is that you’re able to you CRM customizations which make the configuration process worth while.
The disadvantages are that the default CRM mapping (attributemap entity) does not hold the type of the attributes e.g picklist or lookup and the information returned form the CRM also contains inner mappings of picklist and lookups e.g. parentcustomeridname and parentcustomriddsc.


So how can we overcome these disadvantages?
The attribute type can easily be retrieved using the metadata service RetrieveAttributeRequest. The problem is that this call to metadataservice is very slow so you also need to cache the data in order to use it efficiently.
The inner mapping can be ignored if you exclude picklist and lookup attributes that end with name of dsc.


The code does not cache the mapping information and does not show you how to create a new contact with parent mapping but presents a poc of how to retrieve the mapping information for further use.


The following is what the program prints to the console:

paymenttermscode --> paymenttermscode - Type: Picklist
telephone1 --> telephone1 - Type: String
accountid --> parentcustomerid - Type: Customer
name --> parentcustomeridname - Type: String
deletionstatecode --> parentcustomeriddsc - Type: Integer
address1_addresstypecode --> address1_addresstypecode - Type: Picklist
address1_city --> address1_city - Type: String
address1_country --> address1_country - Type: String
address1_county --> address1_county - Type: String
address1_line1 --> address1_line1 - Type: String


Here is the code, simply create a new console application and dump the class inside the program.cs file



using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy.Metadata;
using Microsoft.Crm.Sdk.Metadata;

namespace EntityMapping
{
class Program
{
static void Main(string[] args)
{
List AcctContMap = GetAttributeMapping("account", "contact");

foreach(AttributeMapping attributeMap in AcctContMap)
{
Console.WriteLine
(
String.Format
(
"{0} --> {1} - Type: {2}",
attributeMap.FromEntityName,
attributeMap.ToEntityName,
Enum.GetName(typeof(AttributeType),attributeMap.AttributeType)
)
);
}
}

public class AttributeMapping
{
public String FromEntityName;
public String ToEntityName;
public AttributeType AttributeType;

public AttributeMapping(String fromEntityName,
String toEntityName,
AttributeType typeofAttribute)
{
this.FromEntityName = fromEntityName;
this.ToEntityName = toEntityName;
this.AttributeType = typeofAttribute;
}
}

private static List GetAttributeMapping(String fromEntity, String toEntity)
{
DynamicEntity entityMap = GetEntityMapping(fromEntity, toEntity);
List startMap = RetieveAttributeMappingByEntityMap(entityMap);
List endMap = new List(startMap.Count);

foreach (DynamicEntity attributeMap in startMap)
{
String targetAttribute = attributeMap.Properties["targetattributename"].ToString();
String sourceAttribute = attributeMap.Properties["sourceattributename"].ToString();
endMap.Add(
new AttributeMapping(sourceAttribute,targetAttribute,
GetAttributeType(toEntity,targetAttribute))
);
}

return endMap;
}

private static List RetieveAttributeMappingByEntityMap(DynamicEntity entityMap)
{
Guid entityMapId = ((Key)entityMap.Properties["entitymapid"]).Value;
QueryExpression attributeQuery = GetAttributeQuery(entityMapId, new AllColumns());
return RetrieveMultiple(attributeQuery);
}

private static AttributeType GetAttributeType(string entityLogicalName, string attributeLogicalName)
{
RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest();
attributeRequest.EntityLogicalName = entityLogicalName;
attributeRequest.LogicalName = attributeLogicalName;
RetrieveAttributeResponse attributeResponse =
(RetrieveAttributeResponse)GetMetaService("http://moss:5555/","MicrosoftCRM").Execute(attributeRequest);
return attributeResponse.AttributeMetadata.AttributeType.Value;
}

private static MetadataService GetMetaService(string serverUrl, string orgName)
{
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = orgName;

MetadataService service = new MetadataService();
service.Url = String.Format("{0}mscrmservices/2007/metadataservice.asmx", serverUrl);
service.PreAuthenticate = false;
service.UnsafeAuthenticatedConnectionSharing = true;
service.CrmAuthenticationTokenValue = token;
service.UseDefaultCredentials = true;

return service;
}

private static List RetrieveMultiple(QueryExpression query)
{
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.Query = query;
request.ReturnDynamicEntities = true;
RetrieveMultipleResponse response =
(RetrieveMultipleResponse)GetCrmService(
"http://moss:5555/","MicrosoftCRM").Execute(request);
return response.BusinessEntityCollection.BusinessEntities;
}

private static CrmService GetCrmService(string serverUrl, string orgName)
{
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = orgName;

CrmService service = new CrmService();
service.Url = String.Format("{0}mscrmservices/2007/crmservice.asmx",serverUrl);
service.PreAuthenticate = false;
service.UnsafeAuthenticatedConnectionSharing = true;
service.CrmAuthenticationTokenValue = token;
service.UseDefaultCredentials = true;

return service;
}

private static DynamicEntity GetEntityMapping(String fromEntity, String toEntity)
{
QueryExpression query = new QueryExpression(EntityName.entitymap.ToString());
query.ColumnSet.AddColumn("entitymapid");
query.Criteria.AddCondition(
new ConditionExpression("sourceentityname",ConditionOperator.Equal,
new object[]{fromEntity})
);
query.Criteria.AddCondition(
new ConditionExpression("targetentityname",ConditionOperator.Equal,
new object[]{toEntity})
);

List entityMapping = RetrieveMultiple(query);
return entityMapping[0] as DynamicEntity;
}

private static QueryExpression GetAttributeQuery(Guid entityMapId, ColumnSetBase columnSet)
{
QueryExpression query = new QueryExpression(EntityName.attributemap.ToString());
query.ColumnSet = columnSet;
query.Criteria.AddCondition(
new ConditionExpression("entitymapid",ConditionOperator.Equal,entityMapId.ToString())
);
return query;
}
}
}



Источник: http://mscrm4ever.blogspot.com/2009/...g-in-code.html
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
Microsoft Dynamics CRM Team Blog: Building Rich-Client Dashboards for Microsoft Dynamics CRM with Windows Presentation Foundation Blog bot Dynamics CRM: Blogs 1 31.03.2009 13:24
Microsoft Dynamics CRM Team Blog: List Web Part for Microsoft Dynamics CRM 4.0 Deployment Scenarios Blog bot Dynamics CRM: Blogs 0 30.01.2009 22:05
mscrm4ever: CRM 4.0 State and Status code Manager Wizard Blog bot Dynamics CRM: Blogs 0 19.01.2009 02:07
mscrm4ever: CRM 4.0 Supported Multi Select (Picklist) Control Blog bot Dynamics CRM: Blogs 0 25.12.2008 15:05
Microsoft Dynamics CRM Team Blog: Data Migration Manager Tips and Tricks Blog bot Dynamics CRM: Blogs 0 02.09.2008 22:05

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 00:26.