Override WorkContext

Topics: Customizing Orchard, Writing modules
Jun 24, 2011 at 3:52 AM

Hello, I'm using Orchard 1.2 , and now I have faced a problem that I need to get the Id of the current employee (CurrentEmployee for example) . I'm trying to override the WorkContext but still unsuccessful , anyone have any idea or did this before can help me . Thanks very much

Jun 24, 2011 at 4:20 AM

From where? Who defined the employee type? What do you mean "override the workcontext"?

Jun 24, 2011 at 4:47 AM

For more details , I have create an EmployeeType , inside EmployeeType edit page , I click a button "Add new Contact" , a popup appear to create contact . So now I need to get EmployeeId to insert into ContactRecord. 

I want to add the current editing employee to WorkContext , and get it like WorkContext.CurrentEmployee

Jun 24, 2011 at 4:51 AM

I'm sorry, I still don't understand what you mean or what you are trying to do.

Jun 24, 2011 at 7:03 AM

You could send your EmployeeId to the URL you are using for that popup. If it's a modal dialog, say from jQueryUI, then all you need to do is add an <input type="hidden" name="EmployeeId" value="@Model.EmployeeId"/> when you render the dialog's menu.

You can't use WorkerContext in the way you've expressed unless you add an Extension method for WorkerContext and have the data you are after in the Session object.

Jun 24, 2011 at 8:14 AM

My question mean : How can I add another member to WorkContext , for example : WorkContext.CurrentEmployee , just like WorkContext.CurrentUser

Jun 24, 2011 at 8:17 AM

And my question is why do you want to do that?

Jun 24, 2011 at 8:22 AM

And I answered you that you should use an extension method and have CurrentEmployee().

I also warned you that worker context is implemented as a unit of work. Once the page is rendered you should not have access to that information.

If you reallly want to add that CurrentUser as a property, by all means, in Orchard.Source/src/Orchard/WorkerContext.cs you will find the abstract class. You will need to write a


public int EmployeeId {
            get { return GetState<int>("EmployeeId"); }
            set { SetState("EmployeeId", value); }


In your display driver/controller you can say WorkerContext.CurrentEmployee = employee_id.

Jun 24, 2011 at 8:32 AM

Simpler: stick it on workContext.Layout: that one is dynamic. Still, I'm doubting this is the right way of doing whatever it is you are trying to do.

Jun 24, 2011 at 8:41 AM

It seems to me that he is trying to have something like the following

if (window.showModalDialog) {
} else {
menubar=no,scrollbars=no,resizable=no ,modal=yes');


When he calls some URL he is probably thinking that he can use WorkContext as a Session object from whatever view he is in. Since he saw that a view has acess to the WorkContext, he must have said, Hey! I know what, I can use that WorkContext thing to keep the EmployeeId - I just need to know how to add CurrentEmployeeId to WorkContext.

In my opinion that is not the way to go about it, but maybe it is just me.

Jun 24, 2011 at 8:45 AM

Yes, if that's the case, that simply won't work. The employee id just needs to be passed as a parameter on that popup url instead. Well, actually there is no reason why this popup couldn't be done entirely without a server round-trip, with some jQuery. In that case, none of this is necessary.

Jun 24, 2011 at 8:50 AM

Thank you for the answer , I have read the WorkContext.cs before , but i don't want to change the Orchard core , so that's why I'm asking how to override it. 

About the hidden field that you told me , I thought about it once , but if i can get it from context like CurrentUser will be better

@bertrand: oh you still don't understand my situation , I'm working on an Employee module with lot of information , for now is the ContactDetails , one Employee may have many contacts address . ContactDetailsPart was added Employee content type , and when I click Edit a Employee , I must have a section to edit ContactDetails as well as create more Contact , so that I create a button show a popup window when it clicked to create new or edit Contact . And in the popup window , I need to find what Employee that Contact will be added to. Hope that clear.

Jun 24, 2011 at 8:54 AM

How are you displaying the popup window?

You cannot override WorkerContext, period! All you can do is add values to it via SetState and GetState and it is only valid during a http request, for multiple requests you need something else to hold data cross-requests.

Jun 24, 2011 at 7:40 PM

That's exactly what I had understood actually. Your popup does not need to be a separate request. Nowadays, people do this sort of thing on the client-side without a server round-trip until the data needs to be persisted. Example of code I use to do similar editing in an app I built (requires jQueryUI):

<script type="text/javascript">
    $(function () {
        var editPane = $("#editPane");
        function saveEditPane() {
            var updatedValue = $("#updatedValue").val();
            var id = editPane.data("translationid");
            $.post("@Url.Action("Update", "Localization")",
                    id: id,
                    culture: '@Model.Culture',
                    value: updatedValue
                function(data) {
                    var li = editPane.data("li");
                    li.find(".originalString").css("color", "black");
            autoOpen: false,
            height: 530,
            width: 780,
            modal: true,
            resizable: false,
            buttons: {
                Save: saveEditPane,
                Cancel: function () {
        $("#updatedValue").keypress(function(e) {
            if (e.ctrlKey === true && (e.which == 10 || e.which == 13)) {
        $(".editButton").click(function () {
            var li = $(this).closest("li");
                translationid: li.data("translationid"),
                li: li
<div style="display:none" id="editPane" title="Edit">
    <div id="editPaneContext"></div>
    <br />
    <label for="originalValue">Original string:</label>
    <textarea id="originalValue" class="translationField" rows="6" cols="75" disabled="disabled"></textarea>
    <label for="updatedValue">Translation:</label>
    <textarea id="updatedValue" class="translationField" rows="6" cols="75"></textarea>

Jul 1, 2011 at 11:10 AM

Thanks , and sry for the late reply , like you said , I don't need the override it anymore XD