Last active
August 29, 2015 14:04
-
-
Save zzeneg/d357c35ecd5e73dd6911 to your computer and use it in GitHub Desktop.
GenTSDefFromSP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare module Portal { | |
export interface IListItem { | |
ID: number; | |
Title: string; | |
Author: SP.FieldUserValue; | |
Created: Date; | |
} | |
export interface IBook extends IListItem { | |
Year: number; | |
Genre: SP.FieldLookupValue; | |
PageCount: number; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template debug="false" hostspecific="true" language="C#" #> | |
<#@ assembly name="System.Core" #> | |
<#@ assembly name="System.Xml" #> | |
<#@ assembly name="System.Xml.Linq" #> | |
<#@ import namespace="System.IO" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ import namespace="System.Xml.Linq" #> | |
<#@ import namespace="System.Text" #> | |
<#@ import namespace="System.Collections.Generic" #> | |
<#@ output extension=".d.ts" #> | |
declare module Portal { | |
export interface IListItem { | |
ID: number; | |
Title: string; | |
Author: SP.FieldUserValue; | |
Created: Date; | |
} | |
<# | |
var path = Host.ResolvePath(@"..\Portal.Lists\"); | |
var contentTypes = GetContentTypes(path); | |
foreach (var contentType in contentTypes) { | |
#> | |
export interface I<#= contentType.Name #> extends IListItem { | |
<# foreach (var field in contentType.Fields) { #> | |
<#= field.Name #>: <#= field.FieldType #>; | |
<# }#> | |
} | |
<# }#> | |
} | |
<#+ | |
private Dictionary<string, List<string>> _contentTypes; | |
private Dictionary<string, string> _types; | |
private class Field | |
{ | |
internal Field(string name, string type) | |
{ | |
Name = name; | |
switch (type.ToLower()) | |
{ | |
case "text": | |
case "note": | |
case "html": | |
FieldType = "string"; | |
break; | |
case "datetime": | |
FieldType = "Date"; | |
break; | |
case "number": | |
FieldType = "number"; | |
break; | |
case "boolean": | |
FieldType = "boolean"; | |
break; | |
case "user": | |
FieldType = "SP.FieldUserValue"; | |
break; | |
case "usermulti": | |
FieldType = "SP.FieldUserValue[]"; | |
break; | |
case "lookup": | |
FieldType = "SP.FieldLookupValue"; | |
break; | |
case "lookupmulti": | |
FieldType = "SP.FieldLookupValue[]"; | |
break; | |
case "url": | |
FieldType = "SP.FieldUrlValue"; | |
break; | |
default: | |
FieldType = "any"; | |
break; | |
} | |
} | |
internal string Name { get; private set; } | |
internal string FieldType { get; private set; } | |
} | |
private class ContentType | |
{ | |
internal ContentType(string name) | |
{ | |
Name = name; | |
Fields = new List<Field>(); | |
} | |
internal List<Field> Fields { get; private set; } | |
internal string Name { get; private set; } | |
} | |
private List<ContentType> GetContentTypes(string path) | |
{ | |
LoadSpDataFiles(path); | |
var contentTypes = new List<ContentType>(); | |
foreach (var ct in _contentTypes) | |
{ | |
var contentType = new ContentType(ct.Key); | |
foreach (var f in ct.Value) | |
{ | |
string type; | |
var field = new Field(f, _types.TryGetValue(f, out type) ? type : string.Empty); | |
contentType.Fields.Add(field); | |
} | |
contentTypes.Add(contentType); | |
} | |
return contentTypes; | |
} | |
private void LoadSpDataFiles(string path) | |
{ | |
_contentTypes = new Dictionary<string, List<string>>(); | |
_types = new Dictionary<string, string>(); | |
var dir = new DirectoryInfo(path); | |
var files = dir.GetFiles("*.spdata", SearchOption.AllDirectories); | |
foreach (var fileInfo in files) | |
{ | |
var doc = XDocument.Load(fileInfo.FullName); | |
if (doc.Root != null) | |
{ | |
var elementsFileName = | |
doc.Root.Descendants(doc.Root.GetDefaultNamespace() + "ProjectItemFile") | |
.First() | |
.Attribute("Source") | |
.Value; | |
var fileName = fileInfo.Directory.GetFiles(elementsFileName).First().FullName; | |
if (doc.Root.Attribute("Type").Value == "Microsoft.VisualStudio.SharePoint.ContentType") | |
{ | |
LoadContentTypesFromElementsFile(fileName); | |
} | |
else if (doc.Root.Attribute("Type").Value == "Microsoft.VisualStudio.SharePoint.Field") | |
{ | |
LoadTypeFromElementsFile(fileName); | |
} | |
} | |
} | |
} | |
private void LoadTypeFromElementsFile(string fileName) | |
{ | |
var doc = XDocument.Load(fileName); | |
if (doc.Root != null) | |
{ | |
var xmlns = doc.Root.GetDefaultNamespace(); | |
var field = doc.Root.Element(xmlns + "Field"); | |
if (field != null) | |
{ | |
var name = field.Attribute("Name").Value; | |
var type = field.Attribute("Type").Value; | |
_types.Add(name, type); | |
} | |
} | |
} | |
private void LoadContentTypesFromElementsFile(string fileName) | |
{ | |
var doc = XDocument.Load(fileName); | |
if (doc.Root != null) | |
{ | |
var xmlns = doc.Root.GetDefaultNamespace(); | |
var contentType = doc.Root.Element(xmlns + "ContentType"); | |
if (contentType != null) | |
{ | |
var name = contentType.Attribute("Name").Value; | |
var fieldNames = | |
contentType.Descendants(xmlns + "FieldRef").Select(a => a.Attribute("Name").Value).ToList(); | |
_contentTypes.Add(name, fieldNames); | |
} | |
} | |
} | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment