Link an item to its URL with itemstyle.xsl in SharePoint 2010

I’ll show you how to link an item to its URL within itemstyle.xsl so your URLs show up friendly in Content Query WebParts. It’s just a few lines of code.

First, we need to call the “SafeLinkUrl” variable. That variable calls the GetSafeLink template with the UrlColumnName. All that to say…this is the piece of code you need:

<xsl:template name="ItemListCat" match="Row[@Style='ItemListCat']" mode="itemstyle">
<xsl:variable name="SafeLinkUrl">
  <xsl:call-template name="OuterTemplate.GetSafeLink">
   <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
  </xsl:call-template>
</xsl:variable>

Once you’ve called that variable, you can link it using an href of {$SafeLinkUrl}. See my full code of a sample template that shows a link to the title below.

<xsl:template name="ItemList" match="Row[@Style='ItemList']" mode="itemstyle">
  <xsl:variable name="SafeLinkUrl">
    <xsl:call-template name="OuterTemplate.GetSafeLink">
      <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
    </xsl:call-template>
  </xsl:variable>
 <a href="{$SafeLinkUrl}"><xsl:value-of select="@Title"/></a>
</xsl:template>

Create a Classifieds Listing using SharePoint 2010

Creating a “classifieds” listing in Sharepoint 2010 is really easy. You just need a list with a custom column or two, and some pages.

Here’s the list setup:

For my category choices I have: Community Events, Advertisements, For Sale, Free Stuff, General Discussion Topics, etc. Whatever you want to do. Then what you can do is create a landing page that has 4 or 5 webpartzones (again, totally up to you). In those webpartzones place some CQWPs that pull all the items within a specific category, sorted by created date (descending of course). I have customized the itemstyle.xsl page to show the link to the item followed by a really short formatted date. Here’s the xsl for that (see my post on customizing itemstyle.xsl):

<xsl:template name="DiscussionItem" match="Row[@Style='DiscussionItem']" mode="itemstyle">
<xsl:variable name="vv1">
     <xsl:value-of select="@LinkUrl"/>
</xsl:variable>
<div class="classified-item">
 <a href="{$vv1}"><xsl:value-of select="@Title"/></a> (<xsl:value-of select="ddwrt:FormatDateTime(string(@Created),1033,'MMM-dd')"/>)</div>
</xsl:template>

You’ll wind up with something like this:

To create a new listing, click “Add New Item” (this is a button created in a CEWP that goes to the “new item” form aspx page), select the correct category, input the info, and click “save”.


Itemstyle.xsl in SharePoint 2010: A guide for designers

* 4 Comments

Arguably the most useful tool in SharePoint 2010 is the Content Query WebPart (CQWP). This little webpart allows you rollup content from anywhere in the site collection and display it however you like. Yes, it has limitations, but this post is about the things you can do with it. But of course the CQWP is incomplete without a working knowledge of xsl styles. In SharePoint, all styles for the CQWP are stored in itemstyle.xsl. I’m not going to go into creating your own stylesheets, linking to CQWPs, etc. You can figure that out. We will dive into what is possible with styling CQWP results.

We’ll be creating something like this:

Skip ahead:


Create your list


Before we begin, create a list with the following columns:
Title (standard text field)
Body (rich text area)
SiteLink (hyperlink column)


Declare a new style and insert fields


Every new style in itemstyle.xsl must be wrapped in the following tags:

<xsl:template name="testannouncement" match="Row[@Style='testannouncement']" mode="itemstyle"></xsl:template>

Inside this tag, we’ll begin by simply adding in our fields. I like to bring in all my fields, confirm they work on the page, and then begin styling.

<xsl:value-of select="@Title"/>
<xsl:value-of select="@Body"/>
<xsl:value-of select="@Author"/>
<xsl:value-of select="@SiteLink"/>

When you call a variable, it is either already built into the system (Title, Author), or you will need to customize it in the browser to add in your specific fields into those value placeholders.


Format the Hyperlink


The first thing we’ll do is format the hyperlink field to make it a friendly URL. The field, by default, puts the URL and then the friendly name concatenated together, but separated by a column. So what we do is create two variables in xsl, one that pulls everything before the comma, one that pulls everything after the comma. Then we will put the URL into an “a” tag.

<xsl:variable name="SiteLink" select="substring-before(@SiteLink,',')"/>
<xsl:variable name="SiteName" select="substring-after(@SiteLink,',')"/>
Link: <a href="{$SiteLink}"><xsl:value-of select="$SiteName"/></a>


Link Author to their mysite


Next, I want to take the Author’s name and link it to their MySite profile. To do that we are going to create a new variable that takes the login ID of the user (using ddwrt:UserLookup on the @Author tag). Once we have that ID we’re going to pass it into a parameter of the mysite URL.

<xsl:variable name="authoremail">
<xsl:value-of select="ddwrt:UserLookup(string(@Author) ,'Login')" />
</xsl:variable>
Questions about this? Contact <a href="https://m.isb.bj.edu.cn/Person.aspx?accountname={$authoremail}"><xsl:value-of select="@Author" /></a>


Format the Body to use markup


Your body content probably includes all the markup at the moment. The quick fix to that is adding in a disable-output-escaping=”yes” attribute.

<xsl:value-of select="@Body" disable-output-escaping="yes"/>


