Search
Website

Microsoft Content Management Server Development : Building SharePoint Web Parts - Building the Data Layer

Published 3 min readBy Programming4us Editorial Team

The Web Part will generate XML that lists all the ChannelItems in the configured StartChannel using the configured SortOrder. The use of XML facilitates the separation of the presentation layer from the data layer. Our data layer will be another class in the Web Part project but it could have been placed in an entirely different assembly for further abstraction.


  1. In Visual Studio .NET, right-click on the ExtensibleMCMSPageListingWebPart project and choose Add New Item.

  2. Under the Add sub-menu, choose Add New Class.

  3. Enter NavigationWebPartMCMSIntegration as the name and click OK.

  4. Add a reference to Microsoft.ContentManagement.Publishing.dll. As with other library files from the Microsoft.ContentManagement namespace, this is found in the <install directory>/Microsoft Content Management Server/server/bin directory.

  5. You should now have the empty NavigationWebPartMCMSIntegration class on screen. Add the following namespaces to the new class file:

    using System;
    using Microsoft.ContentManagement.Publishing;
    							using System.Security.Principal;
    							using System.Web;
    							using System.Xml;
    							using System.IO;
  6. We will now add the GetListing() method, which connects to MCMS and generates the XML listing all the ChannelItems:

    public static System.Xml.XmlDocument GetListing(string StartChannelPath,
                               NavigationWebPart.enmSortBy sortBy)
    {
      // Get the channel from the searches object
      Channel startChannel = CmsHttpContext.Current.Searches.GetByPath(
                                               StartChannelPath) as Channel;
    
      // Was the channel found?
      if (startChannel != null)
      {
        // Get all Children from the Start Channel
        ChannelAndPostingCollection _Children = startChannel.AllChildren;
    
        // Sort the Items
        _Children = SortMCMSChannelItems(sortBy, _Children);
    
        // Declare the string writer and the XmlTextWriter
        StringWriter tw = new StringWriter();
        XmlTextWriter _Writer = new XmlTextWriter(tw);
    
        // Write the <Channel> element
        _Writer.WriteStartDocument();
        _Writer.WriteStartElement("Channel");
        _Writer.WriteAttributeString("path", startChannel.Path);
        _Writer.WriteAttributeString("displayname", startChannel.DisplayName);
    
        // Loop through all the channel items
        foreach (ChannelItem _ChannelItem in _Children)
        {
          // Write the ChannelItem element and set its attributes
          _Writer.WriteStartElement("ChannelItem");
          _Writer.WriteAttributeString("path", _ChannelItem.Path);
          _Writer.WriteAttributeString("url", _ChannelItem.Url);
          _Writer.WriteAttributeString("displayname",
                              _ChannelItem.DisplayName);
    
          // Check what type of Channel Item it is and set the type attribute
          if (_ChannelItem is Posting)
          {
            _Writer.WriteAttributeString("type", "Posting");
          }
          else
          {
            _Writer.WriteAttributeString("type", "Channel");
          }
          _Writer.WriteEndElement();
        }
        // Close the <Channel> element & finalize the XML document
    
        _Writer.WriteEndElement();
        _Writer.WriteEndDocument();
        _Writer.Flush();
        _Writer.Close();
        XmlDocument _ChannelItemsAsXml = new XmlDocument();
    
        // Load the text from the string writer into the XML document
        _ChannelItemsAsXml.LoadXml(tw.ToString());
    
        return(_ChannelItemsAsXml);
      }
      else
      {
        throw new Exception("The Channel (" + StartChannelPath
                          + ") could not be found or the user does not have "
                          + "subscriber rights on the channel.");
      }
    }
    
    					  

To sort the ChannelAndPostingCollection returned by MCMS, we add the SortMCMSChannelItems() method:

public static ChannelAndPostingCollection SortMCMSChannelItems(
                                 NavigationWebPart.enmSortBy SortBy,
                                 ChannelAndPostingCollection ChildItems)
{
  switch(SortBy)
  {
    case NavigationWebPart.enmSortBy.ChangeDate:
      ChildItems.SortByChangeDate();
      break;

    case NavigationWebPart.enmSortBy.DisplayName:
      ChildItems.SortByDisplayName();
      break;

    case NavigationWebPart.enmSortBy.DisplayPath:
      ChildItems.SortByDisplayPath();
      break;

    case NavigationWebPart.enmSortBy.ExpiryDate:
      ChildItems.SortByExpiryDate();
      break;

    case NavigationWebPart.enmSortBy.Importance:
      ChildItems.SortByImportance();
      break;

    case NavigationWebPart.enmSortBy.Ordinal:
      ChildItems.SortByOrdinal();
      break;

    case NavigationWebPart.enmSortBy.StartDate:
      ChildItems.SortByStartDate();
      break;

    default:
      break;
  }
  return(ChildItems);
}

					  


Now that our helper data layer class is in place, we must also create a test XML file in the solution that will be deployed to SharePoint as a resource.

  1. In Visual Studio .NET, right-click on the ExtensibleMCMSPageListingWebPart project and choose Add New Item.

  2. From the Categories pane, select Data and then select XML File.

  3. Enter TestXml.xml as the name and click Open.

XML and XSLT are case sensitive so be sure to use the correct case when adding elements within these files.


Enter the following XML into the file. This is similar XML to that which will be generated by the GetListing method.

<?xml version="1.0" standalone="no" ?>
<Channel path="/Channels/" displayname="Channels">
  <ChannelItem path="/Channels/Site/1" url="/Site/1.htm"
        displayname="Display 1" type="Posting" />
  <ChannelItem path="/Channels/Site/2" url="/Site/2.htm"
        displayname="Display 2" type="Posting" />
  <ChannelItem path="/Channels/Site/3" url="/Site/3.htm"
        displayname="Display 3" type="Posting" />
  <ChannelItem path="/Channels/Site/4" url="/Site/4.htm"
        displayname="Display 4" type="Posting" />
  <ChannelItem path="/Channels/Site/5" url="/Site/5.htm"
        displayname="Display 5" type="Posting" />
  <ChannelItem path="/Channels/Site/6" url="/Site/6.htm"
        displayname="Display 6" type="Posting" />
</Channel>

This code could be extended to mimic the ChannelItems from child channels as well, like so:

<?xml version="1.0" standalone="no" ?>
<Channel path="/Channels/" displayname="Channels">
  <ChannelItem path="/Channels/Site/5" url="/Site/5.htm"
        displayname="Display 5" type="Posting" />
  <ChannelItem path="/Channels/Site/6" url="/Site/6"
          displayname="Display 6" type="Channel">
  <ChannelItem path="/Channels/Site/6/1" url="/Site/6/1.htm"
          displayname="Display 6.1" type="Posting" />
  </ChannelItem>
</Channel>

But for now we will keep it simple and only list the items for the immediate channel.