Logo SharePoint Thoughts   Downloads   About   Up to Bluedog Limited
Building a blog with SharePoint – the fundamentals [Post 2/3]
Posted on 9/29/2004 10:15 AM by Maurice Prather
Update 9/29:
 - Updated steps to include using a custom form for new comments instead of simply editing the default new item form for the Blog list.
 - Added info on how to get method LoadID to fire
 - Corrected method LoadID
---
 
Keep in mind one thing - building and maintaining a blog is a labor of love. You will always find something that needs tweaking.  If you don’t have the patience or desire to learn what needs to be done so that you can create and/or tweak your blog, then it’s best you stop reading now.  :)
 
The purpose of this weekend’s exercise is to help folks understand what is required to build their own blog.  There are a lot of little pieces that I won’t be able to cover, but I will try my best to cover the basics.  At the end, I’ll point you to a couple of templates that you can use as a shortcut to help you get started without having to worry about starting from scratch.
 
Both blog models have a few things in common.
  • Data is stored in custom lists.
  • Custom display is generated by the DataViewWebPart.   You will need to know a little bit about xsl to really make the DVWP come to life.
  • Users will need permissions to read all items.
  • Users should be allowed to edit only their own items.
  • You will need to create a few pages which are designed for particular tasks (i.e. view post+comments, summary view).
The first thing that may come to your mind is "Geez, the DVWP?  That means that I have to use FrontPage 2003!  Argh!"
 
Not true – If you have FP, your life will be considerably easier. The main advantage of using FP is that it allows for rapid configuration a DWVP.  However, it’s not necessary.  Everything that is presented in this series of posts can be done using nothing more than Notepad, property tool panes, and the browser ui.
 
Now let’s cover the core differences between the two models:
Model #1
  • Based on only one list – let’s call it Blog
  • Moderation is turned ON.  If you don’t do this, then everyone can post as the blog author.
  • The Blog list has at a minimum the following columns
    • Title – single line of text
    • Body – multiple lines of text
    • Type – Choice column with “Post” and “Comment” options
    • Related To – Lookup column which references the ID column in the Blog list
Model #2
  • Based on two lists – let’s call them Blog and Comments
  • Only the admin has access to create/edit posts within the Blog list
  • Everyone has create/edit rights to the Comments list
  • The Blog list has at a minimum the following columns:
    • Title – single line of text
    • Body – multiple lines of text
  • The Comments list has at a minimum the following columns:
    • Title
    • Body
    • RelatedTo – Lookup column which references the ID column in the Blog list
Once you’ve the chosen the operational model and built the required list(s), the majority of the work will be centered on building the display.  At this point in time, the DataViewWebPart will become your best friend.
 
Building and modifying views is probably a simple task for many folks.  The trick (if you were to call it that) is making the pages link and reference each other so that you have a complete user experience: view summary --> view individual post + related comments --> add comment.
 
On my site, I basically have 3 custom views on 2 different pages:
  1. Summary view with links to view individual posts (on default.aspx)
  2. Invidual view (on viewpost.aspx)
  3. Comment view, designed to show items related to the specified post (on viewpost.aspx)
You can tie your pages together by using the two methods – web part cross page connections or "simplistic" links (e.g. viewpost.aspx?id=88).  How you tie the pages together is up to you.  For my site, I chose simplistic links because I find urls generated by cross page connections to be quite ugly (and hard to maintain).
 
As you play with the DVWP and its xsl, you will find that most of your modifications will occur in the "dvt_1.body" template.  If you search for "<xsl:template name="dvt_1.body">", you will notice that this xsl template contains the markup used to display each item in your list.  FrontPage will allow you make modification to most of the display, but there are some things you will have to manually add to the xsl in order to get the desired output.
 
Let’s piece together a couple of pages so that you can see how a fully operational blog can be built.  We’ll build two pages for model #1:
  • Default.aspx: A default view page where only posts are displayed and a button with “View comments”
  • ViewPost.aspx: The page displays both posts and comments.

Instructions for Default.aspx:

  1. Create a new web part page, call it "Default.aspx"
  2. Add a list view part for the Blog list.
  3. Modify the web part...
    1. Select Edit the View, display only Title and Body
    2. Set Toolbar Type to No Toolbar
  4. Open FrontPage, convert the web part to a DVWP.
  5. Adjust the display to your liking.  This provides a basic structure.  Note that your view is currently displaying both posts and comments.
