Created
August 7, 2016 18:36
-
-
Save abhilash0001/6034d02d81c52cb9ba3a7adeb7f9f191 to your computer and use it in GitHub Desktop.
SQL Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int GetDescendantsCount(Item item) | |
{ | |
using (SqlConnection conn = new SqlConnection(Sitecore.Configuration.Settings.GetConnectionString(Sitecore.Context.Database.ConnectionStringName))) | |
{ | |
try | |
{ | |
conn.Open(); | |
Assert.IsNotNull(item, "A valid Sitecore item must be provided to this method"); | |
string query = @"with cteChildren as | |
(select ID | |
from [dbo].[Items] | |
where ID = CAST(@ItemId as UNIQUEIDENTIFIER) | |
union all | |
select e.ID | |
from cteChildren cte | |
inner join [dbo].[Items] e | |
on cte.ID = e.ParentID) | |
select count(ID) - 1 from cteChildren"; | |
using(SqlCommand cmd = new SqlCommand(query, conn)) | |
{ | |
using (SqlDataReader dr = cmd.ExecuteReader()) | |
{ | |
while (dr.Read()) | |
{ | |
// do your thing | |
} | |
} | |
} | |
} | |
catch (Exception e) | |
{ } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment