Giter Club home page Giter Club logo

Comments (6)

Roblinde avatar Roblinde commented on July 23, 2024

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.

hollg avatar hollg commented on July 23, 2024

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.

Roblinde avatar Roblinde commented on July 23, 2024

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.

Roblinde avatar Roblinde commented on July 23, 2024

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.

davidmeekstockport avatar davidmeekstockport commented on July 23, 2024

Hi Rob,

Commenting on Gary's behalf here.

We have implemented those changes and got a little further. Currently this is what we do:

  1. Get existing entry from contentful and map our proposed changes to it.
  2. Convert this to a Management object that has a dictionary with locales.
  3. We call the management API to get the current version of the entry by passing in the ID.
  4. 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.
  5. 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.

Roblinde avatar Roblinde commented on July 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.