Convert Sitecore data sources to Guid, programatically

35 views Asked by At

I want to write some code so that when the author creates an item of a particular template, the data source for that item is converted to Guid.

I know that there is an option to manually do it, but I'd like it done for all items of that template when they are created:

enter image description here

I was thinking of doing in on the ItemCreated event in C#.

I did try doing it on the branch template but hat introduced another problem where for every item, it started storing the data for that page under the branch template instead - so this definitely doesn't work.

Thanks!

1

There are 1 answers

5
mikaelnet On

Do you really need to do this? From what I remember, the "Convert data sources to GUID" script was basically a tool to convert paths from data entered in very old Sitecore versions. Sitecore has stored the datasource as guid internally for many years. I don't remember from what version, but I think this applies to at least Sitecore 7 and above.

If you still want tob build such feature, you can make a processor that hooks into the item:saving pipeline, check if any of the __Renderings or __Final Renderings fields are modified, parse the xml content and find any <r> rendering elements having a ds datasource attribute (note the optional namespaces). The the ds attribute is a path pointing to an existing item, replace it with its ID.