ACS Blogs

A blog site for Architecting Connected Systems staff to tell the world about their exploits in
SharePoint 2007/2010, Windows Workflow Foundation (3.0/4.0) and other great technologies!
Welcome to ACS Blogs Sign in | Join | Help
in Search

CJG

Building Custom SharePoint 2007 Document Converters

Module #09: Custom Document Converter Lab #02 (Optional)

 

 

Course:           Programming Microsoft Office SharePoint Server


Estimated Time to Complete:  45 minutes


Objectives:

·         Create/Deploy a Custom Converter

Operating Notes:  

 

·         Document Converters do not run on Domain Controllers! Therefore, you will be able to create and install the converter, but not test it with this image


Deliverables:

·         None

 

Overview:         Learn to create your own Document Converter

Exercise 1 – Create the Converter

Purpose:         Create a converter to transform a document

Result:           
A document converter

Task 1 – Create a Console Application

  1. Open Visual Studio
  2. Create a new project
    • Click “File->New->Project”
    • Select “Console Application”
    • For name, type “MyConverter”
    • For location, type “D:\Lab Work”

    • Click “Ok”
  1. Add a reference to WindowsBase
    • Right click the project, select “Add reference”
    • On the .NET tab, select “WindowsBase”
    • Click “Ok”

Task 2 – Update the code

  1. Update the program.cs file to the following:


using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.IO.Packaging;

using System.Xml;

 

namespace MyConverter

{

    class ConverterShell

    {

        #region Private Members

 

        private string[] inputArgs = null;

        private string localPathtoFile = null;

        private string localSettings = null;

        private string localLogPath = null;

        private string localOutputFullPath = null;

        private const string ConverterName = "Xml to Xml";

        private const string InputExtension = ".xml";

        private const string OutputExtension = ".xml";

        private const string InputArgInText = "-in";

        private const string InputArgSettingsText = "-settings";

        private const string InputArgOutText = "-out";

        private const string InputArgLogText = "-log";

        private StreamWriter streamLog = null;

 

        #endregion

        #region main

        static void Main(string[] args)

        {

            // Create New Converter

            ConverterShell localConverter = new ConverterShell();

            localConverter.inputArgs = args;

            // Get command line

            localConverter.GetCommandLine(localConverter.inputArgs);

            // Verify the files and copy

            if (localConverter.VerifyFilesExist())

            {

XmlToXml myConverter = new XmlToXml(localConverter.localOutputFullPath);

            }

            return;

        }

        #endregion

 

        #region support routines

        bool VerifyFilesExist()

        {

            StreamReader streamIn = null;

            bool disableCopy = false;

 

            if (this.localPathtoFile.Equals(this.localOutputFullPath))

            {

                disableCopy = true;

            }

            if (this.localOutputFullPath.Length.Equals(0))

            {

                LogResult(" Output file " + this.localOutputFullPath + "is null ");

                LogResult(" Please use a different file for the output file ");

                return false;

            }

 

            try

            {

                Stream streamInTmp = File.OpenRead(this.localPathtoFile);

                streamIn = new StreamReader(streamInTmp);

                streamIn.Close();

            }

            catch

            {

                LogResult("Error Input File for reading" + this.localPathtoFile);

                return false;

            }

            LogResult(" Converting file " + this.localPathtoFile + " to " + this.localOutputFullPath);

            // Copy the file to the output path

            if (!disableCopy)

            {

                try

                {

                    File.Copy(this.localPathtoFile, this.localOutputFullPath, true);

                }

                catch

                {

                    LogResult("Error copying file to " + this.localOutputFullPath);

                    return false;

                }

            }

            return true;

        }

        // Get Command Line Parameters

        public void GetCommandLine(string[] args)

        {

            int i = 0;

            // Get all of the command line arguments

            //            Console.WriteLine(" args " + args[0] + " " + args[1] +

            //                " " + args[2] + " " + args[3] + " " + args[4] + " " + args[5]);

            try

            {

                while (i < 8 && argsIdea != null)

                {

                    if (argsIdea == InputArgInText && args[i + 1] != null)

                    {

                        this.localPathtoFile = args[i + 1];

                        i += 2;

                        continue;

                    }

                    if (argsIdea == InputArgSettingsText && args[i + 1] != null)

                    {

                        this.localSettings = args[i + 1];

                        i += 2;

                        continue;

                    }

                    if (argsIdea == InputArgOutText && args[i + 1] != null)

                    {

                        this.localOutputFullPath = args[i + 1];

                        i += 2;

                        continue;

                    }

                    if (argsIdea == InputArgLogText && args[i + 1] != null)

                    {

                        this.localLogPath = args[i + 1];

                        i += 2;

                        continue;

                    }

                    i += 2;

                }

            }

            catch (IndexOutOfRangeException e)

            {

                // Need to eat the exception since we do not want to end because of insufficient args

                return;

            }

            return;

        }

        void LogResult(string value)

