Friday, May 30, 2008

How to use custom redirection page in SharePoint for custom navigation?

The requirement:
Create a page in SharePoint which will redirect you to specific location based on inputs.

All we need to reach at our custom page
For that you can use many ways like...
From Hyper link from quick launch.
From Edit Control Block.
From FormToolbar in List Add/Edit/Display mode and all that.
Click here to know how to do that.

What we need to do is that create a new ASPX page which will execute our custom code.

How to create a new aspx page in SharePoint with feature?
Here is a sample:
I will name this feature as “CustomRedirection”.
Here is the content of elements.xml

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name=" CustomRedirection" Path="" Url="">
<File Url=" CustomRedirection.aspx" Type="Ghostable" IgnoreIfAlreadyExists="FALSE" />
</Module>
</Elements>


The Module element works same as Module element in Onet.xml.
This element useful for using files like webpart or custom page.
So this feature includes our CustomRedirection.aspx as part of site page.
Over here we are putting “Ghostable” page (physical aspx) with these feature.

how aspx looks like?
Here is the content of CustomRedirection .aspx page

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name=" MyCustomCode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3f3cc8bff1d91557" %>
<%@ Page Language="C#" MasterPageFile="~masterurl\default.master"
Inherits=" MyCustomCode. CustomRedirection "
EnableViewState="true" EnableSessionState="True" EnableViewStateMac="false" %>

<asp:Content ID="PageTitleInTitleArea" runat="server" contentplaceholderid="PlaceHolderMain" >
Test
</asp:Content>

You can find it out that in this page what we had done is that adding Microsoft.SharePoint assembly and our custom assembly, which contain code we want to executes) then one asp:content with id “ PageTitleInTitleArea” and contentplaceholderid as “PlaceHolderMain”. This content is used by master page.

Now question is
how our custom code executes when this page accessed?
You can see page derivatives in that we are specifying MasterPageFile and we are inheriting our own class (Inherits=" MyCustomCode. CustomRedirection"). So when this page called our custom code in class CustomRedirection.cs will call.
Note: Put your custom assembly as safe control in web.config.

And once we have our execution in code, we are the king.
We can do anything we want. like…
Fetch URL and check which Item from which list is clicked, redirect to its related page like sub site, related list, look up list, do some operation like creating sub site, creating multiple records anything according to your business requirement and then redirect back to same page or anywhere you want.
Here is a part snippet from my CustomRedirection.aspx

if (Request.QueryString["FROM"] == "ECB")
{
// find List id and Item id from URL
// use that data find item from list
// do manipulation according business logic

SPUtility.Redirect(strWebURL, SPRedirectFlags.Default, httpContxt);
}


Summary
1) Create an aspx page from feature and embed in the site.
2) Use page to place content.
3) Use page as middle way for seamless navigation.
Source page -> customeredirection page -> destination page.
4) Use page as a place where you can execute your code and comes back at your place again or you can go ahead.
5) Any many more way that may be I had not discovered yet.

We are using this feature mostly in custom navigation where we know source but don’t know the destination but we need to find destination with custom business logic and then redirect it.

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com