Proper display of decimal comma in XSLT data view webpart

Filed Under (SharePoint, XSLT) by Boris Gomiunik on 25-11-2009

If you’re having trouble with format-number function in XSLT data view – it’s probably because of decimal comma. The server returns data with decimal comma, and the format-number function works with decimal dot. The workaround is to translate the values

<xsl:value-of select="format-number(translate(translate(@NumberField,’.',’,'),’,',’.'), ‘#.##0,00;-#.##0,00′, ‘lcid1060′)" />

(in the example above the @NumberField represents the internal name of the field that contains numerical value.

Also make sure that in HTML just before the </xsl:stylesheet> tag you have included the following tag:

<xsl:decimal-format decimal-separator="," grouping-separator="." name="lcid1060" />

Inserting JavaScript into XSLT

Filed Under (JavaScript, MOSS, SharePoint, XSLT) by Boris Gomiunik on 12-03-2009

When inserting JavaScript code into XSLT dataForm webpart it can sometimes get tricky. Sometimes SharePoint designer messes up our code or we have huge trouble with & signs,… the best way to insert the JavaScript code block into XSLT data view is to enclose it with xsl:text and cdata. Below is the sample:

<xsl:text disable-output-escaping=”yes”>
<![CDATA[
<script type=”text/javascript”>
(your JavaScript code here)
</script>
]]>
</xsl:text>

“Type Microsoft.SharePoint.WebControls.FormField’ does not have a public property named ‘xmlns:SharePoint” error

Filed Under (JavaScript, XSLT) by Boris Gomiunik on 15-02-2009

If you’ve made custom forms with SharePoint designer and you’ve modified it quite a lot, everything looks good in SharePoint designer, but when you open it in the web browser, you get the following error, the solution is simple:

put your insert/edit form in a div. Let me explain more in detail. A lot of times I don’t want my custom SPD insert of edit forms to be in tables. So I remove all the table tags and format the form with layers, spans, and custom CSS. Like in the screen below. The dvt_1.rowinsert template is clean. And the preview in SharePoint Designer is displaying it ok.

image

But when previewing it in web browser you get the error:

image

(screenshot is from Slovene website).

All I need to do to resolve this problem is to put the form in a layer (div) – or a table. See the example below (I’ve marked wrapper div with comments)

image

You can also place the div inside of entire template if you wish. As long as the sharepoint form fields have wrapper.

And your custom form iwll magically become alive

image

Happy formatting.

Oznake ponudnika Technorati: ,

Limit parts of page or DataForm WebPart to the ones with rights

Filed Under (SharePoint, XSLT) by Boris Gomiunik on 17-01-2009

If you see any SharePoint page anonymously, you can see only the items you’re supposed to see. If we as visitors have no rights to see certain site, it’s not displayed. Also if any elements of a list or a document library has specific permissions not to be available for public, we can’t see it.

SharePoint has a very good Security model, which we can use (yes, and even without visual Studio). We can set to display certain parts of page or DFWP based on rights the visiting user has. For example: If we have a special part that only the users that have the right to edit, can see, we’d wrap it in a conditional formatting tag with a condition ddwrt:IfHasRights(4)

<xsl:if test=”ddwrt:IfHasRights(4)”>Only Editors can see this text!</xsl:if>
 

The number 4 in the example above represents the editing right. You’ll find all available rights permissions in the table below. The example above is good for hiding the “Edit” button.

You can make the same outside of a WebPart. But you need to use different kind of tag to nest the protected content in:

<Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="EditListItems">Only Editors can see this text!</Sharepoint:SPSecurityTrimmedControl>


Use the example above anywhere on the webpage outside of a webpart. As we can see this time the permission is defined with a string instead of number. For this tag to be working, don’t forget to register the SharePoint tagprefix before with

<%@ Register tagprefix="SharePoint" namespace="Microsoft.SharePoint.WebControls" assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

 
(in SharePoint’s default master page it’s already added). You’re probably asking which are the values. Thanks to Ian Morrish I’ve found the whole set. (the strings for PermissionString are quite self-descriptive)

SPSecurityTrimmedControl ddwrt:IfHasRights
ViewListItems 1
AddListItems 2
EditListItems 4
DeleteListItems 8
ApproveItems 16
OpenItems 32
ViewVersions 64
DeleteVersions 128
CancelCheckout 256
PersonalViews 512
ManageLists 2048
ViewFormPages 4096
Open 65536
ViewPages 131072
AddAndCustomizePages 262144
ApplyThemeAndBorder 524288
ApplyStyleSheets 1048576
ViewUsageData 2097152
CreateSSCSite 4194314
ManageSubwebs 8388608
CreateGroups 16777216
ManagePermissions 33554432
BrowseDirectories 67108864
BrowseUserInfo 134217728
AddDelPrivateWebParts 268435456
UpdatePersonalWebParts 536870912
ManageWeb 1073741824
UseRemoteAPIs 137438953472
ManageAlerts 274877906944
CreateAlerts 549755813888
EditMyUserInfo 1099511627776
EnumeratePermissions 4611686018427387904
FullMask 9223372036854775807

 

There are some considerations you should take in mind:

1. This doesn’t work on “System” pages – in _layouts folder

2. This is chekcing the security against the actual page you’re viewing (if you put the spsecuritytrimmedcontrol in a masterpage it will check ivisitors permissions on a page he’s viewing, not on a masterpage).

Oznake ponudnika Technorati: ,,

XSL template for adding days

Filed Under (SharePoint, XSLT) by Boris Gomiunik on 06-11-2008

I can’t believe it’s so complex to add couple of days to a certain date. I needed an XSLT way of calculating dates with input parameters for date (format YYYY-MM-DD) and number of days (number). I know, some of you say use EXSLT. After not getting it to work, I’ve decided to do my own version.

Please note that this template is not for calculating more than one month (for example to add 50 days. And date has to be in ISO format (YYYY-MM-DD). It can be a starting point for your modifications and needs to work with dates.

<xsl:template name="calcDate">
    <xsl:param name="date"/>
    <xsl:param name="days"/>
    <xsl:param name="oldMonth"><xsl:value-of select="substring($date,6,2)"/></xsl:param>
    <xsl:param name="oldYear"><xsl:value-of select="substring($date,1,4)"/></xsl:param>
    <xsl:param name="oldDay"><xsl:value-of select="substring($date,9,2)"/></xsl:param>
    <xsl:param name="newMonth">
        <xsl:choose>
            <xsl:when test="$oldMonth = '01'">
                <xsl:choose>
                    <xsl:when test="number($oldDay) + $days &lt;= 31">01</xsl:when>
                    <xsl:otherwise>02</xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '02'">
                <xsl:choose>
                    <xsl:when test="($oldYear mod 4 = 0 and number($oldDay) + $days &lt;= 29) or ($oldYear mod 4 != 0 and number($oldDay) + $days &lt;= 28)">02</xsl:when>
                    <xsl:otherwise>03</xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '03'">
                <xsl:choose>
                    <xsl:when test="number($oldDay) + $days &lt;= 31">03</xsl:when>
                    <xsl:otherwise>04</xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '04'">
                <xsl:choose>
                    <xsl:when test="number($oldDay) + $days &lt;= 30">04</xsl:when>
                    <xsl:otherwise>05</xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '05'">
                <xsl:choose>
                    <xsl:when test="number($oldDay) + $days &lt;= 31">05</xsl:when>
                    <xsl:otherwise>06</xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '06'">
                <xsl:choose>
                    <xsl:when test="number($oldDay) + $days &lt;= 30">06</xsl:when>
                    <xsl:otherwise>07</xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '07'">
                <xsl:choose>
                    <xsl:when test="number($oldDay) + $days &lt;= 31">07</xsl:when>
                    <xsl:otherwise>08</xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '08'">
                <xsl:choose>
                    <xsl:when test="number($oldDay) + $days &lt;= 31">08</xsl:when>
                    <xsl:otherwise>09</xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '09'">
                <xsl:choose>
                    <xsl:when test="number($oldDay) + $days &lt;= 30">09</xsl:when>
                    <xsl:otherwise>10</xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '10'">
                <xsl:choose>
                    <xsl:when test="number($oldDay) + $days &lt;= 31">10</xsl:when>
                    <xsl:otherwise>11</xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '11'">
                <xsl:choose>
                    <xsl:when test="number($oldDay) + $days &lt;= 30">11</xsl:when>
                    <xsl:otherwise>12</xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '12'">
                <xsl:choose>
                    <xsl:when test="number($oldDay) + $days &lt;= 31">12</xsl:when>
                    <xsl:otherwise>01</xsl:otherwise>
                </xsl:choose>
            </xsl:when>
        </xsl:choose>
    </xsl:param>
    <xsl:param name="newYear">
        <xsl:choose>
            <xsl:when test="$oldMonth = '12' and (number($oldDay) + $days) &gt;= 31"><xsl:value-of select="number($oldYear) + 1"/></xsl:when>
            <xsl:otherwise><xsl:value-of select="$oldYear"/></xsl:otherwise>
        </xsl:choose>
    </xsl:param>
    <xsl:param name="newDay">
        <xsl:choose>
            <xsl:when test="$oldMonth = '01'">
                <xsl:choose>
                    <xsl:when test="$newMonth = $oldMonth"><xsl:value-of select="number($oldDay) + $days"/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="number($oldDay) + $days - 31"/></xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '02'">
                <xsl:choose>
                    <xsl:when test="$newMonth = $oldMonth"><xsl:value-of select="number($oldDay) + $days"/></xsl:when>
                    <xsl:otherwise>
                        <xsl:choose>
                            <xsl:when test="$oldYear mod 4 = 0">
                                <xsl:value-of select="number($oldDay) + $days - 29"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="number($oldDay) + $days - 28"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '03'">
                <xsl:choose>
                    <xsl:when test="$newMonth = $oldMonth"><xsl:value-of select="number($oldDay) + $days"/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="number($oldDay) + $days - 31"/></xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '04'">
                <xsl:choose>
                    <xsl:when test="$newMonth = $oldMonth"><xsl:value-of select="number($oldDay) + $days"/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="number($oldDay) + $days - 30"/></xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '05'">
                <xsl:choose>
                    <xsl:when test="$newMonth = $oldMonth"><xsl:value-of select="number($oldDay) + $days"/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="number($oldDay) + $days - 31"/></xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '06'">
                <xsl:choose>
                    <xsl:when test="$newMonth = $oldMonth"><xsl:value-of select="number($oldDay) + $days"/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="number($oldDay) + $days - 30"/></xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '07'">
                <xsl:choose>
                    <xsl:when test="$newMonth = $oldMonth"><xsl:value-of select="number($oldDay) + $days"/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="number($oldDay) + $days - 31"/></xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '08'">
                <xsl:choose>
                    <xsl:when test="$newMonth = $oldMonth"><xsl:value-of select="number($oldDay) + $days"/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="number($oldDay) + $days - 31"/></xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '09'">
                <xsl:choose>
                    <xsl:when test="$newMonth = $oldMonth"><xsl:value-of select="number($oldDay) + $days"/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="number($oldDay) + $days - 30"/></xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '10'">
                <xsl:choose>
                    <xsl:when test="$newMonth = $oldMonth"><xsl:value-of select="number($oldDay) + $days"/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="number($oldDay) + $days - 31"/></xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '11'">
                <xsl:choose>
                    <xsl:when test="$newMonth = $oldMonth"><xsl:value-of select="number($oldDay) + $days"/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="number($oldDay) + $days - 30"/></xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$oldMonth = '12'">
                <xsl:choose>
                    <xsl:when test="$newMonth = $oldMonth"><xsl:value-of select="number($oldDay) + $days"/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="number($oldDay) + $days - 31"/></xsl:otherwise>
                </xsl:choose>
            </xsl:when>
        </xsl:choose>
    </xsl:param>
    <xsl:value-of select="$newYear"/>
    <xsl:text>-</xsl:text>
    <xsl:value-of select="$newMonth" />
    <xsl:text>-</xsl:text>
    <xsl:if test="$newDay &lt; 10">0</xsl:if><xsl:value-of select="$newDay"/>
    </xsl:if>
</xsl:template>

Usage:

Copy-paste the upper code to the end of your XSL stylesheet (after the last </xsl:template> tag in your data view. Then in your data view you can call the template.

<xsl:call-template name=”calcDate”>
<xsl:with-param name=”date” select=”substring-before(@Created,’T')”/>
<xsl:with-param name=”days” select=”5″/>
</xsl:call-template>

This will return five days from the created date.

There probably IS a better way, I just couldn’t find it. :)

Oznake ponudnika Technorati: ,,

Making "Post" and "Get" forms from SharePoint’s pages

Filed Under (JavaScript, SharePoint, XSLT) by Boris Gomiunik on 04-09-2008

If you’ve tried to insert a form into a sharepoint’s page you mus have been disappointed. The form just didn’t work. You can’t make the form to post or get to some url, because the whole SharePoint’s page is one big <form> element.

So I went a bit deeper on how Microsoft guys are doing it on their spaces site (blog this functionality makes a post to a certain url) and based on this, I’ve made this simple script to make “forms” to post and to submit.

First thing: Insert your form as it should be

<form action="http://www.somesite.com/somepage.aspx" method="post">
      <input name="name"/>
      ... etc. with all the fields you need.
      <input type="submit" value="Submit"/>
  </form> 

Before you save the page or the content editor webparts, make the three changes:

1. Remove the <form> tags and replace them with <div id=”contactForm”>

<div id="contactForm">
      <input name="name"/>
      ... etc. with all the fields you need.
      <input type="submit" value="Submit"/>
  </div> 

2. Add the following script before the form

<script type="text/javascript">
  function submitForm(containerElement) {
      var theForm = document.createElement("form");
          theForm.action = 'http://www.somesite.com/somepage.aspx';
          theForm.method = "post";
          theForm.innerHTML = document.getElementById(containerElement).outerHTML;
      var formToSubmit = document.body.appendChild(theForm);
          formToSubmit.submit();
  }
  </script>

Note: be sure to correct the theForm.action and theForm.method to appropriate url and method (for your form)

3. Change the submit button with

<button onclick="submitForm('contactForm')>Submit</button>

Note: Make sure you copy the ID of the layer (in the sample above is the contactForm) to the parameter of the submitForm function.

the end code would look something like below:

<script type="text/javascript">
function submitForm(containerElement) {
    var theForm = document.createElement("form");
        theForm.action = 'http://www.somesite.com/somepage.aspx';
        theForm.method = "post";
        theForm.innerHTML = document.getElementById(containerElement).outerHTML;
    var formToSubmit = document.body.appendChild(theForm);
        formToSubmit.submit();
}
</script>
<div id="contactForm">
    <input name="name" type="text"/>
    ... etc. with all the fields you need.
    <button onclick="submitForm('contactForm')>Submit</button>
</div>

Update (5.11.): Unfortunately this method works straight only in IE, because it’s the only one that coppies the innerHTML with the changes (input data). So to get this method to work in FF, Chrome, Safari, Opera,.. you have to copy the field values. Here’s how

After the line

var formToSubmit = document.body.appendChild(theForm);

add code similar to the one below for copying the values for each of the input fields

document.getElementsByName('name')[1].value = document.getElementsByName('name')[0].value;

Apply the similar methodology for other types of fields. I’ll try to come up with a universal script to copy values.

ads
ads
ads