Tuesday, September 9, 2008

Custom Application Page for ListID issue

Scenario:
You want to provide a link to a particular System Application Page, but all the application pages accepts parameter in form of some ID which is not unique in different environments.

Solution:
Custom Application page that accepts the ListName and page you want to get redirected to. The new custom page will internally gets the correct ListID and then will re-direct to the correct Application page.

Redirect.aspx(12\Template\Layouts\Client\):

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" Inherits="Microsoft.SharePoint.WebControls.LayoutsPageBase" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<script runat="server">
protected override void OnLoad(EventArgs e)
{
SPWeb site = this.Web;

Guid listID = default(System.Guid);

string listName = Request.QueryString["List"];
string pageName = Request.QueryString["Page"];

if (listName != null && pageName != null)
{
foreach (SPList list in site.Lists)
{
if (list.Title.ToLower() == listName.ToLower())
{
listID = list.ID;
break;
}
}

Response.Redirect(this.Web.Url + @"/_layouts/" + pageName + ".aspx?List=" + listID);
}
}
</script>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
Redirection Page - Please pass the proper Request Query Parameters to get the redirection to work.</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">Redirection Page</asp:Content>

<asp:Content ID="PageTitleInTitleArea" runat="server" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea">Redirection Page</asp:Content>


Usage:
Now you can call this Redirect page as
http://localhost/_layouts/client/redirect.aspx?List=Tasks&Page=listedit

and you will be redirected to the List Settings ( listedit.aspx ) of the Tasks list.

0 comments: