Thursday, July 30, 2015

Using T4 Text Templates In SharePoint To Get Lists & Columns Name

T4 text template is mixture of text block and control logic that can generate the file in Visual Studio. First time I noticed text templates when I was working with Entity Framework. T4 text template is one of the best hidden feature of Visual Studio.  It can help you to generate the file on the fly and automate the task. You can even generate the .net classes. You can get more details about T4 text templates here.

As a developer you need SharePoint column internal names while writing code for various operations. You can get the internal name for a column by browsing to the List Settings > Edit Columns. Column internal name is encoded as querystring "Field" in URL. For example:

/_layouts/15/FldEdit.aspx?List=%7B004B80BD-040C-4B68-8C86-F140F2C4F373%7D&Field=StartDate

I wanted to generated C# utility class using T4 text template which contains Web, Lists and columns related information. Following is the template I use to get Web, Lists and Columns related information while SharePoint development:

<#@ template language="C#" #>
<#@ output extension="cs" #>
<#@ Assembly Name="System.Core" #>
<#@ Assembly Name="Microsoft.SharePoint.Client" #>
<#@ Assembly Name="Microsoft.SharePoint.Client.Runtime" #>
<#@ Import Namespace="Microsoft.SharePoint.Client" #>
<#@ Import Namespace="System.Text" #>
<#@ Import Namespace="System.Text.RegularExpressions" #>
//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using Microsoft.SharePoint.Client;
using System.Text;

namespace Custom.SharePoint
{
    /// <summary>
    /// Custom class like SharePoint SPBuiltinFieldId with you own columns
    /// </summary>
    public static partial class Constants
    {
<#
    string siteUrl = "http://Sharepoint:1111";

    ClientContext clientContext = new ClientContext(siteUrl);
    Web oWebsite = clientContext.Web;
    ListCollection collList = oWebsite.Lists;
   
    clientContext.Load(oWebsite);
    clientContext.Load(collList);
    clientContext.ExecuteQuery();
#>
        /// <summary>
     /// <para>Title : <#= oWebsite.Title #></para>
     /// </summary>
     public static string WebTitle = "<#= oWebsite.Title #>";

     /// <summary>
     /// <para>Id : <#= oWebsite.Id.ToString() #></para>
     /// </summary>
     public static Guid WebId = new Guid("<#= oWebsite.Id.ToString() #>");

     /// <summary>
     /// <para>Url : "<#= oWebsite.Url #>" </para>
     /// </summary>
     public static string WebUrl = "<#= oWebsite.Url #>";
<#

    foreach (List oList in collList)
    {
        if(!oList.Hidden)
        {
            var listName = Regex.Replace(oList.Title, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled);
            clientContext.Load(oList.RootFolder);
            clientContext.ExecuteQuery();
#>
  /// <summary>
  /// <para>ID : <#= oList.Id.ToString() #></para>
  /// <para>Type : <#= oList.BaseType #> </para>
  /// <para>Title : <#= oList.Title #></para>
        /// <para>Url : "<#= oWebsite.Url #><#= oList.RootFolder.ServerRelativeUrl #>" </para>
  /// </summary>
  public class <#= listName #>
  {
   /// <summary>
   /// <para>Title : <#= oList.Title #></para>
   /// </summary>
   public static string Title = "<#= oList.Title #>";

   /// <summary>
   /// <para>Id : <#= oList.Id.ToString() #></para>
   /// </summary>
   public static Guid Id = new Guid("<#= oList.Id.ToString() #>");

   /// <summary>
   /// <para>ServerRelativeUrl : "<#= oList.RootFolder.ServerRelativeUrl #>" </para>
   /// </summary>
   public static string ServerRelativeUrl = "<#= oList.RootFolder.ServerRelativeUrl #>";

   /// <summary>
   /// <para>Url : "<#= oWebsite.Url #><#= oList.RootFolder.ServerRelativeUrl #>" </para>
   /// </summary>
   public static string Url = "<#= oWebsite.Url #><#= oList.RootFolder.ServerRelativeUrl #>";

<# 
            FieldCollection fields = oList.Fields;
            clientContext.Load(fields);
            clientContext.ExecuteQuery();
#>
   public class Fields
   {
<#
            foreach (Field field in fields)
            {
                if (!field.FromBaseType)
                {
#>
    /// <summary>
    /// <para>Title : <#= field.Title #></para>
    /// <para>InternalName : <#= field.InternalName #></para>
    /// <para>Id : <#= field.Id.ToString() #></para>
                /// <para>Type : <#= field.TypeAsString #></para>
    /// </summary>
    public static Guid _<#= field.InternalName #> = new Guid("<#= field.Id.ToString() #>");

<#
                }
            }
#>
      }
     }
<#
        }
    }
#>
    }
}


