Deleting a ContentItem (User) strange issue (Controller vs. Service)

Topics: Customizing Orchard, Troubleshooting, Writing modules
Dec 4, 2013 at 7:29 AM

I've built a module in the past that attaches a custom part to the User content type (let's call this ClaimsPart) -- everything has been working perfectly for a few months. This contains information about other linked resources for the user (for example, an AD account, or an exchange mailbox) Now I have been requested to add a new feature that lets me delete all of these linked items including the Orchard user in question. I've completed all the items above, save for deleting the Orchard user.

I'm under the assumption that I simply do ContentManager.Remove(part.ContentItem) -- and I have in fact looked at the Orchard.Users module and this is how it does it. However, the User does not get deleted at all. I can verify through attaching a debugger that the method does get hit and goes through all the methods. If I delete the user from the 'Users' admin section, it gets deleted properly.

Now, I have noticed that when I moved my .Remove() code into the Controller, I was able to successfully remove the user. Can anyone tell me what's going on? Literally moving the code back to my service class breaks the functionality.

Dec 4, 2013 at 7:37 AM
Removing content from a controller or a service should not make any difference.
Question: how does the call stack look to invoke your service?
If its for example invoked from a content handler, and an exception occurs, that exception is caught and logged. Perhaps anything in log files?
Aldo, enable Exceptions when Thrown (Debug -> Exceptions), attach the debugger and see if an exception occurs that might cause the transaction to roll back.
Dec 4, 2013 at 7:52 AM
My service is directly invoked in the Controller. I thought maybe it had something to do with the way dependencies were being injected, so I decided to pass ContentManager from Controller to the Service method and used that, but still the same. Anyway, reverted that back, and here's the stack trace from my Controller:
>   Orchard.Framework.dll!Orchard.ContentManagement.DefaultContentManager.Remove(Orchard.ContentManagement.ContentItem contentItem) Line 391    C#
    XXXXX.dll!XXXXX.Services.UserManagementService.RemoveADUser(int id) Line 242 + 0xc2 bytes   C#
    XXXXX.dll!XXXXX.Controllers.UserManagementController.Remove(int id) Line 236 + 0x40 bytes   C#
    [External Code] 
I noticed that resuming from the breakpoint, I get this exception:
The controller for path '/Themes/TheAdmin/styles/menu.mycustommodule-admin.css' was not found or does not implement IController.
I get that too when doing .Publish() and other methods but they work just fine.
For reference, here's the call stack from deleting using the built-in Users section from Orchard.Users:
>   Orchard.Framework.dll!Orchard.ContentManagement.DefaultContentManager.Remove(Orchard.ContentManagement.ContentItem contentItem) Line 391    C#
    Orchard.Users.dll!Orchard.Users.Controllers.AdminController.Delete(int id) Line 288 + 0xc3 bytes    C#
And the relevant Controller/Service code:
public ActionResult Remove(int id)
   return RedirectToAction("Index");
public bool RemoveADUser(int id)
    var user = _orchardServices.ContentManager.Get<IUser>(id);

    if (user == null)
        return false;


    return true;
Dec 4, 2013 at 8:06 AM
Ok, I may have oversimplified my Service code, before the .Remove(), I have a few calls to PowerShell in another service using something like:
using (var runspace = RunspaceFactory.CreateRunspace())

        using (var ps = runspace.CreatePipeline())
        { ......
However, all calls to these are in try/catch blocks (and stepping through debugger I'm sure the catch blocks are never hit... in fact, the PowerShell scripts do what they're supposed to do perfectly. In the PowerShell service, aside from calling PowerShell, I also write to files (essentially just writing the called script verbatim as well as the result) and that doesn't produce any errors as well, and the file gets created.

Once I start commenting out all these calls, Remove() works.