Tuesday, October 18, 2011

Get SPContext.Current.ListItem in SharePoint layouts page

I was developing a layouts page to view a list item for anonymous users with custom styles. So I was doing a little research on how to get the reference to the current list item (SPContext.Current.ListItem) so that the field values can be easily bound using SharePoint web controls itself.

It turns out to be, if a layouts page is being referenced relative to a proper web, and the List id and Item id are passed in as parameters, then I can get the SPContext.Current.ListItem referencing to the particular item being viewed and I can make use of the SharePoint Web Controls to display the field values.

http://localweb/{web url}/_layouts/ViewItem.aspx?List={list guid}&ID={itemid}

Example: http://localweb/home/_layouts/ViewItem.aspx?List={E2E0527C-EFB1-42D4-8FFE-20A6AC364845}&ID=1


In the ViewItem.aspx, I can make use of SharePoint web controls in the layouts page to display the field values as:

<SharePoint:FieldValue FieldName="Title" runat="server" />
 
Hope this helps some one else.

Thanks
Happy Coding
Senthil S

No comments:

Post a Comment