1

I have a cloned item.

I want to know which item is it cloned from (what is the source item of the clone).

How can I do that in Sitecore itself or using codebehind c#?

3 Answers 3

2

Sitecore stores information about the source of a clone in __Source Item and Source fields.

sitecore clone source

You can get the value in c# code using

var sourceField = item.Fields[Sitecore.FieldIDs.Source];
var sourceItemField = item.Fields[Sitecore.FieldIDs.SourceItem];

__Source item field is shared - it keeps the source item reference.

Source field is versioned - it keeps source versions for every language separately. Note that the actual field name is __Source.

They both store string values like:

Source: sitecore://master/{2163CB8F-3CF1-4183-A53A-2B9ED850646C}?lang=de&ver=1

_Source Item: sitecore://master/{2163CB8F-3CF1-4183-A53A-2B9ED850646C}

2
  • it seems good , but those fields are empty and not set , do you have any idea why not set ?
    – Sara
    Sep 26, 2022 at 11:55
  • 2
    That means that it is not a clone. I guess you just copied that item instead of cloning
    – Marek Musielak
    Sep 27, 2022 at 6:30
0

Sitecore supports an event subscription where you define an event name and the code to process the event.

With elevated access, open up the ShowConfig.aspx.

The following depicts an event subscription for item:copied. When this event is raised, the event handler defined will process it.

<events>
    <event name="item:copied">
        <handler type="Sitecore.Links.ItemEventHandler, Sitecore.Kernel" method="OnItemCopied" resolve="true"/>
    </event>
</events>
protected internal void OnItemCopied(object sender, EventArgs args)
{
    Item item = Event.ExtractParameter<Item>(args, 1);
    //Do something with the item.
}

If you continue searching for "item:copied" in a Dotnet reflection tool (like ILSpy, DotPeek, JustDecompile) you'll find a method containing this code:

Event.RaiseEvent("item:copied", new object[] { args.Source, args.Copy });

When considering item clones, there is an event called item:cloneAdded that you can subscribe to. There is another answer that helps you find all clones for a given item.

How to find all the cloned items?

2
  • i just want to know the already cloned item which item is cloned from not the opposite scenario
    – Sara
    Sep 22, 2022 at 20:29
  • Can you add the clarification to your question? Sep 22, 2022 at 20:37
0

The Q&A thus far has covered the n = 1 case, but not the n = n case.

Consider the following: the source item of a clone may not be the source item that you actually need. For example, what if you get the source item of a clone only to find that the source item is itself a clone?

This scenario is entirely within the realm of possibilities, and depending on your use case, the first source item may not be acceptable.

In these scenarios you would want to iteratively or recursively check the source of a source item (using the methods in the other answers) until you encounter the source item that does not have a source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.