This project is read-only.

Updating UserPart Content Item via Custom Module

Topics: Troubleshooting, Writing modules
Sep 6, 2012 at 10:29 AM

Excuse the title being a bit vague - it was hard trying to summarise what I'm trying to achieve...

Basically I've added a couple of extra fields to the User record in Orchard - I can edit these happily through the admin module, so pretty sure I've added code to handle these fields correctly.

What I now want to do is modify this data through a custom module I've written.   What I've tried doing is adding a function to the MembershipService and calling this from the controller in my custom module.   I've debugged this and the function is being called, but for some reason the changes don't get stored in the database.

Here's the exposed function I've added to the MembershipService:

public void SaveComponentAccess(IOrchardServices service, IUser user, string components) {
            if (!user.Is<UserPart>())
                throw new InvalidCastException();

            var userRecord = user.As<UserPart>().Record;
            SaveComponentAccess(service, userRecord, components);

Here's the function called from above:

void SaveComponentAccess(IOrchardServices service, UserPartRecord partRecord, string components) {
            var user = service.ContentManager.Get<UserPart>(partRecord.Id);

           user.MyNewField = components;


The controller in the custom module just calls the exposed function before redirecting onto another view.   I'm guessing I'm missing some sort of update function somewhere, but I'm a bit lost with this bit of Orchard...   Any ideas or suggestions on how to get this working would be appreciated...

Sep 12, 2012 at 7:50 PM

I find it very surprising that this: user.MyNewField = components; would even compile. Oh, you mean they are properties, not fields? You've actually modified the core user class? Ouch. That is not a good idea at all. Just add a part instead to the type. There are plenty of examples in existing profile modules on the gallery.