Showing posts with label SharePoint Designer. Show all posts
Showing posts with label SharePoint Designer. Show all posts

Thursday, October 22, 2009

Remove auto focus on SharePoint web form

We normally get auto focus if there is a textbox on the SharePoint web form. That works fine if the textbox is on the top of the web form, but if there is some content on the top and there is a textbox on the bottom of the page then it is a problem i.e since there will be auto focus on the textbox, when the page loads it will directly move the page to bottom to get the autofocus on the textbox. This happens because SharePoint automatically calls a JavaScript function named WebForm_AutoFocus(focusId) to get the auto focus on the textbox. This JavaScript function is present in the Core.js file. If you don’t want the textbox to get auto focus on the page load then simply override this function in your page, i.e simply call this function this way in your page…

function WebForm_AutoFocus(focusId)
{
//when the page loads, focus is automatically set to commets textbox, to remove the auto focus override this function.
//Override the AutoFocus function
}

Wednesday, July 15, 2009

Client side validation for rich text field in SharePoint

I have faced lot of problem in validating a rich text box in SharePoint through JavaScript. Problem is that it does not recognize the text present in the rich text box.

Here is the solution for validating the rich text box.

function validateComments()
{
//debugger;
var Comment = getTagFromIdentifierAndTitle('textarea','TextField','Comment');
var docEditor=RTE_GetEditorDocument(Comment.id);
if (null==docEditor)
return;
strHtml=docEditor.body.innerHTML;
//alert(strHtml);
if(strHtml !="" && strHtml !="
")
{
return true;
}
else
{
alert('Please enter your comment in the box above');
return false;
}
return true;
}

Wednesday, June 17, 2009

Read more>> option for rich text field in SharePoint

One of the most common requirements in customized SharePoint sites is that if there are announcements that are getting displayed then we may want to some lines of text and then show Read more>> option. This is how we implement it.

<xsl:choose>
<xsl:when test="string-length@Body) &gt; 500">
<xsl:variable name="BodyText" select="substring@Body,1,500)"></xsl:variable> <xsl:variable name="BodyContent"> <xsl:call-template name="substring-before-last"> <xsl:with-param name="input" select="$BodyText" /> <xsl:with-param name="substr" select="string' ')" /> </xsl:call-template> </xsl:variable> <xsl:value-of disable-output-escaping="yes" select="concat$BodyContent,' ')" /> <xsl:if test="not@Body = '')"> <xsl:if test="string-length@Body) &gt; 500"> <a href="ViewHeadlines.aspx?ID={@ID}" style="white-space:nowrap" >Read more&gt;&gt;</a> </xsl:if> </xsl:if> </xsl:when> <xsl:otherwise> <xsl:value-of disable-output-escaping="yes" select="@Body" /> </xsl:otherwise> </xsl:choose>

Function:

<xsl:template name="substring-before-last"> <xsl:param name="input" /> <xsl:param name="substr" /> <xsl:if test="$substr and contains$input, $substr)"> <xsl:variable name="temp" select="substring-after$input, $substr)" /> <xsl:value-of disable-output-escaping="yes" select="substring-before$input, $substr)" /> <xsl:if test="contains$temp, $substr)"> <xsl:value-of select="$substr" /> <xsl:call-template name="substring-before-last"> <xsl:with-param name="input" select="$temp" /> <xsl:with-param name="substr" select="$substr" /> </xsl:call-template> </xsl:if> </xsl:if></xsl:template>

This is how the logic works…..
It first checks if the length of the rich text field if greater than 500 characters. If it is greater than 500 characters then it will take the first 500 characters. There is a possibility that the end of 500 characters might be in the middle of a word, so I am again calling a recursive function “substring-before-last” in XSLT which finds the last space in the text and cuts the text to the end of last space. After the end of last space I am appending the Read more>> option.

Problems:

If the rich text contains html elements like anchor tag even those are counted as characters.

Breaking the rich text at 500 characters might result in the middle of the anchor tag or a span or bold(HTML elements), In this case content authors should take care.

If you are copying the text from Word or PDF or any other source, then the styles are carried forwarded and this might result in SharePoint Designer acting cranky like text becoming double automatically etc…. here is an example of this http://blog.thekid.me.uk/archive/2007/02/18/problems-with-sharepoint-designer.aspx

Another way of implementing the same requirement is here
http://mdasblog.wordpress.com/2009/01/20/displaying-the-first-n-words-of-a-long-text-column-with-xsl/

but the problem with this one is that it strips of all HTML formatting.

Playing flash and media files in SharePoint

On our site we had posts(executive messages) getting displayed every week. As a part of this we were storing the title, description, image link(path – this is the link to executive image of who has given the executive message) and video file link(path - we normally upload the video files to the media library and we get the link to video file) in the SharePoint announcements list. Media can be in flash or .wmv format. Requirement is like this.
• There can be video, image and text. In this case when the page loads first image should be displayed then when the user clicks on the image then the image should disappear and video should start playing.
• There can be video and text.
• There can be image and text.
• There can be only text.
• If the text length is more then specified characters then it should get cut of after some specified characters and provide the option of Read more>>

Here is how I have customized XSLT with DataView web part and implemented it.
<script type="text/javascript">

function PlayVideo(photoId, videoId, playerId)
{
document.getElementById(photoId).style.display = "none";
document.getElementById(videoId).style.display = "block";
document.getElementById(playerId).play();
}

function PlayFlashVideo(photoId, videoId, playerId)
{
document.getElementById(photoId).style.display = "none";
document.getElementById(videoId).style.display = "block";
}
</script>











And the output would be like this.




Thursday, February 19, 2009

Sort a Data View Web Part based on QueryString

There was a requirement where I have to sort a data view webpart on multiple parameters like date, size and LOB. Initially I thought of having three different pages for sorting, but that is very bad idea. So thought of implementing it with the concept of query string.

I want to use the sort option provided by data view web part and provide SortField as query string parameter. After thinking for a while and providing multiple hit and trials I have succeeded. Below is the expression for sorting using query string.

@*[name()= $SortField]

We write this in the edit sort expression.

And in the URL….

http://ServerName/Lists/Deals/LOB.aspx?LOB=All&SortField=Deal_x0020_Date



Thursday, February 12, 2009

Apply multiple conditions to filter data in Data View web part

I was wondering if there is a way to filter the data present in data view web part by applying multiple conditions like this

If((@ApprovalStatus = 'Approved') and (@LOB = $Param1 or $Param1 = 'All'))

After thinking for sometime, I got an idea to add XSLT filtering instead of applying filter criteria available.



So I have checked the checkbox “Add XSLT filtering”, clicked on edit and added the following XSLT like this:

[(@ApprovalStatus = 'Approved') and (@LOB = $Param1 or $Param1 = 'All')]

Sunday, February 8, 2009

Displays one announcement at a time using DataView webpart

I had a requirement in my project where I had to customize the display of announcements. I was supposed to display single announcement at a time, providing the provision for the user, to navigate through the list of announcements one by one. User should be able to view next, previous and all the announcements. This needs to be done without writing any code.

Here is my approach for this problem. I have created a view for the announcements list which contains required columns along with the Id column. ID column is mandatory for this approach. Using SharePoint designer, in the announcements list I have created a custom form. Use this custom form to display one announcement at a time. Place a data view webpart on this form and customize it to accept query string for ID column to display that announcement.

Place Bank To all View Previous View Next links on the top and bottom of the webpart in this way.



Use the following JavaScript to access and display one announcement at a time.