Sometimes I like to dive into the SQL databases that hold all of our precious Sitecore content and go wild. No really; sometimes the Sitecore tree, XPath builder or search function are just not the easiest tools to research a particular problem. Once you understand the database schema of Sitecore (which is not that difficult), querying the databases directly can be of great help.
Here is a script that creates 2 handy functions that you can use to make querying Sitecore data just a little easier:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
USE [SitecoreWebsiteSitecore_Master] GO /****** Object: UserDefinedFunction [dbo].[SC_GetName] Script Date: 06/21/2012 21:59:44 ******/ IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SC_GetName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) DROP FUNCTION [dbo].[SC_GetName] GO /****** Object: UserDefinedFunction [dbo].[SC_GetPath] Script Date: 06/21/2012 21:59:44 ******/ IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SC_GetPath]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) DROP FUNCTION [dbo].[SC_GetPath] GO /****** Object: UserDefinedFunction [dbo].[SC_GetPath] Script Date: 06/21/2012 21:59:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SC_GetPath]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) BEGIN execute dbo.sp_executesql @statement = N'-- ============================================= -- Author: Robin Hermanussen -- Create date: 2012-06-21 -- Description: Takes a Sitecore ID and resolves the path to the item that it corresponds with -- ============================================= CREATE FUNCTION [dbo].[SC_GetPath] ( @ItemID [uniqueidentifier] ) RETURNS varchar(MAX) AS BEGIN DECLARE @Result varchar(MAX) with scpath(Name, ParentID) as ( select Cast(a.Name as varchar(MAX)), a.ParentID from [Items] a where a.ID = @ItemID union all select Cast(b.Name + ''/'' + c.Name as varchar(MAX)), b.ParentID from [Items] b inner join scpath c on b.ID = c.ParentID where c.ParentID is not null ) select top 1 @Result = ''/'' + d.Name from scpath d where d.ParentID = ''11111111-1111-1111-1111-111111111111'' RETURN @Result END ' END GO /****** Object: UserDefinedFunction [dbo].[SC_GetName] Script Date: 06/21/2012 21:59:44 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SC_GetName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT')) BEGIN execute dbo.sp_executesql @statement = N'-- ============================================= -- Author: Robin Hermanussen -- Create date: 2012-06-21 -- Description: Takes a Sitecore ID and resolves the path of the item -- ============================================= CREATE FUNCTION [dbo].[SC_GetName] ( @ItemID [uniqueidentifier] ) RETURNS varchar(MAX) AS BEGIN DECLARE @Result varchar(MAX) select top 1 @Result = [Name] from [Items] where [ID] = @ItemID RETURN @Result END ' END GO |
After running this script (be sure to change the statement in line 1 to match your database name), you can write queries such as this one:
1 2 3 4 5 6 7 8 |
USE [SitecoreWebsiteSitecore_Master] -- Search for all items named 'Sample' and output their paths and template names SELECT [ID] , [dbo].SC_GetPath([ID]) as [Path] , [dbo].SC_GetName([TemplateID]) as [Template Name] FROM [Items] a where Name = 'Sample' |
This results in:
Image may be NSFW.
Clik here to view.
Since the SC_GetPath function uses recursion, make sure your query does not go out of control. SQL Server does not allow too many recursions by default.