Final Code


Finally, just add some divs with classes to style. Here is my final code.

<xsl:template name="testannouncement" match="Row[@Style='testannouncement']" mode="itemstyle">
<xsl:variable name="SiteLink" select="substring-before(@SiteLink,',')"/>
<xsl:variable name="SiteName" select="substring-after(@SiteLink,',')"/>
<xsl:variable name="authoremail">
<xsl:value-of select="ddwrt:UserLookup(string(@Author) ,'Login')" />
</xsl:variable>
<div id="DailyAnnouncement">
<div class="title"><h4><xsl:value-of select="@Title"/></h4></div>
<div class="details">
<xsl:value-of select="@Body" disable-output-escaping="yes"/>
<xsl:if test="@SiteLink != ''">
<br/>Link: <a href="{$SiteLink}"><xsl:value-of select="$SiteName"/></a>

</xsl:if>
</div>
 <div class="contactemail">Questions about this? Contact
	<a href="https://m.isb.bj.edu.cn/Person.aspx?accountname={$authoremail}">
	<xsl:value-of select="@Author" />
	</a>
 </div>
</div>
</xsl:template>

Here’s my CSS

#DailyAnnouncement {
	display:block;
	border:1px solid #fff;
	padding-right:5px;
}
#DailyAnnouncement div.title h4 {
	margin:0px;
	padding:0px;
	line-height:normal;
	background:#efefef;
	border:1px solid #ccc;
	padding:4px;
	background:#efefef url('/PublishingImages/Logos/announcement.gif') no-repeat 0px 0px;
	padding-left:35px;
	display:block;
}

#DailyAnnouncement div.details {
	border:1px solid #ccc;
	border-top:0px;
	padding:10px;
	display:block;
}

#DailyAnnouncement div.contactemail {
	background:#efefef;
	padding:4px;
	text-align:right;
	border:1px solid #ccc;
	border-top:0px;
}

jQuery Slideshow (Carousel) in Sharepoint 2010

* 18 Comments

This blog is also featured on NothingButSharePoint.com
Note: This post was updated Dec. 5 to correct a few errors in the code markup.

This is a solution to create a jQuery slider (carousel) in Sharepoint 2010. It is based on an image library, and uses the tinycarousel jQuery plugin. (more…)


Convert any link into a modal dialog box in Sharepoint 2010

This will show you how to take any ordinary link in Sharepoint, and make it open its contents in a modal dialog box.

You must know that it depends whether or not you are using a Master page or itemstyle.xsl. I’ll explain both, first how to do it on a master page, then how to do it in itemstyle.xsl.

Step 1: How to convert a link on a master page to a modal dialog box
If you are editing a link on a master page, it is assumed that you are directly adding a link right into the layout. For my example, I use a custom link on the sidebar that opens an input form for a feedback list. I want it to open up in a dialog box on that page, so here’s how I edit the link:

 <code>href="javascript;"  onClick="javascript:SP.UI.ModalDialog.showModalDialog({   url: 'http://www.bentedder.com/',  title: 'Ben Tedder's website'});  return false;  "&gt;  link goes here 

Step 2: How to convert a link in itemstyle.xsl to a modal dialog box

&lt; a&gt;


javascript:SP.UI.ModalDialog.showModalDialog({ 
 url: 'http://www.bentedder.com/',
 title: 'Ben Tedder's website'});
 return false;
 

Put MySite link of creator in CQWP using itemstyle.xsl

This will show you how to correctly link the creator of an item to their mysite in a Content Query WebPart.

Step 1:
Declare the variable “authormysite” and the author string of “UserLookup”. We pull the “Login” parameter, because the username is what is appended on the mysite link out of the box.

<xsl:variable name="authormysite">
<xsl:value-of select="ddwrt:UserLookup(string(@Author) ,'Login')" />
</xsl:variable>

Step 2:
Create the link to the mysite page, appending the new variable’s value that you just created.

< a href="https://m.isb.bj.edu.cn/Person.aspx?accountname={$authormysite}" ></ a>

Step 3:
Within the link, you want to declare the value of @Author to get their full display name.

<xsl:value-of select="@Author" />

Final code:

<xsl:template name="mysitelink" match="Row[@Style='mysitelink']" mode="itemstyle">
<xsl:variable name="authormysite">
<xsl:value-of select="ddwrt:UserLookup(string(@Author) ,'Login')" />
</xsl:variable>

< a href="https://m.isb.bj.edu.cn/Person.aspx?accountname={$authormysite}">
<xsl:value-of select="@Author" />
</ a>

</xsl:template>

Use a friendly URL in a Sharepoint list display

Is your URL waaaaaaaaay too long for your display form (usually dispform.aspx) in Sharepoint 2010? This is an easy fix.

Step 1:
Create a new display form for your list item in Sharepoint Designer

Step 2:
Find your URL field. My URL is a link to the mysite of a person.

Step 3:
Create a new variable, wrapping what is currently there as your xsl item.

So in my case, I replaced this:

<xsl:value-of select="@MySiteLink"/>

with this:

<xsl:variable name="linktomysite">
  <xsl:value-of select="@MySiteLink"/>
</xsl>

Step 4:
Finally, call the variable in a nice short, user-friendly link

< a href="{$linktomysite}">MySite Profile</ a>