Schlagwort-Archive: Plugins

Using HttpUtility.UrlEncode in a plugin leads to a SecurityException

In a recent project I had to develop a plugin which is executed in the plugin sandbox. The sandbox restricts the rights plugins to a ‘safe’ amount. See http://msdn.microsoft.com/en-us/library/gg334752.aspx for a description. According to the SDK, web request are allowed.

Part of my task was to send a request to a web service which expects data URL-encoded. Normally you would use following class (part of System.Web)

HttpUtility.UrlEncode("some data here");

I was surprised that this leads to a SecurityException if used in a sand-boxed plugin. The sandbox does not allow the use of this class.

Instead of rewriting the method (I hate reinventing the wheel…) I’ve searched for a replacement, and found

Uri.EscapeDataString("some data here");

which is usable in sand-boxed plugins. For my task the result was the same as with UrlEncode, however I am not sure I they are different in their results.

Howto: Use pre-generated Guids for records

A customer asked me recently if it is possible to get the id of a record in a pre-create plugin. By default, it is not possible because the record is not yet created at this point and has not got an id.

However, Dynamics CRM allows you to create your own id for a record. The primary key of a record is stored in its Key-Property. On all default entities, for example account, the id property is marked as Valid for create (see accountid). This means, the sdk allows you to pass your own id.

using System;
using Microsoft.Crm.Sdk;
 
namespace PreCreateKeyPlugin
{
    public class PreCreateKeyPlugin : IPlugin
    {
        public void Execute(IPluginExecutionContext context)
        {
            if (IsPreCreate(context))
            {
                if (context.InputParameters.Contains(ParameterName.Target))
                {
                    DynamicEntity target = context.InputParameters[ParameterName.Target] as DynamicEntity;
 
                    if (target != null)
                    {
                        Guid customId = new Guid("{d7256b93-a5b5-45f9-9f2d-a1838279c35c}");
 
                        target["accountid"] = new Key(customId);
                    }
                }
            }
        }
 
        private bool IsPreCreate(IPluginExecutionContext context)
        {
            return  context.MessageName == MessageName.Create && 
                    context.Stage == MessageProcessingStage.BeforeMainOperationOutsideTransaction;
        }
    }
}

If you register this plugin for pre-create on the account entity and create a new account, it will have the provided id.

Please keep in mind, that you are in the responsibility to assign an new GUID for every execution of this plugin, but this should be clear when working with primary keys.

Who triggered my plugin?

Do you ever want to know who triggered the execution of a plugin? This knowledge is handy, if you have to do some operation if the user entered data, but not if an external application made an update with the SDK.

The IPluginExecutionContext contains the property CallerOrigin which will tell us more about the origin which triggered the plugin execution.

The CallerOrigin is populated with one of these four types

  • Application – The user has triggered the event (by creating or editing a record).
  • AsyncService – The async service triggered the event (by workflow execution).
  • WebServiceApi – An application which makes use of the crm web service triggered the event.
  • OfflineOrigin – The event was triggered by replaying offline actions while going online.

This sample shows how to make use of this property (the sample is part of the sdk article Online vs. Offline Plug-ins)

using System;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
 
public class OnlinePlugin : IPlugin
{
   public void Execute(IPluginExecutionContext context)
   {
      // Check to see if this is a playback context.
      CallerOrigin callerOrigin = context.CallerOrigin;
 
      if (callerOrigin is OfflineOrigin)
      {
         // This plug-in was fired from the playback queue after the user
         // selected to go online within Microsoft Dynamics CRM for Outlook.
         return;
      }
      else
      {
         // Do something here.
      }
   }
}