Now add a display time filter to limit the view to only posts ...
  1. Again, in FP and viewing the source, search for "<xsl:template name="dvt_1.body">"
  2. Find the for-each block which is used to iterate through all items in your list (look for "<xsl:for-each select="$Rows">").
  3. Add a display time filter to limit the view to only posts by wrapping the entire contents of the for-each section in an "if" block.  Thus, add the filtered view would look like...

<xsl:for-each select="$Rows">
  <xsl:if test="@Type='Post'">
    … original content …
  </xsl:if>
</xsl:for-each>

The next thing to do is to add a link to your view page.  Let’s say we wanted to create a link that looks like...

View comments (0)

  1. Search "<xsl:value-of disable-output-escaping="yes" select="@Body"/>".
  2. Insert the following text immediately after the <xsl:value-of> tag:

<div>
  <a style="text-decoration: none;"><xsl:attribute name="href">ViewPost.aspx?ID=<xsl:value-of select="@ID"/></xsl:attribute>View comments (<xsl:variable name="commentCount" select="@ID"/><xsl:value-of select="count(//@RelatedTo[.=$commentCount])";/>)</a>
</div>

The first page is complete.  It now displays a filtered view of the Blog list in which only Posts are shown, a comments counter is available, and a link to ViewPost.aspx is included. 
 
Now, let’s build a page which will display a single post and all of the associated comments.  For simplicity sake, these instructions will simply call out the specialized steps.
 
Instructions for ViewPost.aspx:
  1. Create a page called ViewPost.aspx
  2. Add a DataViewWebPart for the Blog, filtered to show only Posts.  Let’s call this web part "Posts".
  3. Add a DataViewWebPart for the Blog, filtered to show only Comments.  Let’s call this web part "Comments".
  4. Create a WebPart connection between "Posts" and "Comments".  Starting from the "Posts" web part...
    • Provide Data Values to "Comments"
    • Target action = Filter View Using Data Values From "Posts" column ID to "Comments" column RelatedTo.
  5. In the "Posts" web part, supply an additional filterParam binding so that the web part will be able to filter the display based on the query string parameter "?ID=x".  You will need to follow the directions posted at http://support.microsoft.com?id=831093.  The parameter name for this example is "ID"; thus, the complete string will be "QueryString(ID)".

Now let's add a "Post a New Comment" button in the "Posts" web part.

  1. View the xsl data, find the area where the Body is pumped out (search for "<xsl:value-of disable-output-escaping="yes" select="@Body"/>")
  2. Insert the following markup after the Body section (note: the display format is not really important, but the onclick handler GetID is crucial)...

<table border="0" width="140" cellspacing="0" height="8" cellpadding="0" >
  <tr>
    <td class="ms-navframe" bordercolor="#798073" style="border-style: solid; border-width: 1px; background-image:url('/_layouts/images/partgrad.gif')">
    <p align="center">
    <span style="cursor:hand" onclick="GetID()">
    <font style="font-size: 0.68em">Post a New Comment</font></span>
    </td>
  </tr>
</table>

Now add the GetID javascript method to the page source.  This is the onclick event handler that will redirect the user to the appropriate new item form for the Blogs list. 

  1. View the page source. 
  2. In the <head> of the page, add the definition for the "GetID" javascript function.     

function GetID () {
 var regex = new RegExp("(?:(\\?|\\&)id=|FilterValue1%3d)", "gi");
  var logID = document.location.href.split(regex);
  var pageURL = "Lists/Blog/NewForm.aspx";
  var returnURL = "ViewPost.aspx?id=" + logID[1];
  window.location = pageURL + "?Source=" + returnURL + "&LogID=" + logID[1];
}

Finally, copy "/Lists/Blog/NewForm.aspx" to "/NewComment.aspx".  Open the new file and add a javascript method which will associate IDs taken from the query string with requisite list column so that when comments are created the RelatedTo field is automatically populated.  Four modifications are needed in newcomment.aspx:
  • First, add the following script toward the top of the page (e.g. in the <head>)...

function LoadID () {
  var RegX = new RegExp("LogID=", "gi");
  var Log_ID = document.location.href.split(RegX);
  document.forms[0]["urn:schemas-microsoft-com:office:office#RelatedTo"].value=Log_ID[1];
  document.forms[0]["urn:schemas-microsoft-com:office:office#Type"].value="Comment"
}

  • Add input tags after the <form> tag:

<input type="hidden" name="urn:schemas-microsoft-com:office:office#RelatedTo" value=""/>
<input type="hidden" name="urn:schemas-microsoft-com:office:office#Type" value=""/>

  • then toward the bottom of the page markup (before the closing <form> tag), add the following:

<img border="0" alt="" onload=LoadID() src="/images/spacer.gif" width="1" height="1">

