Page 1 of 1
How to get Current User on Start Page
Posted: Fri Sep 17, 2021 3:52 pm
by chuck1440
I am attempting to create a custom Start Page, and I would like to access the current username. I followed the directions previously posted here:
https://devforum.softprocorp.com/viewto ... ?f=6&t=310. Unfortunately, the javascript statement 'var myautomation = window.external.GetObject("MyAutomation")' returns an exception 'window.external.GetObject is not a function'. I enumerated the members in window.external and found only AddSearchProvider, & IsSearchProviderInstalled. After consulting the SelectDefaultStartPage.html, I noticed references to EnvICE that seems to support some of the functionality that windows.external previously provided. Enumerating the members of EnvICE, I found showContextMenu, getRecentDocuments, execCmd, equals, getHashCode, & toString - but no getObject. Is it still possible to access the current username from a custom start page? And, if so, how is it done? We are currently running version 4.3.60210.119 of Select. Thx.
Re: How to get Current User on Start Page
Posted: Sun Sep 19, 2021 2:23 pm
by BobRichards
Sorry for the issues. I have passed this on to R&D. I will return any information they provide.
Re: How to get Current User on Start Page
Posted: Thu Sep 30, 2021 5:47 pm
by BobRichards
R&D has responded that there is no direct way to do what you are asking.
Their only recommendation is for you to create a
custom shell package that provides an extended start page object with the property you need. An (untested) example class was provided by them:
Code: Select all
using SoftPro.Select.Controls;
internal class SessionExtensibilityObject : IHtmlBrowser2ExtensibilityObject
{
private IServiceProvider _site;
private HtmlBrowser2 _browser;
public SessionExtensibilityObject(IServiceProvider site)
{
_site = site;
SelectServer sps = _site.GetService<SelectServer>();
this.UserName = sps.AuthenticatedUserName
}
void IHtmlBrowser2ExtensibilityObject.Initialize(HtmlBrowser2 browser)
{
_browser = browser;
}
public string UserName {get; private set;}
}
In the shell package initialization, you must register the new class with the IStartPageProvider:
Code: Select all
var spp = GetService<IStartPageProvider>();
spp.Register("Session", () => new SessionExtensibilityObject(this));
Finally, your start page you would get the new property.
Re: How to get Current User on Start Page
Posted: Mon Oct 04, 2021 9:42 am
by chuck1440
Bob,
Thx for the reply. I tried the example class you provided, and would like to relate my experience/discoveries for any other viewers.
The sample code creates a browser extension called Session which exposes an property called UserName. I found that the
property was not visible to the javascript code. However, when I changed it to a public
method, it was visible to the start page javascript. So, something like the following worked:
Code: Select all
internal class SessionExtensibilityObject : IHtmlBrowser2ExtensibilityObject
{
private IServiceProvider _site;
private HtmlBrowser2 _browser;
private string userName;
public SessionExtensibilityObject(IServiceProvider site)
{
_site = site;
SelectServer sps = (SelectServer)_site.GetService(typeof(SelectServer));
userName = sps.AuthenticatedUserName;
}
void IHtmlBrowser2ExtensibilityObject.Initialize(HtmlBrowser2 browser)
{
_browser = browser;
}
public string GetUserName() => userName;
}
On the javascript side, the call to the method returns a Promise. So, to access the values, I used something like this:
Code: Select all
function getUserName() {
try {
Session.getUserName().then( // Call the Session extension - then handle the result
function (value) { display(value); }, // do something with the returned data
function (error) { alert('error:' + error); } // ... or, report the error
);
}
catch (err) {
alert(err.message);
}
}
And it is possible to return multiple values from the extension method using an anonymous object or a Dictionary. Thx again for your assistance.
Re: How to get Current User on Start Page
Posted: Sun Oct 10, 2021 5:23 pm
by BobRichards
Thank you so much for providing the follow up for your solution. We appreciate that you shared your time and effort with others on the DevForum!