This project is read-only.

Setting values on User ContentPart

Topics: Customizing Orchard, Writing modules
Aug 22, 2011 at 2:32 PM
Edited Aug 22, 2011 at 2:38 PM

I have created a ContentPart for extending a User and attached it to the User ContentType, but I haven't been able to work out how to set the values on it. I know I could use the Contrib.Profile module but I'd rather use a strongly typed part.

In my Migrations file I have the following code:



SchemaBuilder.CreateTable("UserAccountLockingStatusPartRecord", table => table
				.Column("LockStatus", DbType.String)
				.Column("LoginAttempts", DbType.Int32)

                                     cfg => cfg.Attachable());

                                                         type => type.WithPart(typeof (UserAccountLockingStatusPart).Name));


I have also created the part, part record and a handler using the normal conventions. I've overridden the MembershipService and can get an IUser, but how can I set the properties of the UserAccountLockingStatusPart? This code gives me a NullReferenceException, so it seems the part is being returned (as no exception is thrown by calling First()), but I cannot manage to set the properties of the part...


var part = user.ContentItem.Parts.Where(
                   p => p.PartDefinition.Name == "UserAccountLockingStatusPart").First().As<UserAccountLockingStatusPart>();
                part.LoginAttempts = 1;
                part.LockStatus = LockStatus.NotLocked;



Aug 22, 2011 at 3:28 PM

Ok apparently my issue was that I was using an existing user, once I created a driver for my part and created a new user then my part was created automatically and I can now use the above code to modify the values.

So my question changes slightly... is it possible to create a new part on the user if it doesn't already exist?

Aug 22, 2011 at 7:26 PM

You mean if the part doesn't already exist? Yes.