Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

Show your Twitter log in SharePoint

About year ago I wrote how to display blog feeds in SharePoint sites using XML Web Part. Today is Twitter very popular and we can use XML Web Part to render Twitter feeds. To see how to configure XML Web Part check out blog feeds entry referred above. This time you have to insert user Twitter feed as XML link, by example http://twitter.com/statuses/user_timeline/12358522.rss. Click XSL Editor button and insert the following XSL to dialog window.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    exclude-result-prefixes="xsl">

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>   

<xsl:template match="/">       
    <xsl:value-of select="rss/channel/title" />
    <div>
        <xsl:apply-templates select="rss/channel"/>
    </div>   
</xsl:template>   

<xsl:template match="rss/channel">
    <xsl:variable name="link" select="link"/>
    <xsl:variable name="description" select="description"/>       

    <ul>
             <xsl:apply-templates select="item"/>
    </ul>
</xsl:template>   

<xsl:template match="item">       
    <xsl:variable name="owner">
        <xsl:value-of select="substring(/rss/channel/title,11)" />
    </xsl:variable>
    <xsl:variable name="item_link" select="link"/>
    <xsl:variable name="item_title" select="substring(description,string-length($owner)+3)"/>       

    <li>
        <xsl:call-template name="getDate">
            <xsl:with-param name="dateTime" select="pubDate" />
        </xsl:call-template> |
        <a href="{$item_link}" title="{$item_title}">
            <xsl:value-of select="$item_title"/>
        </a>
    </li>
</xsl:template>

<xsl:template name="getDate">
    <xsl:param name="dateTime" />
    <xsl:variable name="monthName">
        <xsl:value-of select="substring($dateTime,9,3)" />
    </xsl:variable>
    <xsl:variable name="day">
        <xsl:value-of select="substring($dateTime,6,2)" />
    </xsl:variable>
    <xsl:variable name="month">
        <xsl:choose>
        <xsl:when test="$monthName = 'Jan'">01</xsl:when>
        <xsl:when test="$monthName = 'Feb'">02</xsl:when>
        <xsl:when test="$monthName = 'Mar'">03</xsl:when>
        <xsl:when test="$monthName = 'Apr'">04</xsl:when>
        <xsl:when test="$monthName = 'May'">05</xsl:when>
        <xsl:when test="$monthName = 'Jun'">06</xsl:when>
        <xsl:when test="$monthName = 'Jul'">07</xsl:when>
        <xsl:when test="$monthName = 'Aug'">08</xsl:when>
        <xsl:when test="$monthName = 'Sep'">09</xsl:when>
        <xsl:when test="$monthName = 'Oct'">10</xsl:when>
        <xsl:when test="$monthName = 'Nov'">11</xsl:when>
        <xsl:when test="$monthName = 'Dec'">12</xsl:when>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="year">
        <xsl:value-of select="substring($dateTime,13,4)" />
    </xsl:variable>

    <xsl:variable name="time">
        <xsl:value-of select="substring($dateTime,18,8)" />
    </xsl:variable>

    <xsl:value-of select="concat($day,'.',$month,'.',$year,' ',$time)" />
</xsl:template>
</xsl:stylesheet>


Now apply or save changes and check out the result. You should see something like this.

SharePoint displaying Twitter feed

If you find any bugs in this code or you know some tricks how to make this XSL shorter then please feel free to drop me a line. :)

Comments

Links (2/22/2008) « Steve Pietrek - Everything SharePoint said:

Pingback from  Links (2/22/2008) &laquo; Steve Pietrek - Everything SharePoint

# February 22, 2009 8:18 PM

Michael Greth MVP SharePoint Blog said:

Nach längerer Zeit mal wieder ein paar Linktipps im Blog (müsste jetzt eigentlich Teetasse heißen) –

# February 23, 2009 3:32 AM

Ralph Boeije said:

Good idea, tnx! Looks great!

Problem is I get the following error message: Cannot retrieve the URL specified in the XML Link property. For more assistance, contact your site administrator.

I think it is because of the security settings in twitter rss (no anonymous access). How did you get it working.

# February 23, 2009 3:50 AM

DigiMortal said:

You have to use user time line URL: twitter.com/.../numeric_id.rss

# February 23, 2009 5:42 AM

Mirrored Blogs said:

Otro resumen de noticias y mas material de lectura… 1 – Twitter on SharePoint – En el siguiente articulo

# February 23, 2009 10:47 PM

singapore trip/boeing 777-200 said:

Pingback from  singapore trip/boeing 777-200

# February 24, 2009 12:12 PM

dotnetguts said:

Good Info, it helped me.

Thanks for sharing

# April 18, 2009 11:26 AM

Ken said:

Is there any way to limit the feeds that show up to like the last 5?

# July 27, 2009 6:13 PM

DigiMortal said:

Hi Ken!

It is possible to use variables and counting in xsl. It shouldn't be hard thing to do.

# July 27, 2009 6:55 PM

Douglas said:

