May 18, 2007

How to set or change the owner of the dynamic entity

First things first: The user has to have the rights to change the owner of the entity. I had some problems with the code from SDK. You can find it on two places, but only one works.

If you want to create an entity with the other owner first create the owner:

Owner owner = new Owner();
owner.type = EntityName.systemuser.ToString();
owner.Value = newEntityOwnerId;

Then add the owner to the dynamic entity properties array together with other properties and execute the request:

//Create dynamic entity
DynamicEntity entity = new DynamicEntity();
entity.Name = entityName;
entity.Properties = properties;
//Create request
TargetCreateDynamic myTarget = new TargetCreateDynamic();
myTarget.Entity = entity;
CreateRequest create = new CreateRequest();
create.Target = myTarget;
//Execute the request
CreateResponse response = (CreateResponse)_crmService.Execute(create);

In the response object you have the ID of the newly created entity.


 


If you just want to assign (change) the owner of the use this code:

// Create the SecurityPrincipal object.
SecurityPrincipal assignee = new SecurityPrincipal();

// Set the properties of the SecurityPrincipal object.'
// PrincipalId is a GUID that identifies the user or team
// that will own this record.
assignee.PrincipalId = userId;

// Create the target object for the request.
TargetOwnedDynamic target = new TargetOwnedDynamic();

// Set the properties of the target object'.
// EntityId is a GUID that identifies the dynamic entity
// Entity name is the name of the dynamic entity
// that is being assigned to the user.
target.EntityId = dynamicEntityId;
target.EntityName = dynamicEntityName;

// Create the request object.
AssignRequest assign = new AssignRequest();

// Set the properties of the request object.'
assign.Assignee = assignee;
assign.Target = target;

// Execute the request.
AssignResponse assignResponse = (AssignResponse)_crmService.Execute(assign);