Update owner in Plugin

Here's a code snippet that will help you to update the owner of a record in a plug-in. 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("476E234C-5E15-DE11-80BA-000C297AF856");

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

// 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
}


Have fun copy and pasting :)

11 comments:

Anonymous said...

Hi,
I am trying to use this code to assign a custom entity. The custom entity is called "book" so i am using TargetOwnedbook but i am getting an error on it, i also tried TargetOwnednew_book but i still get the error.
What should be done?
Thank you

Ronald Lemmen said...

Hi,

You can use the dynamic entity. Here's a small code snippet that should help you:

TargetOwnedDynamic dyn = new TargetOwnedDynamic();
dyn.EntityName = "new_book";
dyn.EntityId = id;

With kind regards,
Ronald Lemmen

Anonymous said...

Thank you Ronald! :)

Ronald Lemmen said...

That would be one beer please ;)

z.burak güven said...

Hi,

I got a question, i hope you could help me. I have been trying to share a case record
according to the modifiedBy user's team. If user double click on the case record and
change the owner of the case; my assing plugin works fine, so i can share the record
with user's team. But on the main case list grid, if user selects more than one records
and tries to assign the selected cases to a user, my plugin doesn't work. I mean kind of bulk operation.
You got any ideas ? Thanks a lot...

Ronald Lemmen said...

Hi z.burak güven,

Have you tried running your plugin on the child pipeline? Most likely your plugin will be called at that moment. Keep in mind that if this is indeed the solution, that this will impact the way you need to instantiate the webservice.

Kind regards,
Ronald

z.burak güven said...

Hi,

Thanks for advice. I am trying to handle assign message on the pluginRegistrationTool and when i try to change pipeline execution from parent to child it says only create, update and delete operations can work on the child pipeline. So i can't fire assign message on the child pipeLine. I also tried to fire my event as asynchronous. But it didn't make it. Any more advices ?

joon said...

Hi Ronald,
I am using BizTalk server. I can create record in a custom entity with desired Owner Id but cannot update the Owner.
I used type as systemuser and set the value of Owner Id field with the GUID of the User. Also checked the Role given to the account under which BizTalk is performing the action. It has the "Assign" role.

May be you could help me out with suggestion.

Martin said...

Thanks! Helped a lot - was struggling to update the owner using properties. Just wasn't working.

hug said...

Thanks Ronald this works perfectly

Helen Evans said...

With the existence of the Internet, you are now able to make property searches in just a few clicks and for a few minutes only. Here at Property Tax Record, all you need to do is choose a reliable site that can give you complete and valid information.