Kentico: Exposing The SQL Generated By DocumentHelper API
- Published on
- -1 min read
Yesterday, I was frantically trying to debug why some documents weren’t getting returned when using the DocumentHelper.GetDocuments() method. Normally when this happens, I need delve deeper to see what SQL Kentico is generating via the API in order to get a little more information on where the querying could be going wrong. To do this, I perform a little “hacky” approach (purely for debugging) whereby I break the SQL within the API by insert a random character within the OrderBy or Where condition parameters.
Voila! The can see the SQL in the error page.
But it was only yesterday where I was shown a much more elegant solution by simply adding a GetFullQueryText()
to your GetDocuments()
, which then returns the SQL with all the parameters populated for you:
string debugQuery = DocumentHelper.GetDocuments()
.OnSite(SiteContext.CurrentSiteName)
.Types(DocumentTypeHelper.GetClassNames(TreeProvider.ALL_CLASSNAMES))
.Path("/", PathTypeEnum.Children)
.Culture(LocalizationContext.PreferredCultureCode)
.OrderBy("NodeLevel ASC", "NodeOrder ASC")
.GetFullQueryText();
I can’t believe I did not know this after so many years working on Kentico! How embarrassing...
Before you go...
If you've found this post helpful, you can buy me a coffee. It's certainly not necessary but much appreciated!
Leave A Comment
If you have any questions or suggestions, feel free to leave a comment. Your comment will not only help others, but also myself.