(Note: the src should point to an image on your system - in the templates listed in post 3/3, spacer.gif is a 1x1 transparent image)

  • finally, update the input display.  This is done by right-clicking on the ListFormWebPart and selecting "Customize SharePoint List Form".  Highlight and delete the two table rows which contain the RelatedTo and Type dropdowns.  This is done so that users will not have to mess with those fields.
You've now built three custom pages to enable the desired user experience described at the beginning of the article (user experience: view summary --> view individual post+comments --> add comment).
 
The process for model #2 is basically the same - the only difference is that you will be working with 2 lists and therefore your xsl code will need to change to account for differences in the list structures.
 
This is without a doubt a whirlwind of information…  the key thing to keep in mind is your pages need to carry some information forward as the user moves from page to page so that the end user experience is seamless.

My final article will briefly touch on how you can edit/create your DVWP outside of FP, additional blog "features", and finally include links to a template or two...
 
 
re: Building a blog – the fundamentals
Would it be possible to make a template of what you did available for Download.  I was trying to follow the instructions but i am having some problems here and there
Anonymous User @ 9/27/2004 9:14 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
Quick question for you:
 
on a model 2 system how would you modify the comment count (<xsl:value-of select="count(//@RelatedTo[.=$commentCount])";/>) so that it counts items from the comments list rather than the blog list.
 
I've been toying with the idea of incorporating native commenting rather than my existing system and this is just about the only stumbling block I've got.
 
Cheers,
Colin.
Colin Walker @ 9/28/2004 2:08 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
I knew that I'd forget to add something... :)
 
Basically, you can't.  That's one of the defining factors between model 1 and 2.  Since everything is self-contained in model 1, the DVWP is able to deliver information about both types of list entries.  In model 2, you run up against a limitation of the DVWP (and SharePoint for that matter) in which you can't effectively join two lists to arrive at one composite data set. The DVWP can only deliver information about 1 list at a time.
 
On my blog, for example, you might recall that I once had a comment counter...  that was under model 1.  Nowadays, I don't...  that's because I use model 2.
 
You may want to try a workaround where you add a hidden DVWP that pulls down info from the Comments list, then add javascript to the main Blog DVWP to insert that information where appropriate.  Kind of hacky, but doable I imagine (I haven't tried it myself, but will eventually when I get around to it).
 
Maurice Prather @ 9/28/2004 8:11 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
I had a feeling you were going to say that ;) and was pretty thinking of the same resolution. If I sort it out I'll let you know how.
Colin Walker @ 9/28/2004 8:47 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
There should have been "much" in the middle of that ;)
Colin @ 9/28/2004 11:29 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
Hi Maurice Prather,
First a lot of thanks, i was just looking for a thing like this.
i have some questions regarding the article:
1. I have a problem on click the post new comment button:
- which page should it open? the newForm or the newComment?
-in the "newComment" - should it gets automatically the post ID? i expected it will...
- somehow since the webpart pages i created saved in "shared  documents" doclib (which is the default place to locate web part pages) - i don't get to the right place.
- in the relatedTo column - shouldn't it filter only post id's? or maybe since it's hidden and should get the post ID automaticaly i don't have to worry about it?
pllooking forward to get answers, it may be very useful!
BTW - can you publish the model 2 example? thanks
 
keren sar-el @ 1/5/2005 2:38 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
Hi,
i managed to set the pages at the right place so now it almost works... the only thing is that somehow the relatedTo not get data automatically (also the loadID function)... any idea why?
 
BTW 
(i think there wase a mistake in the getID function - it should point newcomment instead of newform, isn't it?)
 
 
THANKS
keren sar-el @ 1/5/2005 3:46 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
As mentioned in the last post of this series, Model #2 is available in FrontPage 2003.  Thus, there's no reason for me to post it separately.
Maurice Prather @ 1/18/2005 3:29 PM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
Anonymous User @ 6/23/2005 2:27 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
hi,
first of all a lot of thanks...
 
i want the users to post their comments for news in portal. have you any idea how to do it?
Anonymous User @ 6/23/2005 4:19 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
Nice example
Anonymous User @ 2/23/2006 3:21 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
Sounds like a great tool / framework you have developed...
Anonymous User @ 3/14/2006 2:42 PM
Comment on Comment
How do i post comments on existing comments
Anonymous User @ 4/12/2006 4:13 PM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
Based on the models presented, commenting on comments is not really possible.  You could setup a blog based off a discussion list, which will give you the opportunity to utilize the built-in threading system.
 
-Maurice
Maurice Prather @ 4/18/2006 5:44 PM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
Maurice,
 
This serie of posts has been very useful for me.
 
I need to post pictures in the posts. Do you have any idea how can I do this?
 
Thanks in advance,
 
Leonardo
Leonardo B @ 5/15/2006 5:09 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]

