Select Page

This post is a quick brain dump of some of the challenges I’ve been working through updating CKSDev to work with Visual Studio 2012.

Item Templates

 

You can extend the SharePoint Visual Studio tooling in many different ways. One of which is by creating new Item Templates. Item templates are the code templates which appear in the ‘add new’ dialog in the solution project. Microsoft wrote several good walkthroughs http://msdn.microsoft.com/en-us/library/vstudio/ee256697(v=vs.110).aspx as an example.

In Visual Studio 2010 the SharePoint items were grouped in a basic ‘SharePoint’ category and deploying them via a VSIX package was actually dead simple. Edit the CSProj and replace this VSTemplate element with the following XML, and then save and close the file.

<VSTemplate Include="ItemTemplate.vstemplate">
<OutputSubPath>SharePoint\SharePoint14</OutputSubPath>
</VSTemplate>

The OutputSubPath element specifies additional folders in the path under which the item template is created when you build the project. The folders specified here ensure that the item template will be available only when customers open the Add New Item dialog box, expand the SharePoint node, and then choose the 2010 node.

So that worked fine in Visual Studio 2010 for SharePoint 2010. CKSDev for VS2010 had some smarts under the covers for packaging, but also followed this basic process.

So why is this an issue for Visual Studio 2012????

Well as you can see Microsoft moved the items into a new category called ‘Office/SharePoint’

image

In true Lt Gorman style (Aliens reference when the troops realise they can’t fire the pulse rifles under the reactor)

SO??, So what?

Rather then setting off a thermo-nuclear explosion this just causes a real headache for custom template VSIX packaging.

Each VSTemplate item in your solution has a property called ‘Category’, this is where you can tell VS to package that template under a specific category. Great so all you have to do is match the path in the new item dialog. Yes in theory.

The challenge comes that the VS/MSBuild bits behind the scenes understand a forward slash as a new folder. So you end up with Office with a sub folder of SharePoint. Not what you wanted. Sad smile

Ok so what next?

Well another thing to try is to match the OOTB folder path for the native MS templates. They live under “C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Office SharePoint\1033”

So setting the ‘Category’ to ‘Office SharePoint’ should work right? Wrong more Crying face ensue as this time the space is encoded to %20 and the templates don’t show up at all.

This process of elimination had by this point taken well over two hours and was getting somewhat frustrating.

So as i mentioned earlier CKSDev already has a subtle difference to its packaging. This is due to it being distributed via the Visual Studio Gallery. The VS gallery has some nasty folder length checks (less that a combined 256) which mean the CKSDev templates are compacted folder and name wise to be as short as possible. The VS Gallery rules don’t understand the fact the tools couldn’t install on XP and check for the length anyway and reject if over 256.

Another example of this technique in action is here at Aaron Marten’s blog article which explains how this works.

The CKSDev packaging uses some MSBuild stuff to create an ‘I’ for item templates and ‘P’ for project templates during build. The VSIX then picks them up from there.

The assets section of the VSIX looks like this:

<Assets>
    <Asset Type="Microsoft.VisualStudio.MefComponent" Path="|CKS.Dev11;AssemblyProjectOutputGroup|" />
    <Asset Type="SharePoint.Commands.v4" Path="|CKS.Dev11;SP2010CommandProjectOutputGroup|" />
    <Asset Type="SharePoint.Commands.v5" Path="|CKS.Dev11;SP2013CommandProjectOutputGroup|" />
    <Asset Type="Microsoft.VisualStudio.VsPackage" Path="|CKS.Dev11;PkgdefProjectOutputGroup|" />
    <Asset Type="Microsoft.VisualStudio.Assembly" Path="|CKS.Dev11;AssemblyProjectOutputGroup|" />
    <Asset Type="Microsoft.VisualStudio.ProjectTemplate"
           d:Source="Project"
           d:ProjectName="CKS.Dev11"
           d:TargetPath="p"
           Path="P"
           d:VsixSubPath="P" />
    <Asset Type="Microsoft.VisualStudio.ItemTemplate"
           d:Source="Project"
           d:ProjectName="CKS.Dev11"
           d:TargetPath="I"
           Path="I"
           d:VsixSubPath="I" />
  </Assets>

So the solution was to modify the MSBuild elements for the CSProj file

<Target Name="GetVsixTemplateItems">
   <ItemGroup>
     <VSIXSourceItem Include="@(IntermediateZipItem)">
       <VSIXSubPath>I\%(IntermediateZipItem.Language)\$(ItemTemplateFolderPath)\%(IntermediateZipItem.Culture)</VSIXSubPath>
     </VSIXSourceItem>
   </ItemGroup>
   <ItemGroup>
     <VSIXSourceItem Include="@(IntermediateZipProject)">
       <VSIXSubPath>P\%(IntermediateZipProject.Language)\%(IntermediateZipProject.OutputSubPath)\%(IntermediateZipProject.Culture)</VSIXSubPath>
     </VSIXSourceItem>
   </ItemGroup>
</Target>

 

The item templates are using a variable declared with the category (aka the folder path) desired, whereas the projects are declared using the ‘Category’ in the properties window of the VSTemplate item. (this is the output sub path for those interested).

The path has to be declared in another element as it contains a space.

<PropertyGroup>
    <ItemTemplateFolderPath>Office SharePoint\CKSDev</ItemTemplateFolderPath>
  </PropertyGroup>

All that together gives the desired effect of all the CKSDev items appearing inside the right OOTB category.

image

image

Ok so now everything is sitting in the desired place. As with a lot of CKSDev coding its the little things like this which take so long. Understanding Visual Studio, MSBuild and SP API’s all in a days work Smile with tongue out

Project templates

 

So with the information above the project template settings are actually a lot easier.

Simply set your category to ‘Office\SharePoint Solutions’ As you actually want it to appear in a sub folder this time.

image

I hope this saves someone time and headaches Open-mouthed smile