How to append a DateEditor to a field of a given name

Topics: Writing modules
Aug 30, 2012 at 7:16 AM

Hi Guys,

in blog post in the admin area I can see that there is a created date field which gets a date/time picker..

 

I cannot for the life of me work out HOW in code that gets tacked on to the edit screen..

 

Basically I'd like to make use of some common parts in a module I'm writing but have no idea how to make them part of my Part. If you're using pre existing parts in code you don't need to specify where and how they're stored do you?

I basically wanted to add an AuctionStartDate to my contentpart..

 

Short of a few really good blog posts im finding it difficult to find a decent resource which explains welding techniques and reuse programatically as opposed to adding a field via the gui...

 

 

Developer
Aug 30, 2012 at 2:10 PM
Edited Aug 30, 2012 at 2:10 PM

You can attach fields to your content parts using the ContentDefinitionManager, which is a property of the DataMigrationImpl class, from which your own migration class should derive. To see how that works, just have a look at all of the Migration.cs files that most modules have.

Aug 30, 2012 at 5:09 PM
Thanks for the reply!
just wondering but how do you then query them for the purposes of filtering etc in a linq based format?


I couldn't see in any of the migrations how to add something like the Date Editor.. there's always the common parts and some have an extra call hanging off them to render the date picker but not what field it's assigned to etc..


Cheers,
Tom


On Thu, Aug 30, 2012 at 11:10 PM, sfmskywalker <notifications@codeplex.com> wrote:

From: sfmskywalker

You can attach fields to you content parts using the ContentDefinitionManager, which is a property of the DataMigrationImpl class, from which your own migration class should derive. To see how that works, just have a look at all of the Migration.cs files that most modules have.

Read the full discussion online.

To add a post to this discussion, reply to this email (orchard@discussions.codeplex.com)

To start a new discussion for this project, email orchard@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Developer
Aug 30, 2012 at 6:04 PM

Perhaps I misunderstood your question, but that extra call hanging off the parts is exactly the way to attach content fields to a content part. As for filtering in a linq based format, I think that only works on in-memory collections.

Here's an example attaching a datetimepicker field to a content part (excuse any errors, I haven't actually tested it):

ContentDefinitionManager.AlterPartDefinition("MyPart", part => part
.WithField("MyDatePicker", field => field
.OfType("DateTimeField")
.WithSetting("DateTimeFieldSettings.Display", "DateOnly")
));

Here's an example of a LINQ query (also not tested, it's just to give you an idea):

var q = ContentManager.Query<MyPart>().List().Cast<dynamic>().Where(c => c.MyDatePicker.Value.Year == 2012);

Aug 30, 2012 at 11:48 PM
Thank you so very much I'll give that a go this morning! how then would you assign a name to that field for the purposes of a label etc? OR do you have to define a DateTime property on your part and then hook it up?

Thank you for the linq query! that's exactly what i was looking for as I haven't been able to work out how to begin querying.. the ContentManager / what you can do with it hasn't been very well documented or I haven't been able to find things on it.. I love your tutorial by the way.. one of the best tutorials I've read in a long time.. not just on orchard but in general!

Thanks,
Tom

On Fri, Aug 31, 2012 at 3:04 AM, sfmskywalker <notifications@codeplex.com> wrote:

From: sfmskywalker

Perhaps I misunderstood your question, but that extra call hanging off the parts is exactly the way to attach content fields to a content part. As for filtering in a linq based format, I think that only works on in-memory collections.

Here's an example attaching a datetimepicker field to a content part (excuse any errors, I haven't actually tested it):

ContentDefinitionManager.AlterPartDefinition("MyPart", part => part
.WithField("MyDatePicker", field => field
.OfType("DateTimeField")
.WithSetting("DateTimeFieldSettings.Display", "DateOnly")
));

Here's an example of a LINQ query (also not tested, it's just to give you an idea):

var q = ContentManager.Query<MyPart>().List().Cast<dynamic>().Where(c => c.MyDatePicker.Value.Year == 2012);

Read the full discussion online.

To add a post to this discussion, reply to this email (orchard@discussions.codeplex.com)

To start a new discussion for this project, email orchard@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Developer
Aug 30, 2012 at 11:54 PM

Well, assigning the display name of a content field is just as easy:

ContentDefinitionManager.AlterPartDefinition("MyPart", part => part
.WithField("MyDatePicker", field => field
.OfType("DateTimeField")
.WithSetting("DateTimeFieldSettings.Display", "DateOnly")
.WithDisplayName("My Date Picker")
));

Thanks for the compliment :)

Aug 31, 2012 at 12:09 AM
Edited Aug 31, 2012 at 12:11 AM

Thank you.. That looks perfect.. so do you have to create a new field called MyDatePicker in code? or is the name just a dummy place holder?

 

The goal with the linq querying and playing around with the concept of a service i really just wanted to retrieve the top 5 items ordered by whatever value the datepicker has and display them in a widget..

 

I guess the main thing I'm finding tricky with orchard vs umbraco is that umbraco the documentation is there.. and getting this kind of stuff up and running is really fast.. whereas the learning curve in orchard is pretty big but I can see the power of it all..

It's very easy to add a date time field to something using the gui and you can find out how to do that BUT they don't then show you how to query that in code

Things around the ContentManager are a big unknown for new users is there any good documentation on getting started with querying content parts, field values in the code behind?

 

Am i missing something here.. are queries on fields generally not done.. or are people mainly using projections?

 

and re the compliment.. thank you for taking the time to help others! :)

Aug 31, 2012 at 12:44 AM
Edited Aug 31, 2012 at 2:20 AM

Can't seem to get the Query working alas..

 

 public IEnumerable<AuctionPart> GetUpcoming(int numberToReturn) { return contentManager.Query<AuctionPart>().List().Cast<dynamic>().OrderByDescending(a => a.AuctionStartDate.Value); }
should I then cast it back to an AuctionPart?

 

 

 

Also if I wanted to have the Date come up in my available list of things that can be bound to a query how would i go about that?

Cheers,

Tom

Developer
Sep 13, 2012 at 6:32 AM

Yes, what you could try is to skip the .Cast<dynamic> part, because extensions methods cannot be used together with dynamics. You could add a property to your AuctionPart which basically wraps the value of the AuctionStartDate field. E.g.

public DateTime StartDate {
  get { return ((dynamic)this).AuctionStartDate.Value; }
}

Now that your part has a statically typed StartDate property, you can use that in your LINQ queries:

public IEnumerable<AuctionPart> GetUpcoming(int numberToReturn) { return contentManager.Query<AuctionPart>().List().OrderByDescending(a => a.StartDate); }