Quantcast
Viewing all articles
Browse latest Browse all 6517

New Post: GetListItems, XMLtoJSON, and jQueryUI autocomplete

Eric, Matt,

Here is what I'm using in a Data View webpart... It generates a json object ready to be consumed on the client side. In my usage, I wanted the data to be provided back as txt and not evaluated to a javascript object... the reason being I did not want to take up memory with large data structure until I actuality need it. I have used this to dump out entire list content, even those that have fields of type "Rich Text" (html) with no issues (thus far).

So the xslt below writes the json data as text to a script tag that is set to type text/plain (note the type attribute).  Then, when appropriate on the browser, I read it as:

 

var data = $.parseJSON( $("#jsonData").html() );

 

XSLT template is:

 

<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">
    <xsl:output method="html" indent="no"/>
    <xsl:decimal-format NaN=""/>
    <xsl:param name="dvt_apos">'</xsl:param>
    <xsl:param name="dvt_firstrow">1</xsl:param>
    <xsl:param name="dvt_nextpagedata" />
    <xsl:variable name="dvt_1_automode">0</xsl:variable>
    
     <xsl:template match="/"
                xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">
        <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>
        <div class="jsonData">
        
        <script type="text/plain" id="jsonData">
        
        <xsl:text>{"data": [</xsl:text>
        <xsl:if test="count($Rows)">
            <xsl:for-each select="$Rows">
                <xsl:text>{</xsl:text>
                <xsl:variable name="thisRowAttr" select="@*" />
                <xsl:for-each select="$thisRowAttr">
                                <xsl:text>"</xsl:text>
                    <xsl:value-of select="name()" />
                                <xsl:text>":"</xsl:text>
                        
                                <xsl:variable name="noQuotes">
                                                <xsl:call-template name="doTextReplace">
                                                                <xsl:with-param name="textValue" select="." />
                                                                <xsl:with-param name="findValue">&quot;</xsl:with-param>
                                                                <xsl:with-param name="replaceValue">\&quot;</xsl:with-param>
                                                </xsl:call-template>
                        </xsl:variable>
                                <xsl:variable name="noNewLineBreaks">
                                                <xsl:call-template name="doTextReplace">
                                                                <xsl:with-param name="textValue" select="$noQuotes" />
                                                                <xsl:with-param name="findValue"><xsl:text>&#13;&#10;</xsl:text></xsl:with-param>
                                                                <xsl:with-param name="replaceValue">
                                                                                <xsl:text>\n</xsl:text>
                                                                </xsl:with-param>
                                                </xsl:call-template>
                        </xsl:variable>
                        
                        
                        <xsl:value-of select="$noNewLineBreaks" />
                                <xsl:text>"</xsl:text>
                                <xsl:if test="count($thisRowAttr) != position()">
                                                <xsl:text>,</xsl:text>
                                </xsl:if>
                </xsl:for-each>
                <xsl:text>}</xsl:text>
                <xsl:if test="count($Rows) != position()">
                                <xsl:text>,</xsl:text>
                </xsl:if>
            </xsl:for-each>  
        </xsl:if>
        <xsl:text>]}</xsl:text>
        
        </script> </div>
        
    </xsl:template>   
    
    <xsl:template name="doTextReplace">
        <xsl:param name="textValue" />
        <xsl:param name="findValue" />
        <xsl:param name="replaceValue" />
        <xsl:variable name="first" select="substring-before($textValue, $findValue)" /> 
        <xsl:variable name="remaining" select="substring-after($textValue, $findValue)" />
        
        <xsl:choose>
            <!-- textValue was empty... return nothing. -->
            <xsl:when test="not(string-length($textValue))"> 
                <xsl:text></xsl:text>
            </xsl:when>
            <!-- textValue did not contain the findValue... Return it. -->
            <xsl:when test="not(contains($textValue, $findValue))">
                <xsl:value-of select="$textValue" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$first" />
                <xsl:value-of select="$replaceValue" />
            </xsl:otherwise>
        </xsl:choose>
        <xsl:if test="$remaining">
            <xsl:call-template name="doTextReplace">
                <xsl:with-param name="textValue" select="$remaining" /> 
                <xsl:with-param name="findValue" select="$findValue" />
                <xsl:with-param name="replaceValue" select="$replaceValue" />
            </xsl:call-template>
        </xsl:if>

    </xsl:template>
        
                        
</xsl:stylesheet>

 

Like I said earlier, I use this mostly when I need to get data that combines content from two lists.  I will also be using it soon (new project) to calculate some metrics on the server based on some list content, and send back only the metrics (ex. {Failed: 20, Passed: 30})

Hope this helps (now or in the future).

 

ps. the doTextReplace <xsl:template> above can actually be used for anything.. Its generic sna I used it today client side to manipulate dats for presentation (ex. to break up a list of Users on People field that has multiple users .

Paul.


Viewing all articles
Browse latest Browse all 6517

Trending Articles