7. juni 2010 by Thomas Stern
The number of usersession that is possible to have open in
sitecore depend onj the licens, which is fair enough. But the task
for an administrator to end hanging usersession seems somewhat
headless. The administrator have to logout, to get a list of active
user sessions, and then choose to end hanging or not used sessions.
This might be fine in minor solutions but I think this might pose a
problem in solutions where a user with the "Is Adminstrator" flag
is set to true can be hard to find. He might be working in
different office or just generally be hard to get a hold of. It
doesn't require you have the "Is administrator" flag to end session
but it is required to get the list of active sessions. You don't
even have to be logged in to sitecore to end active session, if
presented with the option to end active session anonymous users
could do so. My idea is to help local administrators with
functionality to end session with out leaving the backend, or
having to log out or anything like that. Since sitecore is one big
tool box I will build it as shortcut in the tray of sitecore.
Okay so we start, all our work is done in the core database and it
only contains minimal coding. We start with making the tray icon.
In the core database we go to content/applications/Desktop/tray and
add a item I will call it UserSessions the Data section of the item
find the click attribute and add the command name, this will also
have to go in the command file we get back to that. I also and icon
for the tray I have choosen the standard user icon.

Now with the tray icon shortcut in place you should be able to see
it in th start bar just save the item and hit F5

Now we will add the command to the App_Config/Commands.config
add the following line
<command name="Tray:UserSessionViewer"
type="UserHelper.UserhelperTrayCommand,UserHelper" />
I will add this little user feature helper as an application so we
need to add an application and layout for the application in the
core database. We start with adding the layout "core database" go
to sitecore/layout/applications and add a new layout in the data
section in path write the physical path to layout you will create
in the next step.

Next we will add the application again this is done in the core
database go to sitecore/content/Applications/ and add a new
application pull on the the layout we just created make sure you
can see it in the layout section for the item. Also note the Id for
your application, we will have to use it in the code part.

Okay now we are done with the configuration in sitecore now to
code part.
First we will start we the Tray click command which is pretty
simple note that is hardcode the id for our application and the
database I getting the item from.
Now we have something that handle the click on the Tray command
no we will code the popup or application window. First the frontend
code for the application. The frontend code shows when the user
session started and when the last request was made info need to
take into account which user sessions you should end
<div class="taskInfoWindow">
<div class="SystemTime>
<span class="Literal">System Time</span>
<span class="Time"><%# SystemTime %></span>
</div>
<div class="tasks">
<asp:Repeater ID="taskRepeater" runat="server" DataSource="<%# ScheduleItems %>"
OnItemCommand="taskRepeater_ItemCommand">
<HeaderTemplate>
<table>
<thead>
<tr class="informationHeader">
<td class="Icon">
*
</td>
<td class="Literal">
Task name
</td>
<td class="Literal">
Is Due
</td>
<td class="Time">
Last run
</td>
<td class="Time">
Next run
</td>
<td class="Button">
Execute Task
</td>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr class="scheduledItem <%# Container.ItemIndex == 0 || Container.ItemIndex % 2 == 0 ? "even" : "odd" %>">
<td class="Icon">
<img src="/sitecore/shell/Themes/Standard/<%#((ScheduleItem)Container.DataItem).Icon%>"
alt="<%# ((ScheduleItem)Container.DataItem).DisplayName %>" />
</td>
<td class="Literal">
<%# ((ScheduleItem)Container.DataItem).DisplayName%>
</td>
<td class="Literal">
<%# ((ScheduleItem)Container.DataItem).IsDue%>
</td>
<td class="Time">
<%# ((ScheduleItem)Container.DataItem).LastRun.ToString("HH:mm dd/MM-yyyy")%>
</td>
<td class="Time">
<%# ((ScheduleItem)Container.DataItem).NextRun.ToString("HH:mm dd/MM-yyyy")%>
</td>
<td class="Button">
<asp:LinkButton ID="LinkButton1" Text="Execute" CssClass="SortButton" runat="server"
CommandName="Execute" CommandArgument="<%# ((ScheduleItem)Container.DataItem).ID %>" />
</td>
</tr>
</tbody>
</ItemTemplate>
<FooterTemplate>
</tbody> </table>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
Now to the code behind, I am only using standard sitecore
functionality nothing fancy here other then the click event for the
repeater that end the selected user session.
using Sitecore.Data;
using Sitecore.Security.Accounts;
using Sitecore.Web.Authentication;
namespace Userhelper.Presentation
{
public partial class UserSessionsView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
public IEnumerable UserSessions
{
get
{
return DomainAccessGuard.Sessions;
}
}
protected void userRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string id = e.CommandArgument.ToString();
switch (e.CommandName)
{
case "Execute":
EndSession(id);
break;
}
}
private void EndSession(string sessionID)
{
DomainAccessGuard.Kick(sessionID);
}
}
}
This is how I looks when we done

The styling part is left for you. With this little userhelper
you can set security on the tray icon so local administrator can
see the tray icon and all other users have denied read access so
only users with elevate security settings can end user
sessions.