Above template generates the following class:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using Microsoft.SharePoint.Client;
using System.Text;

namespace Custom.SharePoint
{
    /// <summary>
    /// Custom class like SharePoint SPBuiltinFieldId with you own columns
    /// </summary>
    public static partial class Constants
    {
        /// <summary>
        /// <para>Title : HemantTest</para>
        /// </summary>
        public static string WebTitle = "Web Title";

        /// <summary>
        /// <para>Id : e8ee6e88-45d2-41f7-b234-f68602fc2f31</para>
        /// </summary>
        public static Guid WebId = new Guid("e8ee6e88-45d2-41f7-b234-f68602fc2f31");

        /// <summary>
        /// <para>Url : "http://Sharepoint:1111" </para>
        /// </summary>

        public static string WebUrl = "http://Sharepoint:1111";
....
/// <summary> /// <para>ID : 0aa3da1b-b1a4-42b6-9136-008f0d462b80</para> /// <para>Type : GenericList </para> /// <para>Title : TestList</para> /// <para>Url : "http://Sharepoint:1111/Lists/TestList" </para> /// </summary> public class TestList { /// <summary> /// <para>Title : TestList</para> /// </summary> public static string Title = "TestList"; /// <summary> /// <para>Id : 0aa3da1b-b1a4-42b6-9136-008f0d462b80</para> /// </summary> public static Guid Id = new Guid("0aa3da1b-b1a4-42b6-9136-008f0d462b80"); /// <summary> /// <para>ServerRelativeUrl : "/Lists/TestList" </para> /// </summary> public static string ServerRelativeUrl = "/Lists/TestList"; /// <summary> /// <para>Url : "http://Sharepoint:1111/Lists/TestList" </para> /// </summary> public static string Url = "http://Sharepoint:1111/Lists/TestList"; public class Fields { /// <summary> /// <para>Title : Field_SingleLineText</para> /// <para>InternalName : Field_SingleLineText</para> /// <para>Id : 386a0aff-9acb-4e86-a619-b56fdfc8124d</para> /// <para>Type : Text</para> /// </summary> public static Guid _Field_SingleLineText = new Guid("386a0aff-9acb-4e86-a619-b56fdfc8124d"); /// <summary> /// <para>Title : Field_MultiLineText</para> /// <para>InternalName : Field_MultiLineText</para> /// <para>Id : 497e52a5-e87b-46c3-857e-b15effc46659</para> /// <para>Type : Note</para> /// </summary> public static Guid _Field_MultiLineText = new Guid("497e52a5-e87b-46c3-857e-b15effc46659"); .... /// <summary> /// <para>Title : Taxonomy Catch All Column</para> /// <para>InternalName : TaxCatchAll</para> /// <para>Id : f3b0adf9-c1a2-4b02-920d-943fba4b3611</para> /// <para>Type : LookupMulti</para> /// </summary> public static Guid _TaxCatchAll = new Guid("f3b0adf9-c1a2-4b02-920d-943fba4b3611"); } } } }

Now you can using intellisense to get List, Column Id .







Simply add "SharePointObjects.tt" file to your project in Visual Studio. Update the Site url and save. It will generate the C# class.

No comments:

Post a Comment