Orchardcore: Demo code of how to Create / Update ConentItem programmable

Created on 9 Jul 2020  路  4Comments  路  Source: OrchardCMS/OrchardCore

I there any Demo-code of how to Create / Update ContentItem programmable

as well, what doing
_contentManager.UpdateValidateAndCreateAsync()

question

Most helpful comment

We have mentioned a few times to write some unit tests / integration tests to demonstrate usage of this.

Here is some old code from before UpdateValidateAndCreateAsync() that makes a content item for me.

            var result = await _contentManager.NewAsync("AgentTestResult");
            result.Weld<ContainedPart>();
            result.Alter<ContainedPart>(x => x.ListContentItemId  = traderAgentTestResultsContentItemId);
            result.DisplayText = "[email protected]";

            result.Alter<ContentPart>("AgentTestResult", a =>
            {
                a.Alter<TextField>("Email", a => a.Text = "[email protected]");
                a.Alter<TaxonomyField>("TraderType", a =>
                {
                    a.TaxonomyContentItemId = traderTypeContentItemId;
                    a.TermContentItemIds = new string[] { traderTypeResult.TradingAgentTypeTermId };
                });
            });
            result.Alter<BagPart>("Answers", a =>
            {
                a.ContentItems = traderTypeResult.Answers;
            });

            await _contentManager.CreateAsync(result, VersionOptions.Published);

UpdateValidateAndCreateAsync is an extension method to assist calling some content manager methods in the correct sequence, to ensure all the events are fired. Validate is a reasonably resent method which ensures that some important validation, like alias part unicity, and autoroute unicity is maintained (i.e. the result will fail if you try to create something when the autoroute path already exists.

All 4 comments

Learn from Code, you can check OrchardCore.Modules\OrchardCore.Content\Controllers\AdminController there are variety of methods such as New(), CreateAsync(), GetAsync() and much more

We have mentioned a few times to write some unit tests / integration tests to demonstrate usage of this.

Here is some old code from before UpdateValidateAndCreateAsync() that makes a content item for me.

            var result = await _contentManager.NewAsync("AgentTestResult");
            result.Weld<ContainedPart>();
            result.Alter<ContainedPart>(x => x.ListContentItemId  = traderAgentTestResultsContentItemId);
            result.DisplayText = "[email protected]";

            result.Alter<ContentPart>("AgentTestResult", a =>
            {
                a.Alter<TextField>("Email", a => a.Text = "[email protected]");
                a.Alter<TaxonomyField>("TraderType", a =>
                {
                    a.TaxonomyContentItemId = traderTypeContentItemId;
                    a.TermContentItemIds = new string[] { traderTypeResult.TradingAgentTypeTermId };
                });
            });
            result.Alter<BagPart>("Answers", a =>
            {
                a.ContentItems = traderTypeResult.Answers;
            });

            await _contentManager.CreateAsync(result, VersionOptions.Published);

UpdateValidateAndCreateAsync is an extension method to assist calling some content manager methods in the correct sequence, to ensure all the events are fired. Validate is a reasonably resent method which ensures that some important validation, like alias part unicity, and autoroute unicity is maintained (i.e. the result will fail if you try to create something when the autoroute path already exists.

Seb do we need to add a unit test?

Seb do we need to add a unit test?

I think whatever that will exist in the Demo module or / and in documentation are welcome.
Personally, using the GitHub issues to search whatever was asked, not best user friendly option.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aghili371 picture aghili371  路  3Comments

hishamco picture hishamco  路  3Comments

chillibug picture chillibug  路  4Comments

deanmarcussen picture deanmarcussen  路  3Comments

JanSichula picture JanSichula  路  3Comments