Thursday, March 22, 2012

Passing MS CRM 2011 CRMParameter from ribbon button to Silverlight application

It’s pretty cool when you can pass CRMParameters to Silverlight application. To do that we need to get “location” :

public static string GetCRMParametr()
{
     if (HtmlPage.IsEnabled)
    {
        dynamic location = (ScriptObject)HtmlPage.Window.GetProperty("location");
        string crmParametr = location.search;
        if(!string.IsNullOrEmpty(crmParametr))
       {
           return crmParametr
        }
    }

return null;
}

You can add some kind of dummy parser to extract guid’s from passed parameters:


public static List<Guid> GetCRMParametr()
{
     List<Guid> guidsParameters = new List<Guid>();

     if (HtmlPage.IsEnabled)
     {
        dynamic location = (ScriptObject)HtmlPage.Window.GetProperty("location");
        string crmParametr = location.search;
        if(!string.IsNullOrEmpty(crmParametr))
        {
              var delimeters = new char[] {'&','='};
              var splitedParameters = crmParametr.Split(delimeters, StringSplitOptions.None);
              if (splitedParameters[0] == "?Data" && splitedParameters[1] != string.Empty)
             {
                  splitedParameters[1] = splitedParameters[1].Replace("%7d%2c%7b", "*").Replace("%7d", " ").Replace("%7b", " ");
                  var guids = splitedParameters[1].Split('*');
                  guidsParameters.AddRange(guids.Select(Guid.Parse));
                  return guidsParameters;
              }
          }
       }
    return null;
}

Don’t forget to add CRMParameter to your ribbon button:

<CommandDefinition Id="Mscrm.SubGrid.steer_course_partisipant.RunResultsCommand">
     <EnableRules></EnableRules>
     <DisplayRules></DisplayRules>
     <Actions>
        <Url Address="$webresource:steer_ParticipantsResults.htm" PassParams="true" WinParams="height=200, width=420, toolbar=no, resizable=no" WinMode="0">
           <CrmParameter Name="Data" Value="SelectedControlSelectedItemIds" />
        </Url>
     </Actions>
</CommandDefinition>

Now you can play with this sample, for example you can try Regular expression to parse CRMParameter data. It’s up to you. :)

1 comment:

  1. Great example Danyil, that helped in a lot, thanks a lot.

    ReplyDelete