Hey Gunner.  Looks like a great post.  I am running into an issue of security.  We have a SharePoint site (internal only with user authentication).  One of our exec is using twitter to comment about the company.  He has made his tweets private so others cant see them such as competition.  You are only able to use the widget in Twitter if your posts are public as with the XSL above.  Ideally when he updates Twitter, I want it to feed into the SP site.  Any suggestions?

# July 28, 2009 12:48 PM

DigiMortal said:

Hi Douglas!

XML web part, as far as I know, is able to show data from unprotected sources. Of course, you may try URL-s in form myurl but it is not sure if all IE users will see these messages due to security settings.

I think you need some server-side solution.

# July 28, 2009 1:55 PM

LS said:

Hi

thanks for this. I have changed the <li>s to little Twitter Balloons, moved the date below and removed milliseconds so it looks nicer. Oh and used a Twitter Bird at the Title icon!

One thing I can't work out is limiting the number of tweets as it ignores the Feed Limit.

Can anyone help?

Thanks

# September 9, 2009 11:20 AM

lvirden said:

I have attempted to use the above xslt code with a twitter feed. However, I end up with the xml web part displaying the error "Failed to apply XSLT to the content."

# November 10, 2009 3:25 PM

chris said:

I have been successful in following your example and adding your twitter feed into the SharePoint site I have to administer. This helps a lot for an upcoming project/requirement.  With that said, I really need to limit the number of feeds into the web part.  I am not a programmer (at best a script kiddie).  Could you please show an example of the code of variables and counting you mention in one of the post above.  Very much appreciated.  Regardless, thanks for your example.  

# December 24, 2009 10:54 AM

dotnetguts said:

It was awesome info.

Thank you so much for sharing.  Any idea, how can i show blogger feeds in sharepoint???

# February 22, 2010 11:58 PM

DigiMortal said:

Just take your blogger RSS and try out what happens. If you are not happy with results then modify XSL to fit your needs.

# February 23, 2010 2:09 AM

Geir Waaler said:

LS:

What does your code looks like after your changes?

LS and Chris: Any luck in limiting the nmber of feeds?

# June 2, 2010 2:42 AM

Geir Waaler said:

Really appreciate your example! This will me my first go of an XML web part in Sharepoint

Being a complete programming newbie, would it be easy to rewrite your code having the time stamp put below the post like Twitters "x hours ago"?

# June 2, 2010 2:50 AM

DigiMortal said:

Limiting the number of items in feed and calculating time ranges can be done using XSLT. The last one is not very nice thing to so for more demanding sites I suggest you to use some specific web part that has those options available.

# June 3, 2010 12:36 PM

Show your Twitter log in SharePoint – Gunnar Peipman's ASP.NET blog « xsl said:

Pingback from  Show your Twitter log in SharePoint &#8211; Gunnar Peipman&#039;s ASP.NET blog &laquo;  xsl

# June 5, 2010 5:21 AM

Blog del CIIN said:

Estos días he estado dándole bastantes vueltas a las posibilidades que brinda por defecto SharePoint

# September 21, 2010 5:14 PM

SharePoint 2010: Integración con Twitter (I)! « Pasi??n por la tecnolog??a… said:

Pingback from  SharePoint 2010: Integraci&oacute;n con Twitter (I)! &laquo; Pasi??n por la tecnolog??a&#8230;

# September 21, 2010 5:14 PM

oliver marshall said:

Any idea how I can change that to show only the last 5 rss items? I've tried using the RSS FEED limit in the web part in SP but that didnt do anything so I wondered whether it could be done in the XLT

# October 14, 2010 11:41 AM

fudoki said:

Great post Digimortal.

For those finding this blog and wanting to restrict the number of records just add a couple of extra lines to the item template.

In the section starting xsl:template match="item" find the <li>...</li> and add the following lines before and after the <li></li>

<xsl:if test="position() &lt; 6">

<li>

...

</li>

</xsl:if>

//fudoki

# February 24, 2011 1:12 PM

Sathish said:

How to show only one random.

cmsathish@gmail.com

# April 30, 2011 6:11 AM

Nilesh Patil said:

Hi  all ,

If u want to put  Twitter Icon With With Twitter Feeds(Twitter TWEET) of User then follow code:

put Image Src @ above "getdate  Template"

And One thing Remind

u r image must have .ico extension  and store it on sharepoint site in image folderor may be other folder

<img src="http://(Sharepoint -url))/images/twwe.ico" width="25" height="25"/>

<xsl:call-template name="getDate"><xsl:with-param name="dateTime" select="pubDate" />

</xsl:template>

# May 9, 2011 9:56 AM

Mikhail said:

Twitter features in sharepoint workflows: HarePoint Workflow Extensions ( www.harepoint.com/.../HarePointWorkflowExtensions ) – about 200 new workflow activities, including free ones.

# December 26, 2011 8:59 AM

Sharepoint twitter | Millerslandsca said:

Pingback from  Sharepoint twitter | Millerslandsca

# December 29, 2011 11:18 PM