This project is read-only.

help set the contentPart value

Topics: Administration, Customizing Orchard, Writing modules
Jun 17, 2013 at 3:35 AM
I add some field to my contentPart like follow:
 ContentDefinitionManager.AlterPartDefinition(typeof(FixedAssetPart).Name, builder =>
                   .WithField("AssetCategory", fld =>
                   .WithSetting("TaxonomyFieldSettings.Taxonomy", "AssetCategory")
                   .WithSetting("TaxonomyFieldSettings.LeavesOnly", "False")
                   .WithSetting("TaxonomyFieldSettings.SingleChoice", "True"))
                   .WithField("DateGeted", fld =>
                   .WithSetting("DateTimeFieldSettings.Display", "DateOnly"))
when I import data from a excel file,I want to set the "AssetCategory" and the "DateGeted" value of the "FixedAssetPart", how to set those value?
Jun 17, 2013 at 7:05 AM
any help?
Jun 17, 2013 at 11:40 AM
Edited Jun 17, 2013 at 5:07 PM
You can set field values by accessing those fields via the content item in question, e.g.
dynamic contentItem = ... 
contentItem.FixedAssetPart.DateGeted.DateTime = ...;
For the taxonomy field, you can call ITaxonomyService.UpdateTerms to set the terms for that field, e.g.
_taxonomyService.UpdateTerms(contentItem, terms, "AssetCategory");
See TaxonomyFieldDriver for exact syntax.
Jun 17, 2013 at 12:47 PM
Avoid using DateTime.Now;

Please inject IClock in to your class and use the .UtcNow from that.
Jun 17, 2013 at 5:06 PM
Thanks Nick. Definitely, when you need the current Date Time, inject IClock. However that is not what this code snippet is about. Let me update the snippet with ... to avoid further confusion.
Jun 17, 2013 at 5:10 PM
I am just being pedantic hehe
Jun 17, 2013 at 5:17 PM
Jun 18, 2013 at 4:38 AM
thank you very much, I do set the value by use sfmskywalker's methods,but when I create the obj , it seems not saved the value to the obj,
and My code snippet are follows:
   dynamic  p = Services.ContentManager.New<FixedAssetPart>("FixedAsset");
                             p.AssetName = AssetName;
                             p.AssetCode = AssetCode;
                             p.OriginalValue = OriginalValue;
                             p.AssetUseMonths = AssetUseMonths;
                             p.SalvageValue = SalvageValue;
                             p.MonthDepreciateValue = MonthDepreciateValue;
                             p.AssetCount = AssetCount;

                             p.FixedAssetPart.DateGeted.DateTime = AssetGetDate;
                             var taxonomy = _taxonomyService.GetTaxonomyByName("DepreciateStatus");

                             IEnumerable<TermPart> terms = _taxonomyService.GetTerms(taxonomy.Id);

                              _taxonomyService.UpdateTerms(p.ContentItem, terms, DepreciateStatus);

                             //if not the same AssetCode exists,add
                              if (_fixedAssetService.VerifyUnicity(AssetCode))


                                  dynamic pE = _fixedAssetService.GetContentItemByAssetCode(AssetCode).As<FixedAssetPart>();
                                  pE = p.ContentItem;

                                Services.ContentManager.UpdateEditor(pE, this);
it seems the "AssetCategory" and the "DateGeted" value I set not save to the contentItem,what's wrong with my create and edit code ? thanks agin!:)