        {

            try

            {

                Stream streamLogTmp = File.Open(this.localLogPath, FileMode.Append);

                this.streamLog = new StreamWriter(streamLogTmp);

                this.streamLog.WriteLine(value);

                this.streamLog.Close();

            }

            catch

            {

                Console.WriteLine(value);

            }

        }

        #endregion

    }

}

 

  1. Add a new class called XmlToXml.cs
    • Right click the project, click “New->Item”
    • Select “Class”
    • For Name, type “XmlToXml.cs”

    • Click “Add”
  1. Modify the class code to the following:


using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Xml;

 

namespace MyConverter

{

    class XmlToXml

    {

        private string filePath = null;

       

        public XmlToXml(string pathName)

        {

            filePath = pathName;

        }

 

        public void AddNode()

        {

            try

            {

                XmlDocument doc = new XmlDocument();

                doc.Load(filePath);

                doc.LastChild.AppendChild(doc.CreateNode(XmlNodeType.Element, "NewNode", "A new node"));

                doc.Save(filePath);

            }

            catch (Exception ex)

            {

                throw ex;

            }

            finally

            {               

            }

        }       

    }

}

 

  1. Review the code you just pasted in, mainly review the “Main” method
  2. Right click the project, select “Properties”
  3. Click the “Debug” tab
  4. For Command line arguments, copy the following:


-in C:\target.xml –out c:\output.xml –log c:\converter.log

  1. Compile the code, fix any errors
  2. Copy the executable (“D:\Lab Work\MyConverter\MyConverter\bin\DebugMyConverter.exe”) to the “C:\program files\Microsoft office servers\12.0\TransformApps” directory.
    • NOTE: this is where your document converters should reside, notice the other converters that reside here:
      1. DocXPageConverter
      2. InfoPathPageConverter
      3. XslApplicatorConverter

Task 3 – Set permission on HtLauncher directory

 

1.      Open the “c:\Program Files\Microsoft office Servers\12.0\Bin” directory

2.      Right click the HtmlTrLauncher directory, select “Properties”

3.      Click “Security”

4.      Add everyone with Full Control

5.      Click “Ok”

 

Task 4 – Create a feature and elements file

  1. Create a new directory called MyConverter in the /template/features directory of the 12 hive
  2. Create a file called feature.xml, paste the following into it:


<?xml version="1.0" encoding="utf-8" ?>

<Feature  Id="[new guid]"

          Title="My Converter"

          Description="Converts xml to my own format"

          Version="1.0.0.0"

          Scope="WebApplication"

          xmlns="http://schemas.microsoft.com/sharepoint/">

    <ElementManifests>

        <ElementManifest Location="MyConverter.xml"/>

    </ElementManifests>

</Feature>

 

  1. Also note that you need to replace the [new guid] with a new GUID
    • Run the D:\Lab Files\09_Lab02\guidgen.exe tool
    • Click “Radio #4”
    • Click “Copy”
    • Paste the GUID into the feature.xml file
    • Remove the curly braces ({})
  2. Create another file called MyConverter.xml, paste the following into it:


<?xml version="1.0" encoding="utf-8" ?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

    <DocumentConverter

        Id="[new guid]"

       Name="Xml to Xml"

       App="MyConverter.exe"

       From="xml"

       To="xml" />

</Elements>

 

  1. Be sure you create another guid id for the converter!
  2. Run the following commands:


stsadm –o installfeature –filename MyConverter\feature.xml

stsadm –o activatefeature –filename MyConverter\feature.xml –url http://servername:100

 

    • NOTE: if you copy and paste and get an error when running the command, replace the dashes!
  1. Perform an IISRESET
  2. Your new document converter is now installed!

Task 5 –Enable Document Converters

  1. Open the Central Administration Site
  2. Click “Application management”
  3. Click “Manage Web Application features”
  4. For the “My Converter” feature, click “Activate”
    • NOTE: Ensure you are in context of web application on port 100
  5. Click “Document Conversions”
  6. Ensure that the web application is set to your team site web app
  7. Check the “Yes” radio button
  8. Select a load balancer server (you likely only have one to pick from)
  9. Notice your new converter is listed (“Customize “Xml to Xml” (xml into xml))
  10. Open in a new tab/windows the link for the converter, note the settings you can apply
  11. In the original central administration window, click Apply

Task 6 – Test the converter

  1. Open your team site (http://servername:100)
  2. Add an xml document to the “Shared Documents” document library called TheirXml.xml with the following text inside the file:

 

<?xml version="1.0" encoding="utf-8" ?>

<WatchThis>

</WatchThis>

 

  1. In the JavaScript dropdown for the item, select “Convert Document->Xml To Xml”

  1. For document name, type “OurXml”

  1. Click “Ok”
  2. A timer job will pick up the conversion request and put the converted document in the Pages document library.  As we mentioned previously before, a document converter will not run on a domain controller properly.
Published Wednesday, December 03, 2008 9:54 PM by cjg
Filed under:

Comments

No Comments
Anonymous comments are disabled

This Blog

Syndication

Powered by Community Server, by Telligent Systems