Thread tagged as: Question, Api, Runway

Creating collection items programmatically

Hi there,

How would I go about creating collection items programmatically. I have managed to create an addon which grabs an XML feed. I can loop through these items. I just need to add each one to a collection I have already set up.

Any help would be kindly appreciated.


CDD Nation

CDD Nation 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

There's no public API for this, but if it's something you need to do you'd be best using the private API rather than trying to put things into the database directly.

Take a look at PerchContent_CollectionItems::create() - that should do what you're after.

Thank you very much for a quick reply, I will take a look at that in the morning!

Drew McLellan

Drew McLellan 2638 points
Perch Support

We do plan to add a proper API for this, but I don't have an ETA on that.

Hi Drew,

I've managed to create a collection item and it seems to work, other than the item needs re-saving before it will actually show up on the site.

I've noticed that when other collection items are created (this time through the CMS) two revisions are created. One with blank values for itemJson and itemSearch, the other revision gets created with the correct values, and is the current revision.

Assuming creating a new revision will fix the issue, how do I achieve this?

I've found the 'create_new_revision' method of the 'PerchContent_CollectionItem' class, but seems to throw an error upon instantiating it because it's expecting a details array to be passed via the construct method of the parent class.

I hope that's all made sense! Any further guidance would be much appreciated.


Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you'll need to call PerchContent_CollectionItem::publish to publish the changes and PerchContent_CollectionItem::index to add it to the index.

Thank you! I have managed to get the import working using those methods. And an API for these sorts of things would be amazing! Looking forward to future developments.

Cheers for the support!

Hi CDD Nation,

Sorry to hijack your post but you appear to have done something very similar to what I need to achieve on a client project. We will be taking an XML feed of jobs from a 3rd party API and processing it to allow inserting / updating / deleting of the relevant job collection item in the CMS.

Can you provide any guidance? Did you get everything working as expected and did you encounter any issues? Search will be a big factor for us so we need to ensure these new dynamically created collection items are searchable

Drew - any news on the API?

Many thanks