Goal
To use a server side function from a XSL used in a web part.Solution
It is simple to use a server side function. Just need to make sure the dll should be deployed in GAC. In this example I created a class with name "ClassName", the project name is "SampleApplication". Deployed the dll in GAC. Modify the XSL head and add xmlns:helper="SampleApplication.ClassName" on the head. "helper" is like an tag which will point to the class.XSL
<xsl:stylesheet version="1.0" exclude-result-prefixes="x d xsl msxsl cmswrt helper" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:cmswrt="http://schemas.microsoft.com/WebParts/v3/Publishing/runtime" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:helper="SampleApplication.ClassName">
SampleApplication.ClassName
#region System using System; using System.Collections.Generic; using System.Linq; using System.Text; #endregion namespace SampleApplication { public class ClassName { #region Properties /// <summary> /// Name /// </summary> private static string Name = "Maulik Dhorajia"; /// <summary> /// Today Date /// </summary> private static string TodayDate = System.DateTime.Now.ToString(); #endregion /// <summary> /// Gets the property /// </summary> /// <param name="propertyKey"></param> /// <returns></returns> public static string GetProperty(string propertyKey) { if (propertyKey == "Name") { return Name; } else if (propertyKey == "TodayDate") { return TodayDate; } else { return ""; } } } }
Calling the function from XSL
<xsl:value-of select="helper:GetLabel('Name')"/> <xsl:value-of select="helper:GetLabel('TodayDate')"/>
This should be all to show the stuff on the page. Let me know if this helped.
Your tutorials to SharePoint are just amazing. I am liking it and getting to learn lot from it as a beginner.
ReplyDeleteRusty Nelson,
DeleteThank you very much for appreciation, this was the main reason I am writing blogs. I only try and add those topics which I faced a lot of issues to figure out. My thinking is "If I am having trouble resolving these stuff, I am sure there are more who are there trying."
Thanks,
Maulik Dhorajia
Hi Maulik
ReplyDeleteThe above approach works when the deployment is GAC.
How to implement the same with sandbox solutions.
R Prashanth kumar
Hi Maulik,
ReplyDeleteCan you give me complete xml for above sample i tried in the same way byadding into existing list view wbpart, but it doesn't worked. please let me know how to use it. i am new to sharepoint 2010.