The default MOSS search results page in a Sharepoint search center site allows the user to create an alert on a set of search results. It does not however give the user the ability to create alerts on individual items in the search results.

The following steps outline the process to add an ‘Alert Me’ link to each individual search result.

The ‘New Alert’ screen is a OOB Sharepoint Application page that takes the following as querystring parameters

  • the GUID of the list the document belongs to
  • the integer ID of the item in the list

The main steps to enable alerts on individual search results are:

  • Ensure the Sharepoint ID column is added to the SSP’s Managed Metadata properties.
  • Make sure this column is selected in the core search results web part data settings.
  • Edit the XSL to write out a hyperlink to a custom application page. The link must have the ID of the list item and the URL of the item added as querystring parameters.
  • Write a custom application page that redirects to the OOB application page located at /_layouts/SubNew.aspx and pass it the appropriate paramters in the querystring.

Add the required Managed Properties

Open the Central Administration site. Select the relevant Shared Service Provider from the left hand menu.

Once the SSP administration site is open go to:

Search Settings > Metadata Properties > New Managed Property

Map the ows_ID(integer) crawled property to a new Managed Property called ListItemID which corresponds to the ID column of each Sharepoint list.

List Item ID Managed Property

After you have created the Managed Property you must run a full crawl of the content source(s) to make sure the search index includes the new managed property.

Start a Full Crawl

Add the List Item ID to the selected columns

Now edit your search results page. Modify the Search Core Results web part by editing the Selected Columns property (in the Result Query Options property group). Add your ListItemID to the list of selected columns.

Core search results additional Selected Columns

You can now if you want check whether your ListItemID column is being successfully returned in the search results XML by following the method outlined by Andy Burns here.

Edit the XSL for the core search results web part

Find the <xsl:template match=”Result”> entry in the XSL file. Directly under this add 2 new variables

<!– Add additional variables for constructing Alert me links on individual search results –>

<xsl:variable name=”listitemid” select=”listitemid”/>

<xsl:variable name=”urlencoded” select=”urlEncoded”/>

Then add the following code where required within the <xsl:template match=”Result”> element (after the above variable declarations).

<!– Add link to custom application page which will redirect to the ‘Alert Me’ screen for this document –>

<div>

<xsl:text disable-output-escaping=”yes”>&amp;nbsp;</xsl:text>

<img src=”/_layouts/images/bell.gif” alt=”Alert Me” />

<a href=”http://myserver/_layouts/CustomSearchAlerts/myapplicationpage.aspx?ListItemID={$listitemid}&amp;itemurl={$urlencoded}”>Alert Me</a>

</div>

You should now have an ‘Alert Me’ hyperlink for each item in the search results. Now we need to create the custom application page that processes the list URL and

Create the custom application page

Create a custom application page which will be located in the 12 hive at /Template/Layouts/CustomSearchAlerts/. This page will not display anything to the user, it merely calculates the querystring parameters required by the ‘New Alert’ page and then redirects them to this page.

We can use the URL of the list item to indirectly obtain a SPList object. We can then get the GUID of the list which is what we need (along with the itemID) to pass to the ‘Create new alert’ page.

protected void Page_Load(object sender, EventArgs e)

{

int listItemID = Convert.ToInt32(Request.QueryString["listitemid"]);

string itemURL = Server.UrlDecode(Request.QueryString["itemurl"]);

// get up to the .com

int endOfHostNamePos = itemURL.IndexOf(".com") + 7;

// get the second forward slash in the URL

int endOfListPath = itemURL.IndexOf("/", endOfHostNamePos + 2);

string hostname = itemURL.Substring(0, endOfHostNamePos);

string listPath = itemURL.Substring(endOfHostNamePos + 1, endOfListPath - endOfHostNamePos - 1);

string listURL = hostname + "/" + listPath + "/";

string redirectURL;

lblMessage.Text = String.Format("ItemURL: {0} <br />ListItemID: {1} <br />Hostname: {2} <br />Library Path: {3} <br />Library URL: {4}",

itemURL,

listItemID,

hostname,

listPath,

listURL);

// create an instance of the list so we can get the lists' GUID...

using (SPSite ListSite = new SPSite(listURL))

{

// Check for null

if (ListSite == null)

{

throw new SPException("Unable to locate site.");

}

// Get the List Web

using (SPWeb ListWeb = ListSite.OpenWeb())

{

// Check for null

if (ListWeb == null)

{

throw new SPException("Unable to open web.");

}

// Get the List

SPList TheList = ListWeb.GetList(listURL);

// Check for null

if (TheList == null)

{

throw new SPException("Unable to find list.");

}

else

{

// You can now use "TheList" reference from nothing more than a URL to the List

lblMessage.Text += "<br />List found..." + TheList.ID;

redirectURL = hostname + "/_layouts/SubNew.aspx?List={" + TheList.ID + "}&ID=" + listItemID + "&Source=http://intranet.nottingahmcity.nhs.uk";

lblMessage.Text += "<br />RedirectURL: " + redirectURL;

}

} // SPWeb.Dipose();

} // SPSite.Dispose();

if (redirectURL != null && redirectURL != "")

{

Response.Redirect(redirectURL);

}

}

References

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DotNetKicks
  • Reddit
  • StumbleUpon
  • Technorati