Transforming native-ribbon XML (SharePoint/Project Server) into c#class

Feb 11, 2013 at 8:37 AM
Hello all,

I reckon that a specific tool is used to transform native-ribbon XML into a c# class (i.e. StandardButtons.cs), but I don't know which one. Maybe someone can enlighten me about this ?

My goal is simply to generate a C# class (namely ProjectServerButtons.cs) that would be Project Server specific. And since I'm working with SP2013/PS2013, I would be able to generate C# classes for each version (SP2010, PS2010, SP2013 and PS2013).

Cheers,
looking forwards to hearing from you very soon!
Coordinator
Feb 11, 2013 at 8:47 AM
Simple T4 transformation can be used for that purpose. I.e. you can examine StandardButtons.tt included into Fluent Ribbon API.
Feb 11, 2013 at 9:00 AM
Hi omlin,

Thanks a lot, I'm not familiar with this at all, but I'll look into it. It seems fairly "understandable", so I think I won't struggle too much on this.
I'll post the template for SP 2013 & PS 2013 when I'm done.
Feb 11, 2013 at 3:44 PM
Re,

The ribbon definition in Project Server is slightly different, so I couldn't use the StandardButtons.tt template file. I had to create a new one, specifically for Project Server.

The following code only creates the buttons of the Project Center group (hardcoded), but at least, it's a start:
<#@ template debug="true" hostSpecific="true"  language="C#" #>
<#@ output extension=".cs" encoding="UTF-8" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ Assembly Name="System.Xml.dll" #>
<#@ Assembly Name="System.Xml.Linq.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Xml.Linq" #>

using System;
using Microsoft.SharePoint.Utilities;
using FluentRibbon.Definitions;
using FluentRibbon.Definitions.Controls;

