Thursday 15 March 2012

Invalid Template URL exception with Office Web Apps.

I installed Office Web Applications on a SharePoint Farm yesterday. I wanted to test the applications, so I created a new SharePoint Document Library. I followed the steps on this article to add Word Document, PowerPoint Presentation, Excel Workbook and OneNote Notebook templates in my library. Because I am lazy, instead of creating my own templates, I uploaded the templates that exist in SharePoint installation folder (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\1033\STS\DOCTEMP) when creating templates.

Then I started testing. Excel and OneNote content types were working fine. However when I tried to create a new "Word Document" or "PowerPoint Presentation", I was getting the following error:

Invalid template URL http://mysharepoint/Test/Forms/Word/WDTMPL.DOTX The template must exist in the Forms directory of this document library. Create the template in the Forms directory, and then re-type the Web address. Note that you cannot move or copy a template into the Forms directory.

Upon looking in SharePoint logs I found the full exception details as follows:

03/15/2012 13:30:18.43 w3wp.exe (0x1D18)   0x21F8 SharePoint Foundation Runtime   tkau Unexpected Microsoft.SharePoint.SPException: Invalid template URL http://mysharepoint/Test/Forms/Word/WDTMPL.DOCX The template must exist in the Forms directory of this document library. Create the template in the Forms directory, and then re-type the Web address. Note that you cannot move or copy a template into the Forms directory.    at Microsoft.Office.Web.CreateNewDocument.Pages.CreateNewDocument.CreateDocumentFromListTemplate(String fileName)     at Microsoft.Office.Web.CreateNewDocument.Pages.CreateNewDocument.ButtonOkClick(Object sender, EventArgs e)     at System.Web.UI.WebControls.Button.OnClick(EventArgs e)     at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)     at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eve... 7f322d53-de57-4a59-85f3-3f49ac2382bc


The error was a bit strange since it was saying that a file in the Forms folder is not in the Forms folder. I tried to change the path using SharePoint 2010 designer to make it directly under the Forms folder instead of a Sub Folder, and I was getting the same error.

So I decided to use .NET Reflector to reverse engineer the method in the stack trace
Microsoft.Office.Web.CreateNewDocument.Pages.CreateNewDocument.CreateDocumentFromListTemplate
to see what's going wrong. And here is the part that was causing the exception:

switch (Path.GetExtension(this._templateUrl))
   {
       case ".dotx":
       case ".docx":
       case ".dotm":
       case ".docm":
       case ".odt":
           return this.CreateWordDocument(fileName, currentFolder, this._templateUrl);
       case ".pptx":
       case ".odp":
           return this.CreatePowerpointPresentation(fileName, currentFolder, this._templateUrl);
       case ".one":
       case ".onepkg":
           return CreateOnenoteNotebook(fileName, currentFolder, this._templateUrl);
       case ".ods":
           return this.CreateExcelDocument(fileName, currentFolder, this._templateUrl);
   }
   throw new SPException(SPResource.GetString("InvalidDoclibTemplateUrl", new object[] { this._templateUrl }));


There are two bugs with this code which caused me to have trouble fixing the issue:

1- The first bug is that checking the extension uses case sensitive comparison. So simple uploading the file as WDTMPL.dotx instead of WDTMPL.DOTX fixed the problem for me.

2- The second bug is the exception error message itself. Instead of telling me to "Create the template in the Forms directory", it should tell me that my template is using an unsupported extension because if you look at the code, this is actually what happens. The exception only gets thrown when the template file is not having one of extensions in the switch statement.


No comments:

Post a Comment