Problem
I first came across this problem when working on a publishing site which had a news List. News items in this list should have a friendly url so they can be passed around easily.Standard SharePoint functionality allows you to access a list item with a url like:
http://server/Lists/News/YourForm.aspx?ID=1
Not that friendly, is it?
We would like to have something like:
http://server/News/the-latest-news-item.aspx
Adding to that a simple url-rewriting module, would allow such an url to be accessed without the aspx extension (http://server/News/the-latest-news-item).
That looks pretty neat, agreed?
Solution
So how do we make this happen?The idea is to use a PageLayout instead of the Display Form, and create an publishing page for every item in the list. To make sure this doesn’t require too much extra space we’ll only save the listitem ID in the publishing page and to make sure it also doens't require to much work we'll do this in an event receiver.
We need to create the:
- news list/content type
- page content type
- pagelayout
- Publishing page /event receiver (next post)
News list
So let’s start with the list. To keep things simple let’s use a list with just an ID, date, and some text.
Content type
We want to save just the page title and the news item ID. To accomplish this we create a new ContentType based on ‘page’.Just add an NewsID field and we’re done.
Page layout
Now we create a page layout based on this content type, I called mine NewsPageLayout.aspxAll we have to do is add a datasource and display the values.
Add a SharePoint datasource:
1 <SharePointWebControls:SPDataSource
2 runat="server"
3 DataSourceMode="ListItem"
4 UseInternalName="true"
5 UseServerDataFormat="true"
6 selectcommand="<View></View>"
7 id="NewsItemDataSource">
8 <SelectParameters>
9 <asp:Parameter Name="ListName" DefaultValue="News"/>
10 <CustomWebControls:FieldParameter
11 Name="ListItemID"
12 FieldName="NewsID"/>
13 </SelectParameters>
14 </SharePointWebControls:SPDataSource>
2 runat="server"
3 DataSourceMode="ListItem"
4 UseInternalName="true"
5 UseServerDataFormat="true"
6 selectcommand="<View></View>"
7 id="NewsItemDataSource">
8 <SelectParameters>
9 <asp:Parameter Name="ListName" DefaultValue="News"/>
10 <CustomWebControls:FieldParameter
11 Name="ListItemID"
12 FieldName="NewsID"/>
13 </SelectParameters>
14 </SharePointWebControls:SPDataSource>
Did you notice the FieldParameter I use?
This is a custom parameter that implements System.Web.UI.WebControls.Parameter class.
1 public class FieldParameter : System.Web.UI.WebControls.Parameter
2 {
3 public string FieldName { get; set; }
4 protected override object Evaluate
5 (HttpContext context, System.Web.UI.Control control)
6 {
7 if (SPContext.Current != null
8 && SPContext.Current.ListItem != null
9 && !string.IsNullOrEmpty(FieldName))
10 {
11 return SPContext.Current.ListItem[PropertyName];
12 }
13 return null;
14 }
15 }
16
Since I don’t really care about the design right now I just use an asp-repeater control to show the data.2 {
3 public string FieldName { get; set; }
4 protected override object Evaluate
5 (HttpContext context, System.Web.UI.Control control)
6 {
7 if (SPContext.Current != null
8 && SPContext.Current.ListItem != null
9 && !string.IsNullOrEmpty(FieldName))
10 {
11 return SPContext.Current.ListItem[PropertyName];
12 }
13 return null;
14 }
15 }
16
1 <asp:DetailsView
2 runat="server"
3 id="DetailsView1"
4 DataSourceID="NewsItemDataSource"
5 AutoGenerateRows="False">
6
7 <Fields>
8 <asp:boundfield
9 DataField="Title"
10 HeaderText="Title"
11 SortExpression="Title">
12 </asp:boundfield>
13 <asp:boundfield
14 DataField="Date"
15 HeaderText="Date"
16 SortExpression="Date">
17 </asp:boundfield>
18 <asp:boundfield
19 DataField="Text"
20 HeaderText="Text"
21 SortExpression="Text">
22 </asp:boundfield>
23 </Fields>
24
25 </asp:DetailsView>
26
Nothing fancy here. Just the basic fields.2 runat="server"
3 id="DetailsView1"
4 DataSourceID="NewsItemDataSource"
5 AutoGenerateRows="False">
6
7 <Fields>
8 <asp:boundfield
9 DataField="Title"
10 HeaderText="Title"
11 SortExpression="Title">
12 </asp:boundfield>
13 <asp:boundfield
14 DataField="Date"
15 HeaderText="Date"
16 SortExpression="Date">
17 </asp:boundfield>
18 <asp:boundfield
19 DataField="Text"
20 HeaderText="Text"
21 SortExpression="Text">
22 </asp:boundfield>
23 </Fields>
24
25 </asp:DetailsView>
26
And that’s really all there is to it.
If you want to, you can create a new site called news, so it will show in the URL.
See the result:
Easy enough, right?
In my next post I will show you the event receiver that will automatically creates and delete these pages.
Geen opmerkingen:
Een reactie posten