Iterate all items to update a property

I feel like what I’m looking for would be a common thing, so the fact I’m struggling to figure this out suggests I’m approaching this wrong, but… I have many items of a particular ContentType. I want to iterate all of these items to update a specific field.

For example, imagine a collection of 83 blog posts with ContentType = “post”. Imagine you update your model to include a new field named “author”. Now imagine you want to default the existing 83 posts so author = “John Doe” on all of them.

This seems a task for the CMA. The CMA documentation shows how to fetch the spaces, environments, and content types, but it does not show how to fetch all items of a specific content type.

Kindly point me in the right direction, please!

Hey Troy,

you can use the same query parameter the CDA provides.

E.g.

curl --include \
 --request GET \
 https://api.contentful.com/spaces/f20lfrunubsq/entries?access_token=CFPA-...&content_type=project&fields.title=Forrest

Works fine. :slight_smile: (and I’ll update the documentation tomorrow)…

For operations like you describe I can also recommend to have a look at https://github.com/contentful/contentful-migration.

Hope that helps. :slight_smile:

Thanks, Stefan. I am still confused, however…

The CDA docs show using:

client.getEntries()

In fact, that is how I use the CDA in my app to fetch my data items. However, getEntries() is not a method of the client in CMA. The client object returned from the CMA SDK’s contentful.createClient() looks like this:

{
    getSpaces: [Function: getSpaces],
    getSpace: [Function: getSpace],
    createSpace: [Function: createSpace],
    getOrganizations: [Function: getOrganizations],
    getCurrentUser: [Function: getCurrentUser],
    createPersonalAccessToken: [Function: createPersonalAccessToken],
    getPersonalAccessToken: [Function: getPersonalAccessToken],
    getPersonalAccessTokens: [Function: getPersonalAccessTokens],
    rawRequest: [Function: rawRequest] 
}

Hey Troy,

that’s correct.

const contentful = require('contentful-management')
const client = contentful.createClient({
  accessToken: '<content_management_api_key>'
})

client.getSpace('<space_id>')
.then((space) => space.getEntries())
.then((response) => console.log(response.items))
.catch(console.error)

The way to do it should be this one. :point_up:

Hope that helps. :slight_smile: (I’ll add the search query reference now :wink: )

Yes! Thanks–I’m rocking now. :slight_smile:

Great - you should definitely have a look at the migration cli though. :slight_smile:

1 Like