9

I need to have some code execute on the click of the ribbon button on an entity that updates some related data from CRM Online 2011. I would prefer not to have to write all of this logic in JScript. Is there a way to call into C# code from JScript? I have looked at the JScript file Microsoft is using from the ribbon and it looks like this:

Mscrm.Campaign.copyCampaign = function (campaignId, saveAsTemplate) {
    var $v_0 = new RemoteCommand("MarketingAutomation", "CopyCampaign", null);
    $v_0.SetParameter("campaignId", campaignId);
    $v_0.SetParameter("saveAsTemplate", saveAsTemplate.toString());
    var $v_1 = $v_0.Execute(),
        $v_2 = $v_1.ReturnValue;
    openObj(Mscrm.EntityTypeCode.Campaign, $v_2, null, null, Mscrm.NavigationMode.NavigationModeInline, null);
    Mscrm.Utilities.refreshParentGrid(Mscrm.EntityTypeCode.Campaign, campaignId)
};

I see a RemoteCommand call being placed that I assume is going back to some web service function. I am hoping to do something like this. Can I add my own web service functions?

I was able to make a call from JScript to send an "Assign" message for an entity just as a test. This potentially could work with a plugin-in, but it seems odd to send a message for an execution event that did not actually occur just to run some C# code ("Assign", "Update", etc.), and I don't see a way to create your own messages.

Matt
  • 4,656
  • 1
  • 22
  • 32
Chad
  • 489
  • 1
  • 6
  • 16

5 Answers5

10

You can't add your own web services or create your own custom plugin messages in 2011. The best way we've found of doing this is to:

  1. Set up a entity that exists exclusively to run custom code on the server.
  2. Give it attributes called message name and another property to pass in parameters in whatever format you choose (XML, JSON, etc.)
  3. From your JavaScript, create an instance of that entity passing in the correct parameters.
  4. Have a plugin attached to the Create message of that entity, and then it reads in the parameters and executes whatever custom code you'd like.

While just randomly passing in an Assign message or something might work, it's probably unsupported in Microsoft's eyes, and it would be hard as hell to debug if someone else ever had to look at this system.

Matt
  • 4,656
  • 1
  • 22
  • 32
  • 1
    It sounds like Microsoft did not think this one through. I am going to try your workaround, as this sounds like the only way available other than writing all of it an JavaScript. Thanks for the help! – Chad Feb 24 '11 at 03:29
  • Hi, is there some example available for this? I'm new to CRM and i want to run some c# code when the user hits a button in the ribbon. I hope someone still reads this. – ThdK Apr 28 '11 at 12:59
  • I've used this method successfully. In my senario the proxy entities (entity P) create plugin created an instance of another entity (entity A) which subsequently fired entity A's create plugin. I did not want this to occur in this particular senario where entity A was created as a result of the button being clicked in the ribbon. I had to make sure entity A's Create plugin handled both senarios appropriately. Not a big deal, but something to keep in mind. – Paul May 04 '11 at 20:22
4

There is another way, insert a two options in the form, when you click custom button which you created in the ribbon make the two options value true then run the save method of the form, in the plugin which you created for the entity check if the two options value is true then run your codes, after that make the two options value to false again. also in the form you can make the two options invisible.

saeid
  • 41
  • 1
1

ok, writing lots of JavaScript logic is a messy think, but creating js code that triggers the “real” logic via a create operation (more or less a command-pattern) will also result in a complex solution.

Depending on the level of complexity and the requirements (e.g. executing an operation on behave of another user is not possible in js) I would always prefer a pure js approach. To reduce the complexity try a library that provideds you with the core functionally:

CrmRestKit

XrmServiceToolkit

Have fun

Daniel

thuld
  • 680
  • 3
  • 10
  • 29
1

I guess this is what you are looking for http://geekswithblogs.net/Nilesh/archive/2011/08/25/call-c-code-inside-silverlight-application-on-click-of-custom.aspx

Nilesh
  • 81
  • 1
  • 10
0

I'm late for the party here, but just to simplify things for people looking at this topic for the newest version of CRM (because it's tagged as crm online): Currently there is something which is called "Action" that can be created the same way as Workflow or Business Process Flow. You can specify input and output parameters for that Action. Main advantages:

  • you can register a plugin for that action, so instead of running jScript logic you can run some c# logic

  • you can call this action using webAPI

As for CRM 2011 although it's already quite outdated - better approach than suggested in the accepted action is to run your logic on post RetrieveMutliple of your custom entity, so that you will avoid creation/deletion of some magic records (and users will only have to have Read privilege for them, not create or update).

Graham
  • 7,431
  • 18
  • 59
  • 84
Pawel Gradecki
  • 3,476
  • 6
  • 22
  • 37