-2

I wanted to create a blank Component in SDL Tridion 2011 using the Core Service. The only information I have at the start of the process is the Schema URI. The Schema may contain any kind of field (text, rtf, number date, embedded etc), some of which may be mandatory.

I understand that for the mandatory fields, I will need to save some dummy value in them, and this is acceptable as they will be changed manually later.

How can i achieve this?

Chris Summers
  • 10,153
  • 1
  • 21
  • 46
Aquarius24
  • 1,806
  • 6
  • 33
  • 61
  • 1
    Please specify your version of SDL Tridion, and explain a little more about the context. Are you using TOM.NET, Core Service, GUI etc? Do you have any mandatory fields in your Schema? You might consider just making the blank component manually, and cloning it using copy and paste. – Chris Summers May 08 '12 at 12:56
  • I am using core services and it also have mandatory fields also version is sdl tridion 2011 – Aquarius24 May 08 '12 at 13:01
  • Nuno has given you some good tips below, but you can't save a component with empty mandatory fields. Perhaps you can explain what you are trying to achieve so we can make more helpful suggestions. please also consider joining the Area 51 proposal for SDL Tridion at http://area51.stackexchange.com/proposals/38335/tridion?referrer=eo63snjNlUWNn9xqeeO2NA2 – Chris Summers May 08 '12 at 13:04
  • @ Chris. I just have a schema id only and on the basis of that i want to create a dummy component with no data or default data in it in tridion 2011 using core services. Also there are some fields which are mandatory and embedded also. So any help on this? – Aquarius24 May 08 '12 at 13:08
  • 1
    Perhaps you can explain your end goal. I don't believe your desired result is to have an empty component. Even if that is your goal for some reason, you can't do this if there are mandatory fields. Do you understand what mandatory means? – Chris Summers May 08 '12 at 13:12
  • I dont want to leave mandatory fields empty. i want to add some junk values to it so that it will somehow create a component in tridion.later on i can edit the values. – Aquarius24 May 08 '12 at 13:15
  • I have moved this discussion to http://chat.stackoverflow.com/rooms/11019/discussion-between-aquarius24-and-chris-summers if you want to join – Chris Summers May 08 '12 at 13:53

4 Answers4

7

First - you make sure all fields are set to optional in the schema, otherwise this will never work.

Second - You save.

When an optional field has no value, it will have no XML representation. If you have a schema that defines a component like this:

  • Field1
  • Field2
  • Field3

When all fields are optional and you save a value in Field 2, Tridion will store the following:

<Content xmlns="yourNamespace"><Field2>SomeValue</Field2></Content>

If one of your fields is not mandatory, then you'll have to provide a value. If you're using the CoreService then you can use ReadSchemaFields class to get the fields and some information about them - what type, mandatory/optional, etc.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Nuno Linhares
  • 10,214
  • 1
  • 22
  • 42
  • thanks Nuno for your concern but there may be mandatory fields available in the schema. Can you share some code if you can that can be helpful in this case. – Aquarius24 May 08 '12 at 13:01
  • 1
    It would really be helpful if you explained to us what you want to achieve. – Quirijn May 08 '12 at 13:25
  • I just have a schema id only and on the basis of that i want to create a dummy component with dummy data in it in tridion 2011 using core services. Also there are some fields which are mandatory and embedded also. So any help on this? – Aquarius24 May 08 '12 at 13:31
  • 2
    You cannot store mandatory fields with no value. So, use ReadSchemaFields, find if the field is mandatory, store a value - what would be the point of mandatory fields if you could bypass it? – Nuno Linhares May 08 '12 at 15:57
7

Looking at your question/requirement to understand what you're exactly looking for, so we can answer the best possible and relevant.

Are you asking for "How can you write a generic code for component creation using core service?" instead of creating a component with a specific schema knowing all the fields upfront.

If that is what you are looking for, here is what you need to do:

  1. You need to read the schema fields with CoreService (since you know the schema URI)
  2. Now you know what type of fields (embedded/component link etc) you need to create content for
  3. use the links pointed by "Puf" in his answer.

Please note that, if the field is marked as required in Tridion Schema you must have to fill a value and it has to match the field type defined in schema.

Reading schema fields via Core Service sample code can be found here

Community
  • 1
  • 1
Ram G
  • 4,829
  • 1
  • 16
  • 26
5

Updating a Component's field through the Core Service is already answered here: Updating Components using the Core Service in SDL Tridion 2011

That post points to a helper class you can find here: Updating Components using the Core Service in SDL Tridion 2011

If those don't help you in creating a Component, I suggest you post your code instead of asking us to write it for you.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

We ask about use case, because code to fill in specific fields for a specific schema only works in one environment. Code that can automatically determine fields is re-usable.

If the use case is for an Tridion setup that has Inline Editing (Experience Manager or SiteEdit), then the correct approach is content/component types. These define a reference component with "junk defaults," instructions to the author, and even save location context.

If the use case is to allow authors the ability to create dummy components, this is out-of-the box with:

  1. CTRL+C
  2. CTRL+V

One-time setup required to create a "reference component." Of course we can mimic this behavior (in case "Copy of Untitled" isn't an appropriate name) by copying items with the core service.

In that case, I'll also do a copy--see a general solution for creating Tridion items using the Core Service.

  • Fields that require a default can have an actual default in the schema.
  • "Junk values" don't help authors much, always consider good defaults such as an appropriate selection or instructions in the case of fields (maybe). A 10 second change costs development practically nothing, but impacts all future components and the authors that create them.
Community
  • 1
  • 1
Alvin Reyes
  • 2,889
  • 16
  • 38