SharePoint 2007: Using the ListFormWebPart on a Publishing Page


I was attempting to re-use a ListFormWebPart in a custom webpart which would be placed on a publishing page, in this case the webpart would display the metadata properties for document library items using custom rendering templates, with the parameters for the List and Item Id’s being supplied by page querystring parameters.

The initial code for creating the ListFormWebPart in CreateChildControls was as below;

_form = new ListFormWebPart
	{
		ID = "lfwp_display",
		ChromeType = PartChromeType.None,
		ControlMode = SPControlMode.Display,
		FormType = 4,
		TemplateName = "CustomRenderingTemplate",
		ListName = list.ID.ToString("B").ToUpper(),
		ListItemId = document.ID,
		Title = "List Form",
		AllowClose = false,
		AllowConnect = false,
		AllowEdit = false,
		AllowHide = false,
		AllowMinimize = false,
		AllowZoneChange = false,
		ViewFlag = "1048576"
	};

This all worked well it seemed, until I noticed that the same ListItem was being displayed, as though the query string parameters were being ignored, until I recycled the Application Pool and refreshed the page.

I managed to fix this issue by creating a unique ID value for the webpart each time CreateChildControls was called, incidently using the novel form of ID quirky to the ListFormWebPart, i.e. g_4b507cf2_8d12_4e1a_9799_cab82a8147f0.

Changing the code as follows solved the problem;

var wpid = Guid.NewGuid();
_form = new ListFormWebPart
	{
		ID = "g_" + wpid.ToString("D").Replace("-", "_").ToLower(),
		ChromeType = PartChromeType.None,
		ControlMode = SPControlMode.Display,
		FormType = 4, 
		TemplateName = "CustomRenderingTemplate",
		ListName = list.ID.ToString("B").ToUpper(),
		ListItemId = document.ID,
		Title = "List Form",
		AllowClose = false,
		AllowConnect = false,
		AllowEdit = false,
		AllowHide = false,
		AllowMinimize = false,
		AllowZoneChange = false,
		ViewFlag = "1048576"
	};

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.