Comments (6)
Hi Gary,
Unfortunately the management API methods are not as straightforward as the delivery API.
For starters the management API requires you to wrap your fields in the locale (or locales) you wish to update. So your structure would look something like this:
{
sys: { //sys props},
fields:[
{
title: {
"en-US": "English title"
}
}
]
}
This means you have to jump through some hoops to modify your model into a suitable Content management model.
Lets say you have a model that looks like this:
public class Person {
public SystemProperties Sys { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
You can retrieve this class from the Delivery API just fine, but to pass it to CreateOrUpdateAsync
you'd need to create a new class something like this.
public class ManagementPerson {
public Dictionary<string,string> Name { get; set; }
public Dictionary<string, int> Age { get; set; }
}
And then use it in a manner similar to this:
Person p = //Person retrieved from Content Delivery API
var managementPerson = new ManagementPerson {
Name = new Dictionary<string, string> {
{"en-US", p.Name}
},
Age = new Dictionary<string,int> {
{"en-US", p.Age }
}
};
var entry = new Entry<ManagementPerson>();
entry.SystemProperties = p.Sys;
entry.Fields = managementPerson;
var res = await client.CreateOrUpdateEntryAsync(entry);
Hope this example helps some.
Let me know if anything is unclear or you'd want some further examples.
from contentful.net.
Hi Robert
Thank you for the explanation and example -- I've implemented those changes and I am getting further now. However, I've now got a versioning error:
Version mismatch error. The version you specified was incorrect. This may be due to someone else editing the content.
I can confirm this isn't due to someone else editing the content. Our model's property is actually null when the Contentful response deserializes into it. I'm then updating some properties on the model and sending it back as above, so the Version is null at that point too, which is what I first took to be causing this error.
I played about with it a bit and manually changed the Version number to reflect the Version presented in the Contentful editor but got the same error. I also tried doing the same thing with the Published Version error but got the same error again.
It turns out that Version isn't the only null property when we deserialize, either. Within Sys we also get null CreatedBy, DeletedAt, LinkType, and UpdatedBy properties.
Do you know of current issues that could be causing that?
Thanks,
Gary
from contentful.net.
Ah yes, if you're updating an already existing entry the easiest way is probably to first retrieve it, update the properties and then post it back. This way the version will always be correct.
Something like this:
var entryToUpdate = await managementClient.GetEntryAsync("id");
entryToUpdate.Fields.name["en-US"] = "Updated value, perhaps from your other model";
var res = await client.CreateOrUpdateEntryAsync(entry, version: entryToUpdate.SystemProperties.Version);
This is because Contentful uses optimistic locking to make sure you don't overwrite each others updates. You can read more about the concept here: https://www.contentful.com/developers/docs/references/content-management-api/#/introduction/updating-and-version-locking
The drawback is that you don't get strong typing for your model from the Management API currently as it's an Entry<dynamic>
, I'm looking to improve this experience in the future.
Regarding properties being null in the ´SystemProperties´ this is expected. Some of the properties are only returned by the management API and some only for certain types of resources. We keep a common type for all occurrences right now instead of having specific EntryManagementSystemProperties
, LocaleManagementSystemProperties
etc. This does mean that many of the properties will be null for most requests though.
Hope this explanation makes sense and let me know if I can be of any further assistance.
from contentful.net.
Hi Gary,
just checking in to make sure this worked for you.
I will close the issue for now, but feel free to reopen if I can help you with anything.
from contentful.net.
Hi Rob,
Commenting on Gary's behalf here.
We have implemented those changes and got a little further. Currently this is what we do:
- Get existing entry from contentful and map our proposed changes to it.
- Convert this to a Management object that has a dictionary with locales.
- We call the management API to get the current version of the entry by passing in the ID.
- We wrap the entry in an Entry<>, setting the fields to our existing entry and the Sys to our system properties which now includes the version we just got.
- We call CreateOrUdpateEntryAsync with this object.
We get the following error:
"An unexpected error occured while performing the get operation|Version mismatch error. The version you specified was incorrect. This may be due to someone else editing the content."
What are the rules regarding which version number to use?
Thanks,
David
from contentful.net.
Hi David
It sure does sound like you're doing everything correct. Just to make sure you're using the SystemProperties.Version.Value
from the entry you get back from the management API to set the version as a parameter when you call CreateOrUpdateAsync
?
Is there any code snippet I could have a look at perhaps?
from contentful.net.
Related Issues (20)
- GetEntriesForLocale() always throws “The resource could not be found.”
- Newtonsoft 13 - Maxdepth error HOT 1
- [v7.1.0] Serialization Error when using a reference in an Entity HOT 3
- Support quality parameter via ImageTagHelper for webp format HOT 4
- Sync API and UsePreviewApi HOT 4
- Call to GetEntriesRaw not resolving linked entries like JavaScript Sdk HOT 4
- Deserializing response that references same content twice in to different objects throws System.ArgumentException: The value "Type" is not of "Expected type" HOT 4
- Assets returning null for some entries but not others, even when published. HOT 2
- Issue with deserialization with sub/superscript is enabled for RichText HOT 2
- Is there a way to setup a default IContentTypeResolver? HOT 2
- ContentfulClient GetEntries not resolving entries sub props objects correctly in certain scenario HOT 4
- MinHeight missing public accessor HOT 1
- System.ArgumentException: Requested value 'superscript' was not found.
- No field with id "sys" found HOT 2
- Could not create an instance of type Contentful.Core.Models.IContent HOT 1
- How the use the .Net SDK to create and entry? HOT 4
- Use `System.Text.Json` instead of `Newtonsoft` HOT 1
- .NET 6.0 support HOT 1
- Contentful.AspNetCore Using Deprecated Packages HOT 1
- Serialization error when creating entries with Document content using the ContentfulManagementClient.CreateOrUpdateEntry HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from contentful.net.