This documentation is archived and is not being maintained.

Reading ListID and ItemID from an Application Page in Windows SharePoint Services 3.0

Windows SharePoint Services 3

Visual How To

Applies to:  Microsoft Windows SharePoint Services 3.0, Microsoft Office SharePoint Server 2007, Microsoft Visual Studio 2005

Ted Pattison, Critical Path Training

May 2007


In Microsoft Windows SharePoint Services 3.0, you can create a menu item for an entry control block that redirects users to a custom application page. For example, the MSDN article Item-Level Auditing with SharePoint Server 2007 demonstrates how to provide each document within a document library with its own menu item in the entry control block. This menu item redirects the user to a custom application page that displays the audit history for that particular document.

When a user clicks a menu item in the entry control block, it runs an application page. The code behind the application page typically must program against the list item or document that supplied the menu item. To do this, the application page must be able to identify the list item or document. Windows SharePoint Services does this by passing the information that identifies the list item or document to the application page in a query string. The page uses this information within the page-initialization code to create an SPListItem object and its containing SPList object.

Code It

When you write a CustomAction entity to create a menu item for an entry control block to redirect the user to a custom application, you should use dynamic tokens to configure the Url attribute that Windows SharePoint Services replaces at run time. In particular, you should assign the value of the Url attribute within the UrlAction element in the following form.


Configure this URL to redirect the user to a custom application page named ItemAudit.aspx. You should also notice that this URL contains three tokens. Windows SharePoint Services replaces these tokens at run time. The ~site token is replaced by the URL that points to the current site. The {ListId} token is replaced by the GUID that identifies the current list or document library. The {ItemId} token is replaced by the integer identifier of the list item or document. These last two tokens are used to create a query string that can pass information that the code in ItemAudit.aspx needs to access the target list item or document and its containing list or document library.

Now let us examine the code that is required to extract the list item ID and the GUID for either the containing list or the document library from the query string. The following code uses a standard programming technique that involves the QueryString property of the ASP.NET Request object to accomplish this.

Dim siteCollection As SPSite = Me.Site
Dim site As SPWeb = Me.Web
'***  obtain query string values
Dim ListId As String = Request.QueryString("ListId")
Dim ItemId As String = Request.QueryString("ItemId")
'***  create list object and list item object
Dim list As SPList = site.Lists(New Guid(ListId))
Dim item As SPListItem = list.Items.GetItemById(Convert.ToInt32(ItemId))
'*** query for information about list and list item
Dim ListTitle As String = list.Title
Dim ItemTitle As String = item.Title

Read It

This topic shows how the code within your application page can create the SPList object and SPListItem object. At this point, it is important to determine whether the SPList object represents a standard list or a document library. If you want to determine this programmatically, you can determine whether the SPList object is also of type SPDocumentLibrary. If the SPList object can be converted to an SPDocumentLibrary object, you can program against this object by using additional methods and properties that are available only when programming against document libraries. You can also access the File property of the SPListItem, which returns an SPFile object. This gives you programmatic access to the document itself.

If TypeOf list Is SPDocumentLibrary Then
  Dim documentLibrary As SPDocumentLibrary
  documentLibrary = CType(list, SPDocumentLibrary)
  Dim DocumentTemplateUrl As String
  DocumentTemplateUrl = documentLibrary.DocumentTemplateUrl
  Dim file As SPFile = item.File
  Dim FileAuthor As String = file.Author.Name
  Dim FileSize As String = file.TotalLength.ToString("0,###") & " bits"
End If
See It

Reading List Id and Item Id From An App Page

Watch the Video

Length: 4:04 | Size: 9.71 MB | Type: WMV

Explore It