ublic class DynamicEntityHowTo
{
public DynamicEntityHowTo()
{
}
public bool Run()
{
#region Setup Data Required for this Sample
bool success = false;
#endregion
try
{
CrmService service = new CrmService();
service.Url = "http://localhost/mscrmservices/2006/crmservice.asmx";
// Pass in a value for the SOAP Header attribute.
// This is the user that I want to impersonate.
service.CallerIdValue = new CallerId();
service.CallerIdValue.CallerGuid =new Guid("2B951FBC-1C56-4430-B23B-20A1349068F3");
// Set the credentials to that of the current process.
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
#region Create Contact Dynamically
// Set the properties of the contact using property objects.
StringProperty firstname = new StringProperty();
firstname.Name = "firstname";
firstname.Value = "Jesper";
StringProperty lastname = new StringProperty();
lastname.Name = "lastname";
lastname.Value = "Aaberg";
// Create the DynamicEntity object.
DynamicEntity contactEntity = new DynamicEntity();
// Set the name of the entity type.
contactEntity.Name = EntityName.contact.ToString();
// Set the properties of the contact.
contactEntity.Properties = new Property[] {firstname, lastname};
// Create the target.
TargetCreateDynamic targetCreate = new TargetCreateDynamic();
targetCreate.Entity = contactEntity;
// Create the request object.
CreateRequest create = new CreateRequest();
// Set the properties of the request object.
create.Target = targetCreate;
// Execute the request.
CreateResponse created = (CreateResponse) service.Execute(create);
#endregion
#region Retrieve Contact Dynamically
// Create the retrieve target.
TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();
// Set the properties of the target.
targetRetrieve.EntityName = EntityName.contact.ToString();
targetRetrieve.EntityId = created.id;
// Create the request object.
RetrieveRequest retrieve = new RetrieveRequest();
// Set the properties of the request object.
retrieve.Target = targetRetrieve;
retrieve.ColumnSet = new AllColumns();
// Indicates that the BusinessEntity should be retrieved as a DynamicEntity.
retrieve.ReturnDynamicEntities = true;
// Execute the request.
RetrieveResponse retrieved = (RetrieveResponse) service.Execute(retrieve);
// Extract the DynamicEntity from the request.
DynamicEntity entity = (DynamicEntity)retrieved.BusinessEntity;
// Extract the fullname from the dynamic entity.
string fullname;
for (int i = 0; i < entity.Properties.Length; i++)
{
if (entity.Properties
![Idea [I]](/cs/emoticons/emotion-55.gif)
.Name.ToLower() == "fullname")
{
StringProperty property = (StringProperty) entity.Properties
![Idea [I]](/cs/emoticons/emotion-55.gif)
;
fullname = property.Value;
break;
}
}
#endregion
#region check success
// bool success = false;
if (retrieved.BusinessEntity is DynamicEntity)
{
success = true;
}
#endregion
#region Remove Data Required for this Sample
TargetDeleteContact targetDelete = new TargetDeleteContact();
targetDelete.EntityId = created.id;
DeleteRequest delete = new DeleteRequest();
delete.Target = targetDelete;
service.Execute(delete);
#endregion
}
catch (System.Web.Services.Protocols.SoapException ex)
{
return false;
}
return success;
}
}
Μήπως έχει δοκιμάσει με Visual Studio 2005?
Ευχαριστώ πολύ
Θρυλικός Προγραμματιστής