Here is another article in the MCMS 101 series. This one deals with getting a list of all the Channels / Postings that contain a specific Customer Property.
Remember that custom property should only be used for searches that are not very extensive and return a small set. If your have requirements of frequent searches on Meta Information of Channels(folders) or Postings(WebPages) then a better solution is to have a User Database linked to it. I will talk about it in my future Posts.
It is good idea to keep the name of all your Custom properties as Structures in a .Net Class.I usually call this class Global.cs This way you can maintain it from a single place and make change if required.
Sample of Global.cs Class
using System;
namespace Jawad.BusinessLogic
{
///
/// Contains the constants and Global entities for the Lms Suite of Sites.
///
public class Global
{
public Global()
{
}
public struct CMSCustomAttributes
{
public const string CategoryName = "CATEGORY_NAME"; // To match Category Name from the Database
public const string GroupNo = "GROUP_NO"; // To match Database Group No
}
// Name of JavaScript Blocks for RegisterJavaScript Function ....NOT MCMS related
public struct JavaScriptBlocks
{
public const string CalendarScript = "CalendarScript";
public const string EditLookUpScript = "EditLookUpScript";
}
public struct PlaceholderName
{
public const string PlaceholderName_Url_plLink = "plLink";
}
}
}
You can use the above class to also keep Session variable keys etc at one place ....
Now to the Real task at Hand to get the list of Channels and Postings by Custom Property ....
using System;
using System.Collections ;
using Microsoft.ContentManagement.Publishing;
namespace Jawad.Ces.Lms.LmsLogic.CmsUtils
{
///
/// Summary description for CMSFunctions.
///
public class CMSFunctions
{
public CMSFunctions()
{
//
// TODO: Add constructor logic here
//
}
///
/// This function return an array list of all the Channels or Postings containing desired Custom Property
///
/// Name of the Custom Property to Search for
/// Specific value you are looking for or "ALL: if you want object for all combinations
/// This should be CmsHttpContext.Current for Wen and ApplicationContext for Windows Console App
/// True if Chnnels are requited; false if only Postings ate required.
/// Array list of all the Channels/Posting meeting search criteria
public ArrayList GetAllItemsByCustomPropertyName(string PropertyName, string propertyValue, CmsContext context, bool Channels)
{
string customPropertyValue = null;
ArrayList urlList = new ArrayList();
HierarchyItemCollection itemColl = (Channels == true) ? (HierarchyItemCollection)context.Searches.GetChannelsByCustomProperty (PropertyName):
(HierarchyItemCollection)context.Searches.GetPostingsByCustomProperty(PropertyName);
if ( itemColl != null )
{
foreach(HierarchyItem item in itemColl)
{
customPropertyValue = (item is Channel) ? ((Channel)item).CustomProperties[PropertyName].Value.ToLower():
((Posting)item).CustomProperties[PropertyName].Value.ToLower();
if (( customPropertyValue == propertyValue.ToLower ())
|| ( propertyValue.ToLower () == "all"))
{
urlList.Add( item);
}
}
}
return urlList;
}
}
Now if I want to get list of all the channels that have specific value
private void Page_Load(object sender, System.EventArgs e)
{
CMSFunctions cms = new CMSFunctions ();
StringBuilder sb = new StringBuilder ();
ArrayList list = cms.GetAllItemsByCustomPropertyName (Jawad..BusinessLogic.Global.CMSCustomAttributes
.CategoryName,"ALL",CmsHttpContext.Current,true); // So for All Categories I can specify specific one like MiniVans etc
foreach(Channel item in list) // Here Channel ch because we specified true for channels parameter above if they are postings then Posting item {
sb.Append ("<a href='");
sb.Append (item.Url);
sb.Append ("'>");
sb.Append (item.DisplayName );
sb.Append ("</a><BR>");
}
Response.Write (sb.ToString ()); // You can here do What ever you want with the string ....
}
Instead of foreach you can bind it to DatGrid , Repeater or some other Control
Note: We are using GetChannelsByCustomProperty with only 1 paprameter that returmns all the Channels regardles of their value. This impelmentation help this solution to be flexible so that you can later filter on muliple Values or Get All values their is another overloaded method that take the value of the property as well and will return only the channels that match the property value so you don't need to filter it in foreach loop . Same applies to GetPostingsByCustomProperty.
Andrew Connell has put a much better solution for implementing Meta Data for your Postings and Channels. It is hightly recommended that you use solutions like these rather then using Cutome Properties to search for a particular Channel or Posting. Andrew's Project is called CustomPropertyDbEx. You can also specify Site (Global) Meta information using CustomPropertyEx