Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save abhilash0001/6034d02d81c52cb9ba3a7adeb7f9f191 to your computer and use it in GitHub Desktop.
Save abhilash0001/6034d02d81c52cb9ba3a7adeb7f9f191 to your computer and use it in GitHub Desktop.
SQL Gist
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