Not sure if I have undertood your question correctly, but only for this question "How do you get the window's URL inside the controller for a Visualforce page hosted in the sidebar?" this is the approach to find out.
I had a requirement that during case creation the Parent object details must be shown as sidebar as follows.
To implement this functionality I have created a visualforce with controller and placed that visualforce as sidebar console component.
Here is the tricky part
We cannot get id the parent object in the controller. For this reason, in the visualforce page load I have called actionFunction to pass values to the controller.
Visualforce
<apex:page id="sidebarPage" Controller="SidebarComponentController" showHeader="false" sidebar="false">
<apex:includeScript value="/support/console/28.0/integration.js" />
<!-- On window load getEnclosingPrimaryTabId() is getting called to retrieve the tabLink which will be passed to actionFunction-->
<!-- Function has been written as in controller, getParameters().get('Id') will not work in console -->
<script type="text/javascript">
var getEnclosingPrimaryTabId = function getEnclosingPrimaryTabId() {
sforce.console.getEnclosingPrimaryTabId(getTabLink);
}
var getTabLink = function getTabLink(result) {
sforce.console.getTabLink(sforce.console.TabLink.PARENT_AND_CHILDREN,
result.id, showTabLink);
}
var showTabLink = function showTabLink(result) {
var link = result.tabLink;
passLinkFunc(link);
};
window.onload = getEnclosingPrimaryTabId;
</script>
<apex:form id="form1">
<apex:actionFunction name="passLinkFunc" action="{!init}" immediate="true" reRender="form1">
<apex:param id="URLString" name="URLString" assignTo="{!URLString}" value="" />
</apex:actionFunction>
<!--
All necessary fields to display in the page
-->
</apex:form>
</apex:page>
Controller
In the init
method url will be captured like this:
https://cs67.salesforce.com/500/e?CF00N0n000000gH4P=test1%2B-%2B3HUA&CF00N0n000000gH4P_lkid=a0l16000006y9q7&ekp=500&ent=Case&nooverride=1&RecordType=0120n0000008dmO&scontrolCaching=1
From that url, we can get the id from Custom object through parsing logic.
public class SidebarComponentController
{
public String urlid{get;set;}
public String URLString {get;set;}
public SidebarComponentController() {
System.debug('Inside Constructor=' + ApexPages.currentPage().getParameters());
}
public void init()
{
System.debug('URLString=' + URLString);
String url = EncodingUtil.urlDecode(URLString, 'UTF-8');
System.debug('init:url= ' + url);
//retrieve CustomObject Id in New mode
//urlid= retriveQueryStringValue(url, 'CF00N0n000000gH4P_lkid');
if(url.contains('CF00N0n000000gH4P_lkid')) //put this in Custom label
{
urlid= retriveQueryStringValue(url, 'CF00N0n000000gH4P_lkid');
}
system.debug('***urlid***'+urlid);
}
public String retriveQueryStringValue(String url, String input)
{
String returnString = null;
if(input != null)
{
//this will return CustomObject Id
Integer position = url.indexOf('?');
if(position != -1)
{
String queryString = url.substring(position + 1);
List<String> parts = queryString.split('&');
for(String str:parts)
{
if(str.contains(input))
{
returnString = str.substring(input.length()+1);
return returnString;
}
}
}
}
}
}
Let me know if it helps!