Page 1 of 1

How to get Current User in custom Start Page

Posted: Wed Nov 02, 2011 9:24 am
by joe.mag
I have a custom start page in which I'd like to get the current user's username. I have embedded an iframe on my custom start page and within it I have an ASPX page running a report. I would like to be able to initialize the username drop down list I use to filter the report to the current user's username. I imagine there is some way to get this via a call to window.external.GetObject("") but lack of documentation means I'm not sure what interfaces I can gain access to or which interface would best expose this attribute.

Re: How to get Current User in custom Start Page

Posted: Thu Nov 03, 2011 9:40 am
by Mark McKenna
Currently, we do not provide an extensibility object that offers up the current user name. However, the Select automation framework is extensible and does allow for you to roll your own objects to provide whatever functionality you need.

Implement your own class that derives from SoftPro.Select.Shell.ExtensibilityObject, and then register that class through your package by calling the RegisterAutomationObject() method and by applying the ProvideAutomationObject attribute to your package class. Then, your newly registered automation class can get the Select Server service and work through that to retrieve the current user name.

Something like this:

Code: Select all

[ProvideAutomationObject("MyAutomation", typeof(MyAutomationExtensibility))]
public class MyPackage : SoftPro.Select.Shell.Package
{
    protected override void OnInitialize()
    {
        base.RegisterAutomationObject("MyAutomation", new MyAutomationExtensibility(this));
    }
}

[ComVisible(true)]
[Guid("ABCDEF01-AAAA-BBBB-CCCC-DDEEFF112233")]
public interface IMyAutomationExtensibility
{
    string UserName { get; }
}

[ComVisible(true)]
[ComDefaultInterface(typeof(IMyAutomationExtensibility))]
[Guid("12345678-1234-1234-1234-1234567890AB")]
public class MyAutomationExtensibility : SoftPro.Select.Shell.ExtensibilityObject, IMyAutomationExtensibility
{
    private IServiceProvider _sp;

    internal MyAutomationExtensibility(IServiceProvider sp)
    {
        this._sp = sp;

        SelectServer sps = _sp.GetService<SelectServer>( );
        UserName = sps.AuthenticatedUserName;
    }

    public string UserName { get; private set; }
}
Now that you have a registered automation object, you can access it inside your start page code. For example:

Code: Select all

function showusername( )
{
    var myautomation = window.external.GetObject("MyAutomation");
    var name = myautomation.UserName;
    ...
}

Re: How to get Current User in custom Start Page

Posted: Thu Nov 03, 2011 9:52 am
by joe.mag
Thanks, Mark! Any reply that includes sample code gets an A+. I will try this out and report back any interesting details for future readers of this thread.

Re: How to get Current User in custom Start Page

Posted: Wed Nov 09, 2011 5:44 pm
by joe.mag
I get the error "The non-generic method 'System.IServiceProvider.GetService(System.Type)' cannot be used with type arguments " on the line that reads "SelectServer sps = _sp.GetService<SelectServer>();"

Do I need to be referncing a server-side component?

Re: How to get Current User in custom Start Page

Posted: Thu Nov 10, 2011 12:07 pm
by Mark McKenna
Just use the non-generic method:

Code: Select all

SelectServer sps = (SelectServer)_sp.GetService(typeof( SelectServer ) );
The generic version is an extension method we've recently added to the IServiceProvider interface, so you probably don't have access to it (although you could provide your own).