Friday, November 23, 2012

Update owner in Plugin MS CRM 2011

This code should run in a POST plug-in:

SecurityPrincipal assignee = new SecurityPrincipal();
assignee.Type = SecurityPrincipalType.User;

// PrincipalId is some known Guid belonging to the user or team that will own this record.
assignee.PrincipalId = new Guid("guid");

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

//use the below code for custom entity
//TargetOwnedDynamic dyn = new TargetOwnedDynamic();
//dyn.EntityName = "new_book";
//dyn.EntityId = id;

// Set the properties of the target object.
// EntityId is some known Guid belonging to the account that is being assigned to the user.
target.EntityId = id;

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

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

// Execute the request.
ICrmService service = context.CreateCrmService(true);
try
{
AssignResponse assignResponse = (AssignResponse)service.Execute(assign);
}
catch (Exception ex)
{
//TODO: Exceptionhandling
}

No comments:

Post a Comment