This project is read-only.

Homepage gets changed when I create new content items programatically

Topics: Core, General, Troubleshooting, Writing modules
Sep 2, 2014 at 4:16 PM
Edited Sep 2, 2014 at 4:26 PM
I have this really weird problem, I'm creating a module that programatically creates content items in an IBackgroundTask (if certain conditions are met). Whenever a new item is created this way, it always gets set as the homepage. Obviously I don't want this to happen.

I'm only using this simple code to create the items:
var contentItem = _contentManager.Create(contentType.Name);
contentItem.As<CommonPart>().Owner = _membershipService.GetUser(_orchardServices.WorkContext.CurrentSite.SuperUser);

// Adding values to some fields here...

I have no idea why this is happening. Am I doing something wrong? Is this a bug?

//Update: This even happens if I only use the first line in this code, if I just create an entirely empty content item, without adding a title, field values, owner or anything.
Sep 3, 2014 at 3:48 PM
I guess your content type has AutoroutePart attached. The content item's friendly URL gets its value through the tokenized pattern defined in the content type settings of AutoroutePart, and by default it uses among others the item's slug, which is generated from the title.

Long story short: a content item gets is friendly URL (what is also in AutoroutePart.DisplayAlias) from the item's data, by default (indirectly) from its title. Now if there is no source data (e.g. the title is empty) then the friendly URL will be empty - which corresponds to the home page's URL (i.e. item with empty alias = home page). Thus if you don't fill the content item with data before it's published, the alias will be empty and you'll overwrite the home page.

I also guess that your content type is not draftable. In this case it gets published with Create() too (think that the Save button would publish it on the admin too), thus even if you fill it with data between Create() and Publish(), at the point of Create() it will have the home page overwritten.
Sep 4, 2014 at 9:58 AM
Edited Sep 4, 2014 at 9:59 AM
My content type does indeed have AutoroutePart attached, but it is draftable. However, you are right that the content item already gets published when I call contentManager.Create(), but this should not happen since the draftable option is checked? When I create an item of the same content type manually via the dashboard, it does not get published (when I click the "save" button instead of "publish now", of course).

I just looked more through the Orchard code and noticed that the Create() method uses VersionOptions.Published by default, if you don't specify it yourself in a parameter. This seems to cause exactly what you're describing. I managed to fix my problem by changing my first line of code to this:
var contentItem = _contentManager.Create(contentType.Name, VersionOptions.Draft);
Now my question is, is all this intended behavior? Or should I create 1 or 2 issues? One for the problem that it gives the item an url of "/", when you don't enter a title and the other for it using "VersionOptions.Published" by default, even if the item is draftable.

// Edit: Forgot to say thanks for your explanation, it helped me fix the problem :)
Sep 4, 2014 at 10:32 AM
Hmm I'm not sure. Now that you mention that overload of Create(), probably this is really how it should work and the default Create() is a shortcut.