Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have two lists Client and Project, each Client has from 0 to N Project stocked in his SPLookupField where multi values are allowed. And I created a new computed Column in the Client List where it shows a link to the page of all of my Projects only when he has indeed project otherwise it won't show the link. Here's the field in the schema of the Client List:

<Field ID="{BD65ECB7-1BFE-4154-844E-3FDC9A760830}" Name="ProjectsLink" StaticName="Projects" ShowInNewForm="FALSE" ShowInEditForm="FALSE" ShowInDisplayForm="FALSE"  Sortable="FALSE" Filterable="FALSE"  Type="Computed"  DisplayName="Projects" ClassInfo="Icon" AuthoringInfo="$Resources:core,linked_to_item;" >
    <DisplayPattern>
      <HTML><![CDATA[<a href="]]></HTML>
      <URL Cmd="DISPLAY" />
      <HTML><![CDATA[" onclick="GoToLink(this);return false;" target="_self">]]></HTML>
      <HTML><![CDATA[<img  Width="25px" Height="20px" border="0" alt="]]></HTML>
      <Field Name="FileLeafRef"  />
      <HTML><![CDATA[" src="/_layouts/DISPLAY.png">]]></HTML>
      <HTML><![CDATA[</a>]]></HTML>
    </DisplayPattern>
  </Field>

so in order to add this Field in my List I have created an XSLT file but I didn't know how to indicate my second condition because it depends on a method in my cs file:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
            xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
            version="1.0" exclude-result-prefixes="xsl msxsl ddwrt"
            xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
            xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
            xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt"
            xmlns:SharePoint="Microsoft.SharePoint.WebControls"
            xmlns:ddwrt2="urn:frontpage:internal" ddwrt:oob="true">
  <xsl:template name="FieldRef_ProjectsLink_body" ddwrt:dvt_mode="body" match ="FieldRef[@Name='ProjectsLink']" mode="Computed_body">
<xsl:param name="thisNode" select="."/>
<xsl:param name="ShowAccessibleIcon" select="0"/>
<xsl:param name="folderUrlAdditionalQueryString" select="''"/>
<xsl:variable name="ID">
  <xsl:call-template name="ResolveId">
    <xsl:with-param name="thisNode" select ="$thisNode"/>
  </xsl:call-template>
</xsl:variable>
<xsl:choose>
  <xsl:when test="$XmlDefinition/List/@TemplateType != 301"> <!--and Condition2-->
    <a onfocus="OnLink(this)" href="{$FORM_DISPLAY}&amp;ID={$ID}&amp;ContentTypeID={$thisNode/@ContentTypeId}" onclick="EditLink2(this,{$ViewCounter});return false;" target="_self">
      Details -
    </a>
    <a onfocus="OnLink(this)" href="/_LAYOUTS/Projects.aspx?ID={$ID}" onclick="EditLink2(this,{$ViewCounter});return false;" target="_self">
      Projects
    </a>
    </xsl:when>
    <xsl:otherwise>
      <a onfocus="OnLink(this)" href="{$FORM_DISPLAY}&amp;ID={$ID}" onclick="GoToLink(this);return false;" target="_self">
        Details -
      </a>
    </xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

and in my ClientManagement.cs file I have this function that gives me the number of all client's projects :

public static int getProjectsCount(int pClientID)
    {
         SPSite mysite = new SPSite(SPContext.Current.Web.Site.Url);
         SPWeb myweb = mysite.OpenWeb();
         SPListItem myContact = myweb.Lists["Client"].Items.GetItemById(pClientID);

         SPFieldLookupValueCollection fieldValues = myContact["Projects"] as SPFieldLookupValueCollection;
         return fieldValues.Count;
    }

So my question is how to call this function my XSL file to use it as a condition??

share|improve this question
    
Did you ever get this figured out? If so, please accept an answer below or provide the answer that worked for you. –  Steve Lineberry Mar 12 '13 at 18:24

2 Answers 2

up vote 1 down vote accepted

When you execute the transform, one of the required properties is the xslArgs. When creating this xslargs object you can assign it an extension object which you can then call methods within that object from your XSLT.

http://msdn.microsoft.com/en-us/library/system.xml.xsl.xsltargumentlist.addextensionobject.aspx

C# Sample Code

XsltArgumentList xslArgs = new XsltArgumentList();
ClientManagement cs = new ClientManagement();  //Your custom code
xslArgs.AddExtensionObject("urn:ClientManagement", cs);

XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(xsltcontent);

using (StringWriter writer = new StringWriter())
{
    transform.Transform(content, xslArgs, writer);
    return writer.ToString();
}

XSLT sample code

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myObj="urn:ClientManagement">
<xsl:value-of select="myObj:getProjectsCount($pClientID)"/>  
share|improve this answer

Could you embed the c# function in your XSL?

http://msdn.microsoft.com/en-us/library/wxaw5z5e%28VS.80%29.aspx

share|improve this answer
1  
according to this blog no! helpmeonsharepoint.com/2012/05/… –  Glolita Mar 6 '13 at 15:46
    
Good call -> I didn't see that blog & didn't realize that limitation. Hopefully the extension object that Steve Lineberry has proposed will work for you! –  Jason Rastovski Mar 6 '13 at 16:09
1  
@Glolita, that blog you posted seems to be talking about the same thing I am in my answer. –  Steve Lineberry Mar 6 '13 at 16:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.