Monday, December 26, 2005

Is a sublayout in use anywhere?


The following sample code may be useful if you want to know whether a sublayout is used anywhere.

   1: Sitecore.Context.SetActiveSite("shell");
   2: Database masterdb = Sitecore.Context.ContentDatabase;
   3:  
   4: // item for which we check sublayout reference
   5: Item item = masterdb.Items["/sitecore/content/home/test"];
   6:  
   7: // get Renderings field of the item
   8: string rend = item.Fields["__renderings"].Value;
   9: LayoutDefinition layout = LayoutDefinition.Parse(rend);
  10: DeviceItem dev = Sitecore.Context.Device;
  11: DeviceDefinition device = layout.GetDevice(dev.ID.ToString());
  12:  
  13: // the rendering which you want to check referrings to
  14: Item myrendering = masterdb.Items["/sitecore/layout/renderings/document"];
  15:  
  16: // get the rendering of our item
  17: RenderingDefinition rendering = device.GetRendering(myrendering.ID.ToString());
  18:  
  19: if(rendering != null){
  20:   Response.Write(String.Format("Item {0} links to the rendering {1}", item.Name, myrendering.Name));
  21: }
  22: else {
  23:   Response.Write(String.Format("Item {0} doesn't link to the rendering {1}", item.Name, myrendering.Name));
  24: }

2 comments:

cmon_stoke said...

Hi Alex,
Thanks for the excellent site and masses of information.

I'm sure you have plenty to do, but do you think it would be possible to reformat your code examples as they are hard to read.

Thanks again
Mark

Unknown said...

Hey Mark,

Thank you!
This should look much better now.

This is a very old post, not sure if this code still works. You should be able to query Link Database for the usaged of a particular sublayout in the __renderings field.