Content Item Report with Sitecore API
Working with items
I have interacted with Sitecore customers where some of their needs on Sitecore reports have been similar: to have a tabular report tool. Whether it s for checking content item inventory, a clean-up analysis, or identifying when it was last updated and by whom. There s already several ways to create a report like this, with external Sitecore modules such as Sitecore Powershell Extensions. But for the curious developer, a good Sitecore API practice comes in handy and can help us exercise our minds on how to solve this need with a simple utility ASPX page, where a Sitecore power user can get access to it and use it in a simplistic way.
Exercise requirements
1. Data needs to come from the database, not from the Sitecore index
2. The utility page must ask:
- From what database it should read the data
- What is the Sitecore root item path, from where it should start looking Sitecore items
- The path of the item s template it inherits from
3. Export the data in CSV format
4. No code behind, for easy push/deployment to any environment without impacting its operation
Solution
<%@ Page language="C#" EnableEventValidation="false" AutoEventWireup="true" EnableViewState="true" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="Sitecore" %>
<%@ Import Namespace="Sitecore.Data" %>
<%@ Import Namespace="Sitecore.Data.Items" %>
<%@ Import Namespace="Sitecore.Data.Fields" %>
Content Item Report
Content Item Report
We set up all this code in an ASPX page, and we drop it into /Sitecore/admin folder on our Sitecore instance and test it:
This is just the tip of the iceberg in terms of report functionality, as there are many areas of improvement! But we ll get into that in another post.
Happy coding!