CRM 2011 + get Page url in Plugin Code

Réponse proposée CRM 2011 + get Page url in Plugin Code

  • mardi 5 avril 2011 07:10
     
     

    Hi,

     now i doing application by Microsoft Dynamic  CRM 2011, in one senario i have to find Page url in PLUG-IN Code , is it Possible. if possible please send suitable solution with Example....

    Thanks and Regards,


    Elanji

Toutes les réponses

  • mardi 5 avril 2011 07:36
     
     

    You can use HttpContext to obtain the page information.

    HttpContext webContext = HttpContext.Current;

    if (webContext != null)
    {
    string url = webContext.Request.Path;
    }

    Gendral scenario example. Same Plugin Execute method is called for both when "Advanced Find" data is fetched and "Export to Excel" option is done. In order to differentiate betwwn both the events you can use HttpContext to identify the url and proceed on.

    Hope this helps.

  • mardi 5 avril 2011 09:29
     
     

    Hi Vinoth ,

                Thank for ur quick reply, i could able to find url, but it shows only page url which we will able to see on address bar.  but i want actual url [ Example in every page url is Same like "********/main.aspx# , but in reality is while we click on left navigation page goto the actual url [ It contain Entity type code etc,] so i want to this actual URL, shall we find through plugin code? if we able to get , send suitable Solution with example...

     

    thanks and regards,


    Elanji
  • samedi 9 avril 2011 15:23
    Propriétaire
     
     
    moving to development and bump

    Regards, Donna

  • samedi 9 avril 2011 18:54
     
     Réponse proposée

    You can always get the entity ID from the plugin, and build the URL at runtime. You would probably have to do something like this:

    public void Execute(IServiceProvider serviceProvider)
            {
                var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

                Entity entity;
                if (context.InputParameters.Contains("Target") &&context.InputParameters["Target"] is Entity)
                {
                    entity = (Entity)context.InputParameters["Target"];
                }
                else
                {
                    return;
                }

                try
                {
                    var webContext = HttpContext.Current;

                    if (webContext != null)
                    {
                        string url = webContext.Request.Path;
                    }
                    var id = entity.Id;
                    url = url + "?Id=" + id; // build the query string at runtime, you probably have to add objecttypecode and other parameters here as well
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                }
            }

    • Proposé comme réponse Torstein Utne lundi 11 avril 2011 09:44
    •  
  • mercredi 6 juin 2012 18:33
     
     Réponse proposée

    Hello Elanji:

    By using this code, you will get the base url of the record.

    HttpContext webContext = HttpContext.Current;

    if (webContext != null)
    {
    string url = webContext.Request.Path;
    }

    Now as you are worried that how to get entity code and construct the url,

    so for your information, we can replace entity code with entity name

    so just use 'etn=entityname' in place of'etc=entitycode'

    Entity name you can get from currentContext.EntityName

    Now, you need current record Id, that you can get from currentContext.PrimaryEntityID.

    You have now all the things to construct a record URL without hard-coding anything.

    Enjoy...:)


    Saurabh Gupta, MS CRM 2011 Software Development Engineer

  • samedi 16 juin 2012 03:53
     
     

    Hi,

    Just to add, if we are using HttpContext (i.e. System.Web dll) in case of CRM 2011 Online Sandboxed plugin we might recieve "SecurityException : The assembly does not allow partially trusted callers".

    Regards,

    Nishant Rana


    http://nishantrana.wordpress.com

    https://twitter.com/#!/nishantranacrm