© 2019 by Sayantan Basu

  • Sayantan Basu

Search Components/Pages from Tridion CMS and Generate Report in .csv format

In this blog I have described how to get the list of components and pages (from CMS) created by an individual(Author)/ everyone  in a specific publication and create a simple report in .csv format using core service.

To provide more flexibility to the user, I have created a small web application and hosted it as a custom page in Tridion.


In my previous blog I have already shared how to connect to CoreService API, so I will skip that part.

As a first step,  On Page load I have collected the User and Publication details  from CM database using core service and store them in a static list. Then I have populated the Title attribute of User and Publication in the Drop-down list.

private void Page_Load(object sender, EventArgs e) { LabelMessage.Text = “”; if (!IsPostBack) { client = Utility.CoreServiceSource; var filter = new UsersFilterData();IdentifiableObjectData[] UserList = client.GetSystemWideList(filter);var filter1 = new PublicationsFilterData();IdentifiableObjectData[] PublicationList = client.GetSystemWideList(filter1); PopulateDropDownListUser(UserList); PopulateDropDownListPublication(PublicationList); } }

Here is the sample code of populating the drop-down list for publication-

private void PopulateDropDownListPublication(IdentifiableObjectData[] publicationList) { List<string> publicationName = new List<string>(); foreach (var item in publicationList) { PublicationDetails pub = new PublicationDetails(); pub.PublicationId = item.Id; pub.PublicationName = item.Title; publications.Add(pub); } publicationName = (from publication in publications select publication.PublicationName).ToList(); publicationddList.DataSource = publicationName; publicationddList.DataBind(); publicationddList.Items.Insert(0, new ListItem(“–select Publication–“, “”)); }

similarly you can create the method to populate the drop-down list for User.

Next user will choose the data and click on Download List button. Once you have that user input ready, just pass those values as a filter criteria and get the search result.

Here is the sample code for that –

SearchQueryData filter = new SearchQueryData(); filter.Author = new LinkToUserData() { IdRef = tcmId }; filter.ItemTypes = new[] { ItemType.Component,ItemType.Page }; filter.SearchIn = new LinkToIdentifiableObjectData() { IdRef = pubtcmId }; filter.IncludeLocationInfoColumns = true; XElement result = client.GetSearchResultsXml(filter); if (result.FirstNode != null) { ParseResult(result); } else { LabelMessage.Text = “No item created by ” + selectedUser; }

Here I have used SerachQueryData for the filtration.  And as a criteria I have passed the Author tcm id, Publication tcm id and Item Types. There are lot of other options available, so you can provide more criteria as per your requirement.

Finally you have the component list is XML format, now parse that XML and generate a .csv file.

Sample Code –

private void ParseResult(XElement result) { string pubtcmId = (from pub in publications where pub.PublicationName == selectedPublication.ToString() select pub.PublicationId).FirstOrDefault().ToString(); foreach (var item in result.Elements()) { ItemDetails itemDetail = new ItemDetails(); itemDetail.ItemId = item.Attribute(“ID”).Value; itemDetail.ItemLocation = String.Format(item.Attribute(“Path”).Value.ToString(), Encoding.Default); itemDetail.ItemType = ((item.Attribute(“Type”).Value == “16”)? “Component” : “Page”); itemDetail.ItemTitle = item.Attribute(“Title”).Value; items.Add(itemDetail); } ExportCSVFile(items); }

I have also uploaded the application source code in the git-hub. Here is the link to download the full Source code.

#TridionCustomPage #TridionCoreService #SdLTridion2013 #SDLWeb8 #SDLTridion2013SP1