Jawad Khan

Jawad's Lodge - The willingness to torture yourself before others is what makes a developer truly a unique breed.
posts - 45, comments - 167, trackbacks - 148

My Links

News

Archives

Post Categories

Image Galleries

MCMS 101 - Getting/Searching list of Channels or Postings by Custom Property ....

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

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Wednesday, May 18, 2005 4:35 PM | Filed Under [ Microsoft Content Management Server ]

Feedback

Gravatar

# re: MCMS 101 - Getting/Searching list of Channels or Postings by Custom Property ....

You might want to add a note that the performance of custom property searches is very bad. You should avoid using it wherever possbile.

Cheers,
Stefan
5/19/2005 5:51 AM | Stefan [MSFT]
Gravatar

# re: MCMS 101 - Getting/Searching list of Channels or Postings by Custom Property ....

Good stuff, I need to use the HiearchyItemCollection in order to retrieve a list of Channels _and_ Postings and then sort them by name. I have used the following:
HierarchyItemCollectio myCollection = (HierarchyItemCollection)CmsHttpContext.Current.Channel.[Postings|Channels];

to get a two collections of HierarchyItem. What I no need to do is join these together so tha I cna take advantage of the SortBy methods, but I cna not find a way to do this. Am I going to have to tip averythign in to an Array or is there a way of mergin two HierarchyItemCollections?

Many thanks in anticipation

Matt
6/30/2005 6:28 AM | Matt Nield
Gravatar

# re: MCMS 101 - Getting/Searching list of Channels or Postings by Custom Property ....

Matt you need to combine the two as follows :

ChannelAndPostingCollection coll = (ChannelAndPostingCollection)LeftNavChannels.Union(MaketingChannels.Channels);

HierarchyItemCoolection doesn't have a Union method ...
6/30/2005 9:42 AM | Jawad Khan
Gravatar

# re: MCMS 101 - Getting/Searching list of Channels or Postings by Custom Property ....

Hurrah!!

Quite handy that, now I can rmove my nasty little Object[] and bubble sort :)

Thanks muchly

Matt
6/30/2005 9:56 AM | Matt Nield
Gravatar

# Javascript in MCMS

Can any one tell me how to use the javascript in the postings published with Content Management Server with the fully functional javascript functions working in the published posting.
12/11/2005 5:47 AM | Gopi
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: