Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Advanced Microsoft Content Management Server Development

You're reading from   Advanced Microsoft Content Management Server Development Working with the Publishing API, Placeholders, Search, Web Services, RSS, and Sharepoint Integration

Arrow left icon
Product type Paperback
Published in Nov 2005
Publisher Packt
ISBN-13 9781904811534
Length 544 pages
Edition 1st Edition
Concepts
Arrow right icon
Toc

Table of Contents (21) Chapters Close

Advanced Microsoft Content Management Server Development
Credits
About the Authors
About the Reviewers
1. Building CMS Explorer FREE CHAPTER 2. Managing Channels and Postings with the PAPI 3. Managing Templates, Template Galleries, and Resources 4. Preparing Postings for Search Indexing 5. Searching MCMS with SharePoint 6. Publishing Content Between MCMS and SharePoint 7. Building SharePoint Web Parts 8. Useful Placeholder Controls 9. Validating Placeholder Controls 10. Staging Static Pages 11. InfoPath with MCMS Web Services 12. MCMS and RSS 13. Essential How-Tos, Tips, and Tricks 1. Setting up MCMS and SPS on the Same Virtual Server 2. MCMS Connector for SharePoint Technologies 3. Installing the Tropical Green Website Index

Using Reflection to List Properties and their Values


In the Properties dialog, we are going to list all properties of the selected object. Usually, when you want to access a property value in code, you simply type the object name, followed by the period key and then the property name.

This is fine when you are just dealing with one or two properties, but there are over 40 properties for the channel object alone. In order to display all its property values in this way, you would have to type in at least 40 lines of code. And should there be future upgrades to the PAPI, the list may grow even longer. The good news is there’s a short cut—.NET Reflection.

Reflection is a technique used to access information about a class, such as its methods, properties, events and even information about its assembly. To implement reflection, use the GetType() method of the object (inherited from System.Object), which returns an object of type System.Reflection.Type. The System.Reflection.Type class contains methods that retrieve metadata about the object’s class. For example, given any object, you can get a list of its methods by calling:

MyObject.GetType().GetMethods();

and to get a list of its properties, you could call:

MyObject.GetType().GetProperties();

As you can see, reflection is a powerful feature. In this example, instead of writing 40+ lines of code to list the properties of a ChannelItem object, we will simply use reflection to iterate through each property and display its value.

Let’s display the list of properties in a DataGrid. To start, add a new web form to the CMSExplorer project. Name the new web form Properties.aspx. Toggle to HTML view and add a couple of headings to the form to describe what it does:

<form>
<h1>Properties of <asp:Literal Runat="server" ID="litCurrentItem"/></h1>
<h2>List of all Properties and their values</h2></form>

In the web form’s code-behind file, import the Microsoft.ContentManagement.Publishing and System.Reflection namespaces and add the following code in the Page_Load() event handler:

. . . code continues . . .
// MCMS PAPI
using Microsoft.ContentManagement.Publishing;
// for reflection
using System.Reflection;

namespace CMSExplorer
{
  /// <summary>
  /// Summary description for Properties.
  /// </summary>
  public class Properties : System.Web.UI.Page
  {
    HierarchyItem hItem; // the current item

    private void Page_Load(object sender, System.EventArgs e)
    {
      CmsHttpContext cmsContext = CmsHttpContext.Current;
hItem = null;
string cmsObjectGuid = "";
if (Request.QueryString["CMSObjectGuid"]!=null)
{
// template gallery items and resource gallery items
cmsObjectGuid = Request.QueryString["CMSObjectGuid"];
hItem = cmsContext.Searches.GetByGuid(cmsObjectGuid);
}
else
{
// channels and postings
hItem = cmsContext.ChannelItem;
}
// list all properties and their values in the grid
if (hItem!=null)
{
litCurrentItem.Text = hItem.Path;
ListProperties();
}
    }
  . . . code continues . . .
  }
}

The code gets a reference to the HierarchyItem whose properties you wish to view. For template galleries, templates, resource galleries, and resources, this is obtained by getting the GUID from the CMSObjectGuid querystring parameter and using the Searches.GetByGuid() method. Channels and postings are obtained from the CmsHttpContext.Current.ChannelItem property.

In Design view, drag and drop the Styles.css file and DataGrid onto the Properties.aspx web form and give the DataGrid the following property values:

Property

Value

Auto Format

Simple 3

Font-Size

10pt

ID

DataGrid1

Below the Page_Load() event handler, add the ListProperties() method. The method first creates a DataTable containing the following columns:

  • The property name

  • The property value

  • Whether or not the property can be written to

We use the GetType.GetProperties() method to retrieve a collection of all properties associated with the hierarchy item. Next, iterate through each property of the current ChannelItem and add each one as a row to the DataTable. Notice that the property value is obtained by calling PropertyInfo.GetValue() and passing in the hierarchy item as an input parameter. Finally, we bind the DataTable to the DataGrid.

private void ListProperties()
{
  // display the property names and values for the current channelitem
  DataTable dt = new DataTable();
  DataRow dr;

  // add columns for the property name, property value and
  // the boolean that indicates if the property is writable
  dt.Columns.Add(new DataColumn("PropertyName"));
  dt.Columns.Add(new DataColumn("PropertyValue"));
  dt.Columns.Add(new DataColumn("CanWrite"));

  // use reflection to iterate through a list of the object's properties
  foreach(PropertyInfo pi in hItem.GetType().GetProperties())
  {
    if (pi.PropertyType.ToString().StartsWith("System"))
    {
      dr = dt.NewRow();
      dr[0] = pi.Name;
      Object piObject = pi.GetValue(hItem, null);
      if (piObject!=null)
      {
        dr[1] = piObject.ToString();
      }
      dr[2] = pi.CanWrite.ToString();
      dt.Rows.Add(dr);
    }
  }

  // bind the datatable to the datagrid
  DataGrid1.DataSource = dt.DefaultView;
  DataGrid1.DataBind();
}

When you are done, save and build the solution. To see the code in action:

  1. Access http://localhost/CmsExplorer.

  2. Click on the Edit button on the row corresponding to the Tropical Green channel.

  3. Click Properties.

The Properties page opens as shown on the facing page. Notice that all properties of the Tropical Green channel are displayed! The page also works when viewing the properties of other object types like template galleries and resource galleries.

You have been reading a chapter from
Advanced Microsoft Content Management Server Development
Published in: Nov 2005
Publisher: Packt
ISBN-13: 9781904811534
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image