Asked by:
CRM 2011 + get Page url in Plugin Code

Question
-
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- Moved by Donna EdwardsMVP Saturday, April 9, 2011 3:23 PM (From:CRM)
Tuesday, April 5, 2011 7:10 AM
All replies
-
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.
- Proposed as answer by Nishant RanaMVP Monday, October 2, 2017 2:26 PM
Tuesday, April 5, 2011 7:36 AM -
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,
ElanjiTuesday, April 5, 2011 9:29 AM -
Saturday, April 9, 2011 3:23 PM
-
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);
}
}- Proposed as answer by Torstein Utne Monday, April 11, 2011 9:44 AM
Saturday, April 9, 2011 6:54 PM -
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
- Proposed as answer by SaurabhGupta_2160 Wednesday, June 6, 2012 6:33 PM
Wednesday, June 6, 2012 6:33 PM -
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
- Edited by Nishant RanaMVP Saturday, June 16, 2012 3:57 AM
- Proposed as answer by Nishant RanaMVP Monday, October 2, 2017 2:26 PM
Saturday, June 16, 2012 3:53 AM -
For CRM online, you are right it's not working.
Is someone have an option for CRM online?
Steeve Gauvreau, CRM Technical Lead, Keyrus Canada
Friday, September 25, 2015 3:28 PM