Web Slice Control adapter for MOSS 2007
This post is about enabling web slices on Microsoft Office SharePoint Server 2007.
The following example will demonstrate how to easily implement a simple control adapter which will render the required html code for turning any web part on your server into a live web slice.
First thing I did was creating a class called WebSliceAdapter which inherits from ControlAdpater.
1: public class WebSliceAdapter : ControlAdapter
ControlAdapter has a single method needs to be overridden:
1: protected override void Render(HtmlTextWriter writer)
2: { 3: var webpart = Control as WebPart;
4: if (webpart != null)
5: { 6: if (Page.Request.QueryString["sliceitem"] != null && Page.Request.QueryString["sliceitem"].Equals(webpart.ID))
7: { 8: Page.Response.Clear();
9: renderWebSlice(webpart, writer);
10: Page.Response.End();
11: }
12: else
13: { 14: renderWebSlice(webpart, writer);
15: }
16: }
17: else
18: { 19: base.Render(writer);
20: }
21: }
(I’m clearing the response and ending it because the webslice is not functioning well for some reason when the html it too large or because of the default js & css of MOSS. I’m still trying to figure out why and how to overcome this)
The following methods are rendering the required HTML code for the webslice surrounding the web part itself:
1: private void renderWebSlice(WebPart webpart, HtmlTextWriter writer)
2: { 3: writer.Write(getWebSliceHeader(webpart.ID, webpart.Title, 15));
4: base.Render(writer);
5: writer.Write("</div></div>"); 6: }
1: private string getWebSliceHeader(string id,string title,int refreshRate)
2: { 3: return String.Format(
4: @"<div class='hslice' id='hsliceitem{0}'> 5: <a rel='feedurl' href='?sliceitem={0}'></a> 6: <span style='visibility:hidden;display:none;' class='ttl'>{2}</span> 7: <span class='entry-title' style='visibility:hidden;display:none;'>{1}</span> 8: <div class='entry-content'> ",
9: id, title, refreshRate.ToString());
10: }
getWebSliceHeader gets 3 parameters: id of the webslice, the title to display in the favorites bar and the refreshRate which configures the time in minutes for the webslice to be updated. Every time the content of the webslice will be changed the webslice will appear as bold in the favorites bar indicating the user an update is available.
after signing the assembly and placing it into the GAC all is needed in order to make it work is a small change in the compat.browser file located under App_Browsers folder within the web application:
1: <browser refID="Default">
2: <controlAdapters>
3: <adapter controlType="Microsoft.SharePoint.WebPartPages.ContentEditorWebPart"
4: adapterType="Netwise.MOSS.Extensions.ControlAdapters.WebSliceAdapter, Netwise.MOSS.Extensions.ControlAdapters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4d08bf77f3d60265" />
5: <adapter controlType="Microsoft.SharePoint.Publishing.WebControls.SummaryLinkWebPart,Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
6: adapterType="Netwise.MOSS.Extensions.ControlAdapters.WebSliceAdapter, Netwise.MOSS.Extensions.ControlAdapters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4d08bf77f3d60265" />
7: <adapter controlType="Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart,Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
8: adapterType="Netwise.MOSS.Extensions.ControlAdapters.WebSliceAdapter, Netwise.MOSS.Extensions.ControlAdapters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4d08bf77f3d60265" />
9: </controlAdapters>
10: </browser>
Make sure to use full assembly names for every web part you wish to enable the adapter for. In this example I’m using the ContetEditorWebPart, SummaryLinkWebPart and the ContentByQueryWebPart.
* In this example I’m enabling the adapter for all the browsers but you can / should use IE8 definition only.
** Webslices use a different http header when they update so you might need to add a separate definition as well.
This screen shot is from my MOSS homepage after enabling the adapter. This page contains 1 content query web part for showing me my tasks, a news list and a content editor web part. As you can see I now have 3 webslice feeds which I can see from the top bar and on the page itself:

![clip_image002[4] clip_image002[4]](http://blogs.microsoft.co.il/blogs/dorong/clip_image0024_thumb_3903C90E.jpg)
Adding the content editor web part:
![clip_image002[6] clip_image002[6]](http://blogs.microsoft.co.il/blogs/dorong/clip_image0026_thumb_0A2CFEC9.jpg)
The webslices on the favorites bar:
![clip_image002[8] clip_image002[8]](http://blogs.microsoft.co.il/blogs/dorong/clip_image0028_thumb_6249D53A.jpg)
Viewing the content of the webslice:
![clip_image002[10] clip_image002[10]](http://blogs.microsoft.co.il/blogs/dorong/clip_image00210_thumb_2E058627.jpg)