其他转换器设置控件

上次修改时间: 2010年3月8日

适用范围: SharePoint Server 2010

如果需要创建自定义文档转换器,则除了 Microsoft SharePoint Server 2010 文档配置设置页上显示的选项,可能还需要向管理员收集更多信息。若要收集这些信息,可以指定要在现有转换器页上承载的自定义 .ascx 控件。

若要指定自定义配置设置控件,请将文档转换器定义中的 ConverterSpecificSettingsUI 元素设置为要使用的 .ascx 控件的文件名。这是一个可选的元素。必须在 ConverterSettingsForContentType 元素中指定一个转换器配置设置页才能承载该控件。

有关文档转换器定义的详细信息,请参阅文档转换器定义架构

所指定的配置设置页必须包含实现 IDocumentConverterControl 接口的代码,如下例所示。

此外,控件返回的字符串必须是有效的 XML 节点。

public class XslApplicatorSettingsControl : UserControl, 
             IDocumentConverterControl
{
  /// <summary>
  /// XSLT asset selector control
  /// </summary>
  public AssetUrlSelector assetSelectedXsl;

  /// <summary>
  /// Validator to make sure user picks XSLT
  /// </summary>
  public FileExtensionValidator fileExtensionValidator;

  /// <summary>
  /// Input form section, used only to get the display title of the 
      section.
  /// </summary>
  public InputFormSection inputSection;

  protected override void OnLoad(EventArgs e)
  {
    base.OnLoad(e);
    this.fileExtensionValidator.ControlToValidate = 
      this.assetSelectedXsl.ID;
  }

  private const string ConverterSettingsXmlName = 
      "XslApplicatorConverterSettings";

  /// <summary>
  /// Property that is the interface to the outer world - get/set a 
      string 
      that persists the settings
  /// </summary>
  public string ConverterSettings
  {
    get
    {
      StringBuilder sb = new StringBuilder("<", 256);
      sb.Append(ConverterSettingsXmlName);
      sb.Append(" Version=\"1\" >");

      sb.Append("<FilePlaceHolder Url=\");
      sb.Append(this.assetSelectedXsl.AssetUrl);
      sb.Append("\">");

      sb.Append("</FilePlaceHolder>");

      sb.Append("</");
      sb.Append(ConverterSettingsXmlName);
      sb.Append(">");

      return sb.ToString();
    }

    set
    {
      if (!String.IsNullOrEmpty(value))
      {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(value);

          RcaUtilities.FilePlaceHolderElementName;
        XmlNodeList cl = xmlDoc.SelectNodes("//FilePlaceHolder");

        if (cl.Count > 0)
        {
          XmlNode node = cl[0];
          XmlAttribute attribute = node.Attributes["Url"];
          string fileUrl = String.Empty;

          if (attribute != null && attribute.Value != null)
          {
            fileUrl = attribute.Value;
          }

          this.assetSelectedXsl.AssetUrl = fileUrl;
        }
      }
    }
  }

  /// <summary>
  /// Implement setter to fulfill the interface
  /// </summary>
  public SPContentType ContentType
  {
    set
    {
    }
    get
    {
      return null;
    }
  }

  /// <summary>
  /// This control always requires configuration
  /// </summary>
  public bool RequiresConfiguration
  {
    get
    {
      return true;
    }
  }

  /// <summary>
  /// Display title, used to direct user to this section in the page in 
      case of validation errors
  /// </summary>
  public string SectionDisplayTitle
  {
    get
    {
      return this.inputSection.Title;
    }
  }
Public Class XslApplicatorSettingsControl
    Inherits UserControl
    Implements IDocumentConverterControl
  ''' <summary>
  ''' XSLT asset selector control
  ''' </summary>
  Public assetSelectedXsl As AssetUrlSelector

  ''' <summary>
  ''' Validator to make sure user picks XSLT
  ''' </summary>
  Public fileExtensionValidator As FileExtensionValidator

  ''' <summary>
  ''' Input form section, used only to get the display title of the 
  ''' section.
  ''' </summary>
  Public inputSection As InputFormSection

  Protected Overrides Sub OnLoad(ByVal e As EventArgs)
    MyBase.OnLoad(e)
    Me.fileExtensionValidator.ControlToValidate = Me.assetSelectedXsl.ID
  End Sub

  Private Const ConverterSettingsXmlName As String = "XslApplicatorConverterSettings"

  ''' <summary>
  ''' Property that is the interface to the outer world - get/set a 
  ''' string 
  ''' that persists the settings
  ''' </summary>
  Public Property ConverterSettings() As String
    Get
      Dim sb As New StringBuilder("<", 256)
      sb.Append(ConverterSettingsXmlName)
      sb.Append(" Version=""1"" >")

      sb.Append("<FilePlaceHolder Url=\")
      sb.Append(Me.assetSelectedXsl.AssetUrl)
      sb.Append(""">")

      sb.Append("</FilePlaceHolder>")

      sb.Append("</")
      sb.Append(ConverterSettingsXmlName)
      sb.Append(">")

      Return sb.ToString()
    End Get

    Set(ByVal value As String)
      If Not String.IsNullOrEmpty(value) Then
        Dim xmlDoc As New XmlDocument()
        xmlDoc.LoadXml(value)

          RcaUtilities.FilePlaceHolderElementName
        Dim cl As XmlNodeList = xmlDoc.SelectNodes("//FilePlaceHolder")

        If cl.Count > 0 Then
          Dim node As XmlNode = cl(0)
          Dim attribute As XmlAttribute = node.Attributes("Url")
          Dim fileUrl As String = String.Empty

          If attribute IsNot Nothing AndAlso attribute.Value IsNot Nothing Then
            fileUrl = attribute.Value
          End If

          Me.assetSelectedXsl.AssetUrl = fileUrl
        End If
      End If
    End Set
  End Property

  ''' <summary>
  ''' Implement setter to fulfill the interface
  ''' </summary>
  Public Property ContentType() As SPContentType
    Set(ByVal value As SPContentType)
    End Set
    Get
      Return Nothing
    End Get
  End Property

  ''' <summary>
  ''' This control always requires configuration
  ''' </summary>
  Public ReadOnly Property RequiresConfiguration() As Boolean
    Get
      Return True
    End Get
  End Property

  ''' <summary>
  ''' Display title, used to direct user to this section in the page in 
  ''' case of validation errors
  ''' </summary>
  Public ReadOnly Property SectionDisplayTitle() As String
    Get
      Return Me.inputSection.Title
    End Get
  End Property

外部文件设置

允许用户指向文件的 .ascx 控件带来了一个特殊的难题:转换器在自己的进程中运行,无权访问服务器上的文件,因此必须将任何文件的内容都读入传递至转换器的配置信息中。文档转换器基础结构加入了一种机制,按照这种机制,在调用转换器时引用文件实际上会将该文件的内容拉入配置设置信息中。

若要将文件的内容读入至配置设置信息中,请向配置设置 XML 添加 FilePlaceholder 元素。此元素只有一个属性,即 Url,表示文件的 URL,您要将其内容读入传递至转换器的配置设置信息中。

例如:

<FilePlaceHolder Url="myUrlHere"><\FilePlaceHolder>

当启动文档转换时,文档转换基础结构为配置设置中的每个 FilePlaceHolder 元素执行以下操作:

  • 解析 URL。

  • 打开指定的文件。

  • 以 base64 编码将文件内容编码,并将编码后的内容放置到 FilePlaceHolder 元素节点中。

当转换器收到配置设置时,它必须转换文件的内容。为此,转换器必须访问 FilePlaceHolder 节点并转换其内容。例如:

byte[] fileContent = System.Convert.FromBase64String(node.InnerXml);
Dim fileContent() As Byte = System.Convert.FromBase64String(node.InnerXml)

有关传递至转换器的配置设置 XML 的详细信息,请参阅文档到页面转换器配置设置架构

请参阅

概念

文档转换器概述

文档转换器

文档转换器部署

文档转换器定义架构

文档到页面转换器配置设置架构