CommonPart

Topics: Customizing Orchard, General, Writing modules
Jun 27, 2011 at 9:09 PM

Hi,

I created a custom content type. It includes a custom content part and the builtin commonpart.

From my custom module, I programmatically create and edit this content part. When created, the fields for commonpart is created as well, but when editing I have to manually enter the values (ie. ModifiedUTC, PublishedUTC) within code.

Do I miss something? Shouldn't the commonpart automatically update those values?

Coordinator
Jun 27, 2011 at 9:19 PM

That's probably because you are doing it through custom code. The code that handles this when going through the normal pipeline is in CommonPartHandler.cs.

Jun 27, 2011 at 9:21 PM

Yes, I'm doing it through custom code. What's the proper way of doing this?

Coordinator
Jun 27, 2011 at 9:25 PM

Of doing what? Are you going through the ContentManager?

Jun 27, 2011 at 9:33 PM

Yes I am. I use the below code to update my custom content (book) item from the values of a viewmodel.


            BookPart book = _orchardServices.ContentManager.Get<BookPart>(bookId, VersionOptions.Latest);

            book.Title = model.BookTitle;
            book.Author = model.BookAuthor;

            book.As<CommonPart>().ModifiedUtc = DateTime.UtcNow;
            book.As<CommonPart>().PublishedUtc = DateTime.UtcNow;
            
            _orchardServices.ContentManager.Publish(book.ContentItem);

Coordinator
Jun 27, 2011 at 9:37 PM

That should only set the publication date on the part and version, not the modified date.

Jun 27, 2011 at 9:43 PM

I think I may find the way to fill in all related fields. What I want to learn is the way of doing this automatically. Like when ContentManager.Create() is called.

Coordinator
Jun 27, 2011 at 9:50 PM

Well, if you follow the code in Create, it is calling the handlers for Creating, Created, Publishing and Published.

Jun 28, 2011 at 7:55 PM

I tried to follow the process in Comments module. However, I realized that the Comments module itself does not update the ModifiedUtc CommonPart field when the associated Comment content is updated.

Shouldn't it be updated?

Coordinator
Jun 28, 2011 at 8:12 PM

Probably yes.

Jun 28, 2011 at 8:22 PM

Then, I'll file a bug for this issue.

By the way, I still feel that if a content type includes the built-in CommonPart and the CRUD operations are handled via ContentManager, then the CommonPart should automatically update its fields after each operation.

Coordinator
Jun 28, 2011 at 8:26 PM

Thanks. You could also submit a patch with the bug report.

There are cases where no, you don't want to systematically update those dates (import is one example).