16 Comments

If using Windows Live Writer or Microsoft Word 2007 to create blog posts in Windows SharePoint Services (WSS) 3.0 it appears, at least on our installation of SharePoint, that when you embed new images in the post, the image tags are generated with relative links, e.g. <img src="/Lists/Posts/Attachments/51/image_thumb.png"> . This is fine if you're viewing the feeds in a web browser as it can resolve the server name for the links from the feed address. But if you're using Outlook 2007 to subscribe to the feeds, any images you embedded in the post using Live Writer or Word are generated as relative links, Outlook can't/doesn't resolve the site name hence the images are broken:

image

Whose fault this is (Outlook/SharePoint/Live Writer) I don't know, but fixing it required a bit of coding hackery - I couldn't find any option in the SharePoint configuration for controlling the generation of RSS.

WSS v3.0's default RSS generator is a page called ListFeed.aspx that lives in Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\template\layouts. By default it looks like this:

<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages" %>
<%@ Page Language="C#" Inherits="Microsoft.SharePoint.ApplicationPages.ListFeed" %> 

Clearly all the logic is in Microsoft.SharePoint.ApplicationPages.dll. So to fix this, I backed up this file and added the following code to ListFeed.aspx which basically passes all of the feed content through a filter using HttpResponse.Filter which uses a regular expression to replaces any relative <img src="/blah"> tags with the absolute <img src="http://site/blah">.

<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages" %>
<%@ Page Language="C#" Inherits="Microsoft.SharePoint.ApplicationPages.ListFeed" %>
<script language="C#" runat="server">
protected override void OnInit(EventArgs e)
{
  base.Response.Filter = new RelativePathFilter(base.Response.Filter, this.Request, this.Response);
  base.OnInit(e);
}

class RelativePathFilter : System.IO.Stream { System.IO.Stream _innerStream; string _siteUrl; Regex _imagesRegex; HttpResponse _response;

public RelativePathFilter(System.IO.Stream innerStream, HttpRequest request, HttpResponse response) { _innerStream = innerStream; _siteUrl = (new Uri(request.Url, HttpRuntime.AppDomainAppVirtualPath)).AbsoluteUri; _response = response;

// Finds rooted images &lt;img ... src="/ ...
_imagesRegex = new Regex(@"(&lt;img .*? \s src \s* = \s* [""']?)/", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

}

public override void Write(byte[] buffer, int offset, int count) { string content = _response.ContentEncoding.GetString(buffer, offset, count);

// Ignore the fact that it's theoretically possible for the &lt;img&gt; tag to be split between 2 Write()s
content = _imagesRegex.Replace(content, @"$1" + _siteUrl);

byte[] newBuffer = _response.ContentEncoding.GetBytes(content);
_innerStream.Write(newBuffer, 0, newBuffer.Length);

}

public override bool CanRead { get { return _innerStream.CanRead; } } public override bool CanSeek { get { return _innerStream.CanSeek; } } public override bool CanWrite { get { return _innerStream.CanWrite; } } public override void Flush() { _innerStream.Flush(); } public override long Length { get { return _innerStream.Length; } } public override long Position { get { return _innerStream.Position; } set { _innerStream.Position = value; } } public override int Read(byte[] buffer, int offset, int count) { return _innerStream.Read(buffer, offset, count); } public override long Seek(long offset, System.IO.SeekOrigin origin) { return _innerStream.Seek(offset, origin); } public override void SetLength(long value) { _innerStream.SetLength(value); } } </script>

I'm not entirely sure whether hacking around with these files is supported (although it did seem to survive the installation of WSS 3.0 SP1), or if there's a better way of doing this, so use at your own risk.

Update 22 April 2008: Modified code so that it doesn't assume the site is at the root of a virtual server.