Generate SharePoint project constants with T4 by feature definitions
Опубликовано: Июль 25, 2010 Filed under: sharepoint 1 Comment »Problem
Usually the main problem of SharePoint development is accessing SPListItem fields values by theirs internal names of field ids. For example:
SPListItem listItem = GetLIstItem(); // I prefer to use field ids for speed and garanteed way extract values listItem[SPBuiltInFieldId.Title]
The output file will be like that:
But in some scenarios fieldId doesn’t help me for get values, such as get it in item event receivers. In this case I need to know the field internal name.
public override void ItemAdding(SPItemEventProperties properties)
{
string toolTipFieldInternalName = "";
//oops, this place have some perfomance overhead
using (SPWeb web = properties.OpenWeb())
{
toolTipFieldInternalName = web.Lists[properties.ListId].Fields[toolTipFieldId].InternalName;
}
string urlVal = properties.AfterProperties["URL"].ToString();
SPFieldUrlValue val = new SPFieldUrlValue(urlVal);
string desc = val.Description;
properties.AfterProperties[toolTipFieldInternalName] = desc;
}
For best experience you may install Visual T4 Editor
There is a simple way to extract and generate usefull constants for field ids and internal names from features definition files in your project with T4 mechanism built-in into visual studio:
Constants.tt (this code works fine only in Visual Studio 2010)
<#@ template inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation" language="C#" debug="true" hostSpecific="true" #>
<#@ assembly name="Microsoft.VisualStudio.TextTemplating.Modeling.10.0.dll"#>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ Assembly Name="System.Xml.dll" #>
<#@ Assembly Name="System.Xml.Linq.dll" #>
<#@ Assembly Name="System.Windows.Forms.dll" #>
<#@ Assembly Name="EnvDTE" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#
var currentDirectory = Path.GetDirectoryName(Host.TemplateFile);
var xmlFiles = Directory.GetFiles(currentDirectory, "*.xml", SearchOption.AllDirectories);
var fieldQualifier = XName.Get("Field", "http://schemas.microsoft.com/sharepoint/");
var listdQualifier = XName.Get("ListInstance", "http://schemas.microsoft.com/sharepoint/");
#>using System;
namespace <#= System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint") #>
{
public static partial class Constants
{
public static class Fields
{
<#
var fields = xmlFiles
.Select(XDocument.Load)
.Select(d => d.Descendants(fieldQualifier))
.SelectMany(x => x)
.Where(i => i != null)
.Select(i => new KeyValuePair<string, string>(
i.Attribute("ID") != null ? i.Attribute("ID").Value : null,
i.Attribute("Name") != null ? i.Attribute("Name").Value : null))
.Where(i => !string.IsNullOrEmpty(i.Key) && !string.IsNullOrEmpty(i.Value))
.Distinct();
foreach (var field in fields)
{
#>
public static string <#= field.Value #>_InternalName = "<#= field.Value #>";
public static Guid <#= field.Value #>_Id = new Guid("<#= field.Key #>");
<#
}
#>
}
public static class Lists
{
}
}
}
The output file will be like follow:
using System;
namespace MyProjectNameSpace.Project1
{
public static partial class Constants
{
public static class Fields
{
public static string DocKind_InternalName = "DocKind";
public static Guid DocKind_Id = new Guid("{b4fc3e5e-7573-4e39-9417-81d85f25ed73}");
public static string RegDate_InternalName = "RegDate";
public static Guid RegDate_Id = new Guid("{e453a0ee-7797-4d90-941c-0ea9a775f48d}");
public static string RegUser_InternalName = "RegUser";
public static Guid RegUser_Id = new Guid("{616997f5-6cf2-4795-b625-d7b7f4681a2e}");
}
public static class Lists
{
}
}
}
Wahey, finally blogging in English?