简体   繁体   中英

CRM 2013 - Plugin Execution (Post Operation) What happens and when?

I have a system configuration entity where I store credentials of a Web Service. I need to encrypt the password field (new_SharePointServicePassword) and I want my plugin to do the encryption.

So, I have created a new assembly with the following events registered.

base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Create", "new_systemconfiguration", new Action<LocalPluginContext>(ExecutePostSystemConfigurationCreate)));
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "new_systemconfiguration", new Action<LocalPluginContext>(ExecutePostSystemConfigurationCreate)));

My plugin code is as below.

string plainTextPassword = SystemConfiguration.new_SharePointServicePassword;
string encryptedPassword;
this.TracingService.Trace("Generating Keys");
Encryption encryption = new Encryption(localContext.PluginExecutionContext.OrganizationId.ToString());
this.TracingService.Trace("Generating Keys - Completed");
if (!string.IsNullOrEmpty(plainTextPassword))
{
    this.TracingService.Trace("Encrypting password");
    encryptedPassword = encryption.Encrypt(plainTextPassword);
    this.TracingService.Trace("Encrypting password - Completed");
    SystemConfiguration.new_SharePointServicePassword = encryptedPassword;
    localContext.OrganizationService.Update(SystemConfiguration);
}

The steps registered against the Plugin are for Post Operation for both Create and Update messages. The Update message is filtered on the new_SharePointServicePassword attribute.

Questions

  1. Why is the call localContext.OrganizationService.Update(SystemConfiguration); required? Is it because, my plugin executes after the changes have been committed to the database?
  2. Can I instead modify the step as Pre Operation on the Update message? I tried this, but my plugin never fired? Why did it not fire?
  3. Is there any way that I can do this better?

I would recommend you to run the plugin on Pre Operation . Because when you enter the password through UI, it will save the unencrypted password first. User will have to refresh to see the encryption. Also if you have auditing enabled on password field. It will show the unencrypted password in Audit log. On Pre Operation you don't need following line:

localContext.OrganizationService.Update(SystemConfiguration);

Make sure that you replace 40 with 20 in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM