Customizing the Display of a SharePoint 2010 Blog

I’ve changed pages countless times in SharePoint Designer, but this case, where I wanted to make a relatively small change to the home page of a Blog site, proved especially onerous.

In most cases, overriding the default XSL is as simply as editing what’s there and saving your changes.

It would seem that the team that works on blogs doesn’t chat with the rest of the product group, because blogs work differently. Blog pages use an XSL file called blog.xsl, which live in the _layouts folder on the Web Front Ends (WFEs).

On the home page of a blog site (default.aspx), there are several XLV Web Parts, but the main one is the one which displays the most recent posts. All I wanted to do in this case was display a different column instead of Created By (@Author) . Because in many organizations someone different that the author is the person who types the post, we had added a Written By column to the Posts list. The logic was simple: if the Written By column had a value, show it, otherwise show the Created By column as usual.

I wanted to keep the XLV Web Part in place in case we needed to work with it in the future, plus the markup it emits is a bit crufty and I didn’t want to have to replicate it all with a custom DVWP. (In retrospect, this *may* have been easier.)

It should have been pretty easy to make the change, but I went down some rat holes. I don’t want *you* to go down those rat holes, so here’s what ended up working.

Open the page in SharePoint Designer, position your cursor on the XLV Web Part which displays the posts and in the List View Tools section of the ribbon, choose Customize XSLT / Customize Entire View.

image

This pulls the XSL from blog.xsl into the page, in theory so that we can customize it. However, don’t go down that path; there be monsters. Instead, find the line:

<xsl:include href="/_layouts/xsl/blog.xsl"/>

and Ctrl-Click on the href link to the blog.xsl file. This will open the blog.xsl file in a different SharePoint Designer window for the root of the Site Collection. This is handy, because you now want to save a copy of blog.xsl in a Document Library in the root site of the Site Collection. I usually create a Document Library called XSL anyway to hold frequently-used XSL templates, like the ones I have in my SPXSLT Codeplex Project. I named my copy BlogWrittenBy.xsl.

Close the default.aspx page you opened in the blog site now WITHOUT saving it. Remember? Monsters. Open it again. See, no monsters.

Since we had added a new custom column called Written By, I needed to add it to the ViewFields section so that the XLV Web Part would retrieve values for it:

<ViewFields>
  <FieldRef Name="Title"/>
  <FieldRef Name="Body"/>
  <FieldRef Name="Author"/>
  <FieldRef Name="PostedByWithDate"/>
  <FieldRef Name="CategoryWithLink"/>
  <FieldRef Name="Permalink"/>
  <FieldRef Name="EmailPostLink"/>
  <FieldRef Name="NumCommentsWithLink"/>
  <FieldRef Name="PublishedDate"/>
  <FieldRef Name="PostCategory"/>
  <FieldRef Name="Written_x0020_By"/>
</ViewFields>

Then I saved the file. Yes, that customizes (or unghosts) the file. I’m OK with that.

Back to the BlogWrittenBy.xsl file I saved above. I found the section of the XSL where Created By (@Author) is emitted:

<xsl:when test="@Name='Author'"><span class="ms-postfootercolor"><xsl:value-of select="'by '"/></span><xsl:text disable-output-escaping="yes" ddwrt:nbsp-preserve="yes">&amp;nbsp;</xsl:text><xsl:value-of select="$thisNode/@Author.span" disable-output-escaping="yes" /></xsl:when>

and replaced it with this:

<xsl:when test="@Name='Author'">
  <xsl:variable name="ShowPerson">
    <xsl:choose>
      <xsl:when test="string-length($thisNode/@Written_x0020_By.id) &gt; 0">
        <xsl:value-of select="$thisNode/@Written_x0020_By.span" disable-output-escaping="yes"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$thisNode/@Author.span" disable-output-escaping="yes"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <span class="ms-postfootercolor"><xsl:value-of select="$thisNode/../@resource.wss.ByPrefix"/></span><xsl:text disable-output-escaping="yes" ddwrt:nbsp-preserve="yes">&amp;nbsp;</xsl:text><xsl:value-of select="$ShowPerson" disable-output-escaping="yes" />
</xsl:when>

This added the conditional logic we wanted.

Next, off to the browser, where I opened the page for editing. In the Tool Pane for the List View Web Part which shows the blog posts, I went to the Miscellaneous section and added a link to my XSL file in the Xsl Link field. In my case, since it was stored in the root of my Site Collection, the link was /XSL/BlogWrittenBy.xsl.

Then I saved the change and voila! Exactly what I wanted.

There are other ways to accomplish what I did here with a deployable feature and managed code, etc. In this case, it was a small farm with only a production environment and a single blog where we wanted to make the change. All you enterprise types just gasped, but this is the reality in many organizations.

Obviously it wasn’t all as simple as what I outline above. Here are some of the things I found:

  • If I edited the XSL in SharePoint Designer (after the Customize Entire View step), my changes worked great in SharePoint Designer,. However, when I went to the browser, I saw the page the same way it had looked before. I believe this is because the blog.xsl file was pulled in anyway, and no amount of jiggling things could get me past that.
  • If I tried to add the XSLLink in SharePoint Designer, which ought to work, it always stripped off the leading / in the link to the XSL file. This meant that the link was invalid and blog.xsl was used again.
  • The sequence I ran through above seems to be the only reliable way to make this work. It’s pretty quick (after 47 tries) and I did it twice as I was writing this. Of course, I had a copy of the original default.aspx file to work with. Never edit the default files without making a copy.

Here are two threads from the MSDN Forums that helped a bit. I didn’t have any luck with the ddwrt:ghost="hide" approach.

http://social.technet.microsoft.com/Forums/en-ZA/sharepoint2010customization/thread/669d7d94-45ad-4fa0-8fe9-069d44ba07fa

http://social.technet.microsoft.com/Forums/en-ZA/sharepoint2010customization/thread/9e5862b2-5271-47b8-8c9c-7742b7af55eb

Similar Posts

45 Comments

  1. Marc,

    This is, by far, the biggest life saver of a post for me yet. There are indeed monsters. I used this technique successfully, but I have an issue I can’t seem to figure out (after easily 20 hours of Bing research) and if you have any ideas or a better direction to look in, I’d really appreciate it. I get ‘cannot display this webpart’ errors w/corr. ids upon first load of the posts. Refreshing generally renders the webpart correctly, though for some it takes a few times. I have this problem no where else on the farm but on these customized blog templates. Any ideas at all?

    A huge fan,
    Kevin Lowe

    1. Kevin:

      I’m glad to have contributed to saving your life.

      I can’t recall the behavior you describe. I’m wondering if it might be some sort of auth issue. Make sure that the XSL file(s) are checked in/approved/read to all.

      M.

  2. Marc,

    This has been extremely helpful, thanks.

    Do you have any tips for forcing the Written By field to be displayed within the actual post?
    Meaning, while I’m seeing the Written By field in blog/default.aspx, can’t get the populated Written By field to be displayed in blog/Lists/Posts/Post.aspx?ID=XX.

    Your thoughts are much appreciated.

    1. LowlyZoomer:

      As I remembers, the steps I show here did just that. Are you still seeing the Created By value instead? If so, your changes may not have “taken”. Add something obvious into your custom XSL and see if it shows up in the page. I usually add something like xxxx.

      M.

  3. Hello, I’ve lost three days trying to link to a new XSL, but whenever I edit any other section of the page and I save again de document .aspx, the XSL template is replaced y do not use the mine created in Documents.

    I have followed the following steps:
    1. Edit in Advanced Mode, page Default.aspx,
    2. Open the file Blog.xsl and save as in documents library with my changes.
    3. In edit webparts set the url of my new file .XSL in link XSL.
    4. Save the documents.

    Can you tell me where I am failing?

    1. It may be that you are saving the default.aspx page after you have hit the Customize XSLT button on the ribbon, which would embed the XSL in the page.

      Try following the steps exactly.

      M.

  4. Hello Marc,

    I have a requirement like need to change whole blog site look and feel. Need to change the image which comes with date, and also need to display the text, which should start from under the image. Like given below

    Don XXXXXXXXXXXXX ome
    SEPTEMBER 17, 2012 BY XXX LEAVE A COMMENT
    Every year, Different site provisioning methods in SharePoint 2010 … Since it’s not supported to change the onet.xml file of site definitions after it has been…….

    Here the date present with image.

    I need to achieve this without changing the blog.xsl in 14 hive. Is there any alternative method to achieve this functionality.

    Could you please help on this asap.

    Thanks in advance!!!!

  5. Hi Marc,

    Have a requirement like need to change the display of Comment Web part of Blog Site. Could you please help on this.

    I am waiting for your replay!!!!

  6. Marc,
    Thanks for posting this.

    I have a request to make the comments more visible in the blog home page (not the individual blog itself) – is there a way to modify this in SPD? By ‘more visible’ possibly just displaying this at the top of the blog post or even just a little larger so it is stands out and users don’t miss it.

    1. kesmit3:

      “Larger” could just be a CSS thing. If you want to move the comments from the bottom to the top, I’d suggest that would be a bad thing, as the post itself would be hidden. People should be used to the blog format these days, right? It’s a WEb Part page, so you can add instructions or whatever would work.

      M.

  7. is there a way to get sharepoint to display “by Admin” instead of “by user’s name” for internal or specific users?

    1. Stuff like this is always possible. You could add some simple script to the page to replace the actual name with whatever you choose or you could have one administrative account that everyone uses to post to the blog, or…

      In other words, lots of options.

      M.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.