1 - Search for SharePoint content types.
2 - Generate type safe Names and ID's for them.
<#@ 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<string>(); var allXmlFiles = new List<string>(); 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 file="ContentTypes.cs" company="Imtech ICT Integrated Solutions"> // Copyright 2012 by Imtech ICT Integrated Solutions. All rights reserved. This material may not be duplicated for any profit-driven enterprise. // </copyright> // <summary> // Static ContentType Names And Ids // </summary> // -------------------------------------------------------------------------------------------------------------------- namespace <#= fileNamespace #> { using Microsoft.SharePoint; /// <summary> /// Static ContentTypeNames /// </summary> 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 #>"; <#}#> } /// <summary> /// Static ContentTypeIds /// </summary> 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 #>"); <# } #> } }
No comments:
Post a Comment