Generate type safe classes for sharepoint – Part two

This is part two of the T4 template file that will:
1 – Search for SharePoint content types.
2 – Generate type safe Names and ID’s for them.
This time some refinement on the script.

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Xml" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.Generic" #>
<# 
// GET fileNamespace -> http://lennybacon.com/CommentView,guid,6ba5f768-6325-4f09-8341-201122804f52.aspx
var hostServiceProvider = (IServiceProvider)Host;
var dte = (EnvDTE.DTE)hostServiceProvider.GetService(typeof(EnvDTE.DTE));
var activeSolutionProjects = (Array)dte.ActiveSolutionProjects;
var dteProject = (EnvDTE.Project)activeSolutionProjects.GetValue(0);
var defaultNamespace = dteProject.Properties.Item("DefaultNamespace").Value;
var templateDir = Path.GetDirectoryName(Host.TemplateFile);
var fullPath = dteProject.Properties.Item("FullPath").Value.ToString();
fullPath = fullPath.EndsWith("") ? fullPath.Substring(0, fullPath.Length-1) : fullPath;
var subNamespace = templateDir.Replace(fullPath, string.Empty).Replace("", ".");
var fileNamespace = string.Concat(defaultNamespace, subNamespace);

// GET All XML files -> http://weblogs.asp.net/lhunt/pages/CSharp-Coding-Standards-document.aspx
var searchPath = new DirectoryInfo(fullPath).Parent.FullName;
var folderList = new Stack();
var allXmlFiles = new List();
string[] currentFolders = null;
string[] currentFiles = null;
string thisFolder = null;
folderList.Push(searchPath);
while(folderList.Count > 0)
{
    thisFolder = folderList.Pop();
    currentFiles = Directory.GetFiles(thisFolder, "*.xml");
    foreach(string file in currentFiles) if (!file.Contains("Debug")) allXmlFiles.Add(file);      
    currentFolders = Directory.GetDirectories(thisFolder);
    if(currentFolders != null && currentFolders.Length > 0) foreach(string folder in currentFolders) folderList.Push(folder);    
}

// GET All Fields from XML files
var fields = new List<KeyValuePair<string, KeyValuePair<string, KeyValuePair<string, string>>>>();
foreach (string xmlFile in allXmlFiles)
{
 var doc = new XmlDocument();
    doc.Load(xmlFile);
    XmlElement root = doc.DocumentElement;
    foreach (XmlNode node in root.ChildNodes)
        if (node.Name == "ContentType" && node.Attributes != null)
  {
            var attributeName = node.Attributes["Name"];
            var attributeID = node.Attributes["ID"];
            var attributeDisplayName = node.Attributes["Description"];
   fields.Add(new KeyValuePair<string, KeyValuePair<string, KeyValuePair<string, string>>>((attributeName != null) ? attributeName.InnerText.Replace(" ", string.Empty).Replace(
                                            "-", string.Empty) : "",new KeyValuePair<string, KeyValuePair<string, string>>(xmlFile.Replace(searchPath + "",""), new KeyValuePair<string, string>((attributeID != null) ? attributeID.InnerText : "",(attributeDisplayName != null) ? attributeDisplayName.InnerText : ""))));
  }
}
#>// --------------------------------------------------------------------------------------------------------------------
// 
//   Copyright 2012 by Imtech ICT Integrated Solutions. All rights reserved. This material may not be duplicated for any profit-driven enterprise.
// 
// 
//   Static ContentType Names And Ids
// 
// --------------------------------------------------------------------------------------------------------------------

namespace <#= fileNamespace #>
{
    using Microsoft.SharePoint;

    /// 
    /// Static ContentTypeNames
    /// 
    public static partial class ContentTypeNames
    {
<#
string previousFile = "";
bool firstTimeRun = true;
foreach (var field in fields.OrderBy(t => t.Value.Key).ThenBy(t => t.Key))
{
 if (previousFile != field.Value.Key)
 { if (!firstTimeRun)
  { #>

<# }
  if (firstTimeRun){firstTimeRun = false;} #>
        // <#= field.Value.Key #>
<#   previousFile = field.Value.Key;
 } #>
        public static readonly string <#= field.Key #> = "<#= field.Key #>";
<#}#>
    }

    /// 
    /// Static ContentTypeIds
    /// 
    public static partial class ContentTypeIds
    {
<#
previousFile = "";
firstTimeRun = true;
foreach (var field in fields.OrderBy(t => t.Value.Key).ThenBy(t => t.Key))
{
 if (previousFile != field.Value.Key)
 { if (!firstTimeRun)
  { #>

<# }
  if (firstTimeRun){firstTimeRun = false;} #>
        // <#= field.Value.Key #>
<#   previousFile = field.Value.Key;
 } #>
        public static readonly SPContentTypeId <#= field.Key #> = new SPContentTypeId("<#= field.Value.Value.Key #>");
<#  } #>
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s