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#?
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communitySitecore 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.
Sitecore stores information about the source of a clone in __Source Item
and Source
fields.
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.
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}