namespace FluentRibbonExtension.Libraries
{
    /// <summary>
    /// This library contains control definitions
    /// </summary>
    public class ControlLibraryExtension : FluentRibbon.Libraries.ControlLibrary
    {
        /// <summary>
        /// OOTB SharePoint ribbon buttons.
        /// </summary>
        public class ProjectServerButtons
        {
            public class ProjectCenter {
<#
        
List<XElement> tabElements = new List<XElement>();

Func<XAttribute, string> GetValueFromAttribute = a => a == null ? String.Empty : a.Value;

XDocument doc = XDocument.Load(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\FEATURES\PWARibbon\listtemplates\pwaribbon.xml");
XDocument resx = XDocument.Load(@"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\Resources\pwafeatures.resx");
XNamespace spns = XNamespace.Get("http://schemas.microsoft.com/sharepoint/");

var root = doc.Element(spns + "Elements"); // single node

// récupération d'un noeud CustomAction spécifique.
XElement projectCenterNode = (from x in root.Elements(spns + "CustomAction")
                                where x.Attribute("Id").Value == "Ribbon.ContextualTabs.ProjectCenter"
                                select x).FirstOrDefault();

// ici, on est sur un noeud custom action.
var commandUIDefinitions = projectCenterNode.Element(spns + "CommandUIExtension").Element(spns + "CommandUIDefinitions");
XElement projectCenter_ContextualTabs = (from x2 in commandUIDefinitions.Elements(spns + "CommandUIDefinition")
                                            where x2.Attribute("Location").Value == "Ribbon.ContextualTabs._children"
                                            select x2).FirstOrDefault();
var groups = projectCenter_ContextualTabs.Element(spns + "ContextualGroup").Element(spns + "Tab").Element(spns + "Groups");

var groupsElements = new List<XElement>();
groupsElements.AddRange(groups.Elements());
foreach (XElement groupElement in groupsElements)
{
    var groupValue = groupElement.Attribute("Id").Value;
    var groupName = groupValue.Substring(groupValue.LastIndexOf(".") + 1);
    var ctrl = groupElement.Element(spns + "Controls");
    foreach (XElement controlElement in groupElement.Element(spns + "Controls").Elements())
    {
        if (controlElement.Name.LocalName != "Button") continue;
        
        var controlValue = GetValueFromAttribute(controlElement.Attribute("Id"));
        var controlName = controlValue.Substring(controlValue.LastIndexOf(".") + 1);
        if (controlValue.StartsWith(groupValue + "."))
            controlName = controlValue.Substring(groupValue.Length + 1).Replace(".", "_");

        var controlText = GetValueFromAttribute(controlElement.Attribute("LabelText"));

        string controlTitle = controlName;
        if (controlText.StartsWith("$Resources:core,"))
        {
            var resourceName = controlText.Substring("$Resources:core,".Length).Trim().TrimEnd(';').ToLower();
            var element = resx
                .Element("root")
                .Elements("data")
                .FirstOrDefault(e => e.Attribute("name").Value.ToLower() == resourceName);

            if (element != null)
                controlTitle = element.Element("value").Value;
        }

        var controlTooltipTitle = GetValueFromAttribute(controlElement.Attribute("ToolTipTitle"));
        var controlTooltipDescription = GetValueFromAttribute(controlElement.Attribute("ToolTipDescription"));

        var controlImage16 = GetValueFromAttribute(controlElement.Attribute("Image16by16"));
        var controlImage32 = GetValueFromAttribute(controlElement.Attribute("Image32by32"));

        var imageX = GetValueFromAttribute(controlElement.Attribute("Image32by32Left"));
        var imageY = GetValueFromAttribute(controlElement.Attribute("Image32by32Top"));
        string xyImageText = string.Empty;
        if (!String.IsNullOrEmpty(imageX))
            xyImageText = ",\r\n\t\t\t\t\t\t\tX = " + -Convert.ToInt32(imageX) / 32 + ",\r\n\t\t\t\t\t\t\tY = " + -Convert.ToInt32(imageY) / 32;

#>
                /// <summary>
                /// Button, which will appear identical to the standard '<#=controlTitle#>' button, but with custom javascript attached to it.
                /// </summary>
                /// <param name="javascript">Javascript code, which will be executed, when the button is clicked.</param>
                public static ButtonDefinition <#=groupName#>_<#=controlName#>(string javascript)
                {
                    return <#=groupName#>_<#=controlName#>(javascript, 1033);
                }
                /// <summary>
                /// Button, which will look exactly like the standard '<#=controlTitle#>' button, but with custom javascript attached to it
                /// </summary>
                /// <param name="javascript">Javascript code, which will be executed, when the button is clicked.</param>
                /// <param name="lcid">Locale id for the current site. If not specified, english locale (1033) will be used.</param>
                public static ButtonDefinition <#=groupName#>_<#=controlName#>(string javascript, int lcid)
                {
                    string id = GetRandom().Next(100, 999).ToString();
            
                    var definition = new ButtonDefinition()
                    {
                        Id = "Button" + id,
                        Title = SPUtility.GetLocalizedString("<#=controlText#>", "core", (uint)lcid),
                        Image = new ImageDefinition()
                        {
                            Url16 = "<#=controlImage16#>",
                            Url32 = "<#=controlImage32#>"<#=xyImageText#>
                        },
                        ToolTip = new ToolTipDefinition()
                        {
                            Title = SPUtility.GetLocalizedString("<#=controlTooltipTitle#>", "core", (uint)lcid),
                            Description = SPUtility.GetLocalizedString("<#=controlTooltipDescription#>", "core", (uint)lcid)
                        },
                        CommandJavaScript = javascript
                    };

                    if (definition.Image.Url16.ToLowerInvariant().Contains("$resources"))
                        definition.Image.Url16 = SPUtility.GetLocalizedString(definition.Image.Url16, "core", (uint)lcid);
                    if (definition.Image.Url32.ToLowerInvariant().Contains("$resources"))
                        definition.Image.Url32 = SPUtility.GetLocalizedString(definition.Image.Url32, "core", (uint)lcid);

                    return definition;
                }
<#

    }
}

#>
            }
        }
    }
}
Hope it helps !