programming4us
programming4us
WEBSITE

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

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
4/30/2013 7:28:48 PM

The Web Part needs to override methods provided by the SharePoint Framework in order to ‘hook in’ to the page request. This custom code will then be executed, and will call our GetListing() method to connect to MCMS and generate the XML file of the channel items. After that, the page and sub-channel listing will be displayed.

The chart below shows an overview of how our code will work:

As we are building the Web Part as a server control, we must declare our controls and instantiate them by overriding the CreateChildControls method in NavigationWebPart.cs:

#region declarations - Web controls

protected System.Web.UI.WebControls.Xml XmlXslWebControl;

#endregion

#region Preparing the controls

protected override void CreateChildControls()
{
  XmlXslWebControl = new System.Web.UI.WebControls.Xml();
  this.Controls.Add(XmlXslWebControl);
}

#endregion

The System.Web.UI.WebControls.Xml Web Control can transform XML using specified XSLT, which is more efficient than developing the XML/XSLT by hand.

The RenderWebPart() method needs to be overridden to ensure that the rest of our code is being executed. Let’s add the code to the RenderWebPart() method to generate the MCMS listing:

#region Methods

protected override void RenderWebPart(HtmlTextWriter output)
{
  // Check our child controls are created OK
					EnsureChildControls();
					try
					{
					// Get the Page listing from MCMS
					ExecuteMCMSListing(output);
					}
					catch(Exception ex)
					{
					// In production show only a short message with no internal exception
					// details
					if (this.Mode == enmMode.Production)
					{
					// Suppress Exceptions
					output.Write("An Error Occurred in the Navigation Web Part.");
					}
					else
					{
					// Output detailed exception message (escape characters)
					output.Write(SPEncode.HtmlEncode(ex.Message + "," + ex.Source + ","
					+ ex.StackTrace));
					}
					}
}

					  

The next method, ExecuteMCMSListing(), determines which mode the Web Part is running in and retrieves the channel listing data. The channel listing data can be read from MCMS or can be loaded from the test XML file on the file system.

/// <summary>
/// Executes the MCMS listing code
/// </summary>
private void ExecuteMCMSListing(HtmlTextWriter output)
{
  // Check the mode we are in and perform the correct action
  switch (this.Mode)
  {
    case enmMode.Production:
    case enmMode.DebugShowException:

      // Set up the Xml/Xsl Webcontrol and render its output to the
      // HtmlTextWriter
      XmlXslWebControl.Transform = GetTransform();
      XmlXslWebControl.Document = NavigationWebPartMCMSIntegration.GetListing(
                                              this.StartChannel, this.SortBy);
      XmlXslWebControl.RenderControl(output);
      break;

    case enmMode.DebugShowXml:

      System.Xml.XmlDocument _Listing =
                NavigationWebPartMCMSIntegration.GetListing(this.StartChannel,
                                                            this.SortBy);
      // Output the escaped XML directly to the HtmlTextWriter
      output.Write(SPEncode.HtmlEncode(_Listing.OuterXml));

      break;

    case enmMode.DebugWithTestData:

      // Get the Test XML Data & load into an XmlDocument
      System.Xml.XmlDocument _DataDocument = new System.Xml.XmlDocument();
      _DataDocument.LoadXml(this.TestXml);

      // Setup XML/XSLT Web control and render its output to HtmlTextWriter
      XmlXslWebControl.Transform = GetTransform();
      XmlXslWebControl.Document = _DataDocument;
      XmlXslWebControl.RenderControl(output);
      break;

    default:
      throw new Exception("Invalid Mode!");  }
}

					  

To retrieve the XslTransform object, we will create a helper method called GetTransform(). This method will return an XslTransform object by loading the XSLT stylesheet from the file system into an XmlDocument object. The XmlDocument object is then loaded into an XslTransform object and returned:

/// <summary>
/// Get the XSLT as a System.Xml.XslTransform object
/// </summary>
/// <returns>The System.Xml.XslTransform object</returns>
private System.Xml.Xsl.XslTransform GetTransform()
{
  // Get the XSLT and load it into an XML document
  System.Xml.XmlDocument _TransformDoc = new System.Xml.XmlDocument();
  _TransformDoc.LoadXml(this.PresentationXsl);

  System.Xml.Xsl.XslTransform _Transform = new System.Xml.Xsl.XslTransform();

  // Load our XSLT
  _Transform.Load(_TransformDoc.CreateNavigator(), null, null);

  return(_Transform);
}
#endregion

					  

As we are dynamically adding controls to the Controls collection, the first call in RenderWebPart() should be to EnsureChildControls(). The EnsureChildControls() method calls CreateChildControls() if the ChildControlsCreated property is false.


We must also define the XSLT stylesheet that will be used to transform our XML into HTML.

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

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

  3. Enter DefaultXsl.xslt as the name and click Open. Enter the following XSLT in the code panel:

    <?xml version="1.0" standalone="no"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
      <xsl:template match="/">
        <b>
          <a href="{@url}">
            <xsl:value-of select="/Channel/@displayname" />
          </a>
        </b>
        <br />
        <ul>
          <xsl:apply-templates />
        </ul>
      </xsl:template>
    
      <xsl:template match="//ChannelItem">
        <li><a href="{@url}"><xsl:value-of select="@displayname" /></a></li>
        <ul><xsl:apply-templates /></ul>
      </xsl:template>
    </xsl:stylesheet>

At this point let’s build the entire solution to see if there are any compilation errors.

Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
programming4us
 
 
programming4us