Tuesday, June 17, 2008

Data View bug on Anonymous Pages

I spent two days trying to fix this problem so I thought I would send it along in case any of you run into it.

I noticed that I had a page on a publishing site that worked fine when logged in but one of the three data views on the page gave the following error when I viewed the page anonymously.

"Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Windows SharePoint Services-compatible HTML editor such as Microsoft Office SharePoint Designer. If the problem persists, contact your Web server administrator."

For some reason the data view web part didn't put a "WebURL" select parameter in "DataSources" for this one control. I not sure why this only affected anonymous users. When I added this parameter with a value of {sitecollectionroot} everything worked fine.

Thursday, June 12, 2008

Data View - Using a Conditional Statement to Build a Variable for an XPath Expression

I was building a Data View in SharePoint Designer that showed a training schedule. I wanted to use a conditional statement to build the XPath expression that selected the classes. If the page was passed a CourseTitle parameter in the querystring then I wanted to use it to select only classes scheduled for that course. If there wasn't a CourseTitle parameter I wanted to select all scheduled classes.

First I tried a choose statement like the following...

<xsl:choose>

<xsl:when test="string-length(CourseTitle) != 0">

<xsl:variable name="Rows" select="/dsQueryResponse/Class_Schedule/Rows/Row[normalize-space(@SC_Course_Title) = $CourseTitle and number(translate(substring before(@Start_x0020_Date,'T'),'-','')) &gt; number(translate(substring-before($Today,'T'),'-',''))]">

</xsl:when>

<xsl:otherwise>

<xsl:variable name="Rows" select="/dsQueryResponse/Class_Schedule/Rows/Row[number(translate(substring-before(@Start_x0020_Date,'T'),'-','')) &gt; number(translate(substring-before($Today,'T'),'-',''))]">

</xsl:otherwise>

</xsl:choose>

I also tried just two separate "<xsl:if statements />" but that didn't work either.

The error message I got was "a required variable (Rows) isn't present". As pointed out by a colleague (Jeremy Luerkens), the error was being caused later in the application when I tried to reference the "Rows" variable. The "Rows" variable was out of scope at that point and no longer valid.

I had to separate the conditional statement into its own variable (condition) then in the "Rows" variable I had to set the node to "self::node" when calling the "condition" variable so the select would get created in the right scope.

<xsl:variable name="condition" select="boolean(string-length($QSCourseTitle) = 0)"/>

<xsl:variable name="Rows" select="self::node()[$condition] /dsQueryResponse/Class_Schedule/Rows/Row[number(translate(substring-before(@Start_x0020_Date,'T'),'-','')) &gt; number(translate(substring-before($Today,'T'),'-',''))] | self::node()[not($condition)] /dsQueryResponse/Class_Schedule/Rows/Row[normalize-space(@SC_Course_Title) = $QSCourseTitle and number(translate(substring-before(@Start_x0020_Date,'T'),'-','')) &gt; number(translate(substring-before($Today,'T'),'-',''))]"/>