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

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.

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.

~site/_layouts/ItemAudit.aspx?ItemId={ItemId}&ListId={ListId}

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.

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.

Watch the Video

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