Leonardo,

In v2, you cannot insert images into Rich Text fields.  In v3, you will be able to do so.

-Maurice

Maurice Prather @ 5/15/2006 9:48 PM
Blog Setup
Maurice, I simply would like to create a blog on an Intranet.  I am using Sharepoint Portal Server 2003 and I have FrontPage 2003.  I am attempting to follow your instructions on how to create a blog, but I am stuck! The part I am stuck on right now is below...
 
Instructions for Default.aspx:
Create a new web part page, call it "Default.aspx"
Add a list view part for the Blog list.
Modify the web part...
Select Edit the View, display only Title and Body
Set Toolbar Type to No Toolbar
Open FrontPage, convert the web part to a DVWP.
Adjust the display to your liking.  This provides a basic structure.  Note that your view is currently displaying both posts and comments.
How do I "add a list view part"?  Additionally, how do I save a web part as a DVMP?  I don't have that option in Frontpage when saving. 
 
I would greatly appreciate some help getting this started!  Thanks!!
 
Eric
Eric @ 5/26/2006 10:47 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
Eric,
 
Q: How do I "add a list view part"? 
A: In your web part page, click on any Web Part Zone.  This will open the pane that lists all web parts.  Find the list you wish to add to the page.  Drag and drop it.  This is your list view web part.
 
Q: Additionally, how do I save a web part as a DVMP? 
A: Once the list view web part has been added the page, right click to bring up the context menu.  Select the "convert to data view" (or similar) option.
 
Maurice Prather @ 6/1/2006 10:06 AM
Struggles
Hello Maurice, I have been struggling with your instructions... I think I just need to get started! 
 
First of all, when I create the Default.aspx page, what web part do I add?  A custom list?  If I create a custom list, I am unable to edit the view to display only Title and Body, because  Body does not show up as an option for a custom list.  (I have attempted to follow the directions using an announcement web part, but I still am unable to locate <xsl:value-of disable-output-escaping="yes" select="@Body"/>)
 
Any ideas what I'm doing wrong??
 
Thanks!!
Eric @ 6/5/2006 1:50 PM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
Hi Maurice,
 
First of all thank you very much for taking your time to share this information. The blog implementation was just what I needed, and I had no idea SharePoint could be that customizable without the need of building custom web parts. The customizations you used has given me a lot of new ideas on using SharePoint.
 
Second, I just want share here the solution to a small problem I ran into when I was implementing Model #1: I created my Blog list with a column named "Related To" ("Related"+space+"To"), and some things didn't work: the comment counter on default.aspx, and adding a new comment on the newcomment.aspx page. Browsing the HTML source for the pages, I could see that the column name was "Related_x0020_To". I just had to change the references to the column on default.aspx and on newcomment.aspx and everything worked ok.
 
Hope that helps someone.

GB
 
GB @ 10/10/2006 5:53 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
Hi All,
 
I want to create a custome web part which can fetch data from a SQL server and display.
My problem is that, the SQL server is in a different machine.
So can anybody out here can help out in this situation?
accessing SQL Database from a custom webpart. @ 8/8/2007 5:38 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
Hi,
 
I am trying to customize a list using FP 2003. I want to customize the
NewForm.aspx or other pages that comes with ths list. I just want to know if it will unghost these pages or any other issues with what I am
trying to achieve?
 
Thanks,
Sumit
Sumit @ 11/2/2007 11:40 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
Sumit,
 
Yes, your pages will be unghosted whenever you edit them (whether or not you use FP). 
 
-Maurice
 
Anonymous User @ 11/14/2007 10:52 AM
re: Building a blog with SharePoint – the fundamentals [Post 2/3]
These instructions are great!  Thanks so much for sharing them.  I do have a question though - I noticed that the Load(ID) function seems to work just fine in IE but doesn't work in Firefox or Opera.  Do you have any suggestions?  Thanks in advance for your help!
Amy @ 12/10/2007 5:36 AM
RSS feed
Microsoft Certified Master
MVP Logo
Follow me on Twitter!
Keyword Search
 
View by category
 

Disclaimer:
The contents of this site represent thoughts and opinions of the authors , not those of anyone else - such as past, present and future employers.  This a forum of the exchange of ideas centered on SharePoint technologies.  It is not a support channel.  :)

Copyright © 2004-2010 Maurice Prather, Inc. All rights reserved.