Answered by:
How do you work with a customized and new entity ?

Question
-
Hi,
A new entity FOO (with attributes) has been created in the CRM.
How do i access it ? Is there a wrapper class to work with it ?
Is there some sample code that could help me figure it out ?
Thanks for helping.
Friday, August 20, 2010 9:06 AM
Answers
-
Here is sample code to retrieve Custom entity :
CrmService service = new CrmService();
service.Url = "http://<servername>:<port>/MSCrmServices/2007/CrmService.asmx ";
service.Credentials = System.Net.CredentialCache.DefaultCredentials;CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "Organization_Name";service.CrmAuthenticationTokenValue = token;
TargetRetrieveDynamic retDynamic = new TargetRetrieveDynamic();
retDynamic.EntityId = result.new_fooid.Value;
retDynamic.EntityName = "new_foo";// custom entity nameRetrieveRequest retReq = new RetrieveRequest();
retReq.Target = retDynamic;
retReq.ReturnDynamicEntities = true;columns.Attributes = new string[] { "new_attribute1", "new_attribute2" }; //define attributes you want to retrieve
retReq.ColumnSet = columns;
DynamicEntity ent = (DynamicEntity)((RetrieveResponse)service.Execute(retReq)).BusinessEntity;
// access the attributes of custom entity by looping through Proprties of Dynamic Entity and Proper casting.
e.g. Guid _fooId= ((KeyProperty)ent.Properties[i]).Value.Value;
In the same way to create Custom entity, you can use TargetCreateDynamic Request.
OR
add references for microsoft.crm.sdk.dll & microsoft.crm.sdktypeproxy.dll & use above code with some modification using the classes from SDK.
Thank you.
JayshriP.
thanks.In your example, result and columns are undefined.
Can you specify what their types are and where they come from ?
Thanks
It's like :
ColumnSet columns = new ColumnSet();
columns.Attributes = new string[] { "new_attribute1", "new_attribute2" }; //define attributes you want to retrieve
retReq.ColumnSet = columns;
...
& result is Dynamic Entity :
DynamicEntity ent = (DynamicEntity)((RetrieveResponse)service.Execute(retReq)).BusinessEntity;
then loop through properties :
suppose you want to retrieve GUID for your custom entity (say "new_fooid")
for (int i = 0; i < ent.Properties.Length; i++)
{
if (ent.Properties[i].Name.ToLower() == "new_fooid")
{
GUID _fooId= ((KeyProperty)ent.Properties[i]).Value.Value ;
break;
}
}In the same way by using proper casting, you can access other attributes.
Thank you.
JayshriP
- Marked as answer by DavidJennawayMVP, Moderator Tuesday, September 21, 2010 5:00 AM
Friday, August 20, 2010 12:42 PM
All replies
-
you can use your custom entity just like as other OOB entities, you have two options for that either use crm webservice but remember to add reference as below
http://<servername[:port]>/mscrmservices/2007/crmservice.asmx?WSDL&uniquename=organizationName
or use CRM SDK API, but in that you have to use Dynamic entity to access your custom entities.
you can get details about using dynamic entity in CRM SDK
Mahain : http://mahenderpal.wordpress.com- Proposed as answer by HIMBAPModerator Friday, August 20, 2010 9:15 AM
Friday, August 20, 2010 9:14 AMModerator -
you need to download the WSDL file from the CRM server. (for correct ORG)..and update the referance in your C# project as follows
Download WSDL files
1. Log on to CRM (on your ORGB) then go to Settings -> Customization - > Download Web Service Description Files
2. This will display two options crmservice.asmx and metadataservice.asmx.
3. Click on metadataservice.asmx first and it will open up in new window as XML...in this new window click on File --> Save as and save it to particular location as <<name>>.XML
4. follow similair step for crmservice.asmx
Add this as Web Referance in your visual studio project .. as follows
1. Click on Add Web Referance
2. Put the file path in the URL (C:\web services referance\crmService.xml) and then put the web referance name and click ok...call it crmSDK
Updating would be just to right click on web service and select update web Referance..Once you have this web referance then you will able to access this entity and attributes in intellisense and will have all methods similair existing entity (for e.g. account)..
Hope this helps..
MayankP My Blog My twitterFriday, August 20, 2010 9:32 AM -
you need to download the WSDL file from the CRM server. (for correct ORG)..and update the referance in your C# project as follows
Download WSDL files
1. Log on to CRM (on your ORGB) then go to Settings -> Customization - > Download Web Service Description Files
2. This will display two options crmservice.asmx and metadataservice.asmx.
3. Click on metadataservice.asmx first and it will open up in new window as XML...in this new window click on File --> Save as and save it to particular location as <<name>>.XML
4. follow similair step for crmservice.asmx
Add this as Web Referance in your visual studio project .. as follows
1. Click on Add Web Referance
2. Put the file path in the URL (C:\web services referance\crmService.xml) and then put the web referance name and click ok...call it crmSDK
Updating would be just to right click on web service and select update web Referance..Once you have this web referance then you will able to access this entity and attributes in intellisense and will have all methods similair existing entity (for e.g. account)..
Hope this helps..
MayankP My Blog My twitterthanks. Can you copy-paste some sample that shows the declaration and the call of the Web Service for a particular customized entity named FOO for instance ?
Thanks again.
Friday, August 20, 2010 11:32 AM -
First add reference of webservice and then use below code
// Set up the CRM Service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "AdventureWorksCycle"; //Remember to change organization name
CrmService service = new CrmService();
service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx"; //Remember to change this URL
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;//creaet your entity object
Foo _FooObj=new Foo();
Mahain : http://mahenderpal.wordpress.comFriday, August 20, 2010 11:37 AMModerator -
Here is sample code to retrieve Custom entity :
CrmService service = new CrmService();
service.Url = "http://<servername>:<port>/MSCrmServices/2007/CrmService.asmx";
service.Credentials = System.Net.CredentialCache.DefaultCredentials;CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "Organization_Name";service.CrmAuthenticationTokenValue = token;
TargetRetrieveDynamic retDynamic = new TargetRetrieveDynamic();
retDynamic.EntityId = result.new_fooid.Value;
retDynamic.EntityName = "new_foo";// custom entity nameRetrieveRequest retReq = new RetrieveRequest();
retReq.Target = retDynamic;
retReq.ReturnDynamicEntities = true;columns.Attributes = new string[] { "new_attribute1", "new_attribute2" }; //define attributes you want to retrieve
retReq.ColumnSet = columns;
DynamicEntity ent = (DynamicEntity)((RetrieveResponse)service.Execute(retReq)).BusinessEntity;
// access the attributes of custom entity by looping through Proprties of Dynamic Entity and Proper casting.
e.g. Guid _fooId= ((KeyProperty)ent.Properties[i]).Value.Value;
In the same way to create Custom entity, you can use TargetCreateDynamic Request.
OR
add references for microsoft.crm.sdk.dll & microsoft.crm.sdktypeproxy.dll & use above code with some modification using the classes from SDK.
Thank you.
JayshriP.
- Proposed as answer by JayshriP Friday, August 20, 2010 11:57 AM
Friday, August 20, 2010 11:53 AM -
//creaet your entity object
Foo _FooObj=new Foo();
Mahain : http://mahenderpal.wordpress.comThanks.
However, after adding the web services to the projet, i still need to use some namespace to be able to declare and use Foo in my class.
Else i get an error message at compilation time of course that says :
"The type or namespace "Foo" could not be found.
What am i missing ?
Thanks again for your precious help.
Friday, August 20, 2010 11:53 AM -
//creaet your entity object
Foo _FooObj=new Foo();
Mahain : http://mahenderpal.wordpress.comThanks.
However, after adding the web services to the projet, i still need to use some namespace to be able to declare and use Foo in my class.
Else i get an error message at compilation time of course that says :
"The type or namespace "Foo" could not be found.
What am i missing ?
Thanks again for your precious help.
If you are using CRM Web service then you will get "Foo" directly but if you are using CRM SDK then you need to use Dynamic Entity to handle your custom entity.Thank you.
JayshriP.
Friday, August 20, 2010 12:00 PM -
Here is sample code to retrieve Custom entity :
CrmService service = new CrmService();
service.Url = "http://<servername>:<port>/MSCrmServices/2007/CrmService.asmx ";
service.Credentials = System.Net.CredentialCache.DefaultCredentials;CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "Organization_Name";service.CrmAuthenticationTokenValue = token;
TargetRetrieveDynamic retDynamic = new TargetRetrieveDynamic();
retDynamic.EntityId = result.new_fooid.Value;
retDynamic.EntityName = "new_foo";// custom entity nameRetrieveRequest retReq = new RetrieveRequest();
retReq.Target = retDynamic;
retReq.ReturnDynamicEntities = true;columns.Attributes = new string[] { "new_attribute1", "new_attribute2" }; //define attributes you want to retrieve
retReq.ColumnSet = columns;
DynamicEntity ent = (DynamicEntity)((RetrieveResponse)service.Execute(retReq)).BusinessEntity;
// access the attributes of custom entity by looping through Proprties of Dynamic Entity and Proper casting.
e.g. Guid _fooId= ((KeyProperty)ent.Properties[i]).Value.Value;
In the same way to create Custom entity, you can use TargetCreateDynamic Request.
OR
add references for microsoft.crm.sdk.dll & microsoft.crm.sdktypeproxy.dll & use above code with some modification using the classes from SDK.
Thank you.
JayshriP.
thanks.In your example, result and columns are undefined.
Can you specify what their types are and where they come from ?
Thanks
Friday, August 20, 2010 12:23 PM -
Better to tell use what are the column in your custom entity that you want to use with their types so that we can give sample code for the same
Mahain : http://mahenderpal.wordpress.comFriday, August 20, 2010 12:30 PMModerator -
Hi
You need to add a reference to your web service in the class after you have added the above WSDL to your web reference.
using <Your Web Ref Name>;
ex. using CrmSdk;
Friday, August 20, 2010 12:35 PM -
Here is sample code to retrieve Custom entity :
CrmService service = new CrmService();
service.Url = "http://<servername>:<port>/MSCrmServices/2007/CrmService.asmx ";
service.Credentials = System.Net.CredentialCache.DefaultCredentials;CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "Organization_Name";service.CrmAuthenticationTokenValue = token;
TargetRetrieveDynamic retDynamic = new TargetRetrieveDynamic();
retDynamic.EntityId = result.new_fooid.Value;
retDynamic.EntityName = "new_foo";// custom entity nameRetrieveRequest retReq = new RetrieveRequest();
retReq.Target = retDynamic;
retReq.ReturnDynamicEntities = true;columns.Attributes = new string[] { "new_attribute1", "new_attribute2" }; //define attributes you want to retrieve
retReq.ColumnSet = columns;
DynamicEntity ent = (DynamicEntity)((RetrieveResponse)service.Execute(retReq)).BusinessEntity;
// access the attributes of custom entity by looping through Proprties of Dynamic Entity and Proper casting.
e.g. Guid _fooId= ((KeyProperty)ent.Properties[i]).Value.Value;
In the same way to create Custom entity, you can use TargetCreateDynamic Request.
OR
add references for microsoft.crm.sdk.dll & microsoft.crm.sdktypeproxy.dll & use above code with some modification using the classes from SDK.
Thank you.
JayshriP.
thanks.In your example, result and columns are undefined.
Can you specify what their types are and where they come from ?
Thanks
It's like :
ColumnSet columns = new ColumnSet();
columns.Attributes = new string[] { "new_attribute1", "new_attribute2" }; //define attributes you want to retrieve
retReq.ColumnSet = columns;
...
& result is Dynamic Entity :
DynamicEntity ent = (DynamicEntity)((RetrieveResponse)service.Execute(retReq)).BusinessEntity;
then loop through properties :
suppose you want to retrieve GUID for your custom entity (say "new_fooid")
for (int i = 0; i < ent.Properties.Length; i++)
{
if (ent.Properties[i].Name.ToLower() == "new_fooid")
{
GUID _fooId= ((KeyProperty)ent.Properties[i]).Value.Value ;
break;
}
}In the same way by using proper casting, you can access other attributes.
Thank you.
JayshriP
- Marked as answer by DavidJennawayMVP, Moderator Tuesday, September 21, 2010 5:00 AM
Friday, August 20, 2010 12:42 PM