views:

193

answers:

1

I have a custom page with the following ListFieldIterator:

<SharePoint:ListFieldIterator runat="server" ControlMode="Display" OnInit="listFieldIterator_Init" />

Here is the Init event:

    protected void listFieldIterator_Init(object sender, EventArgs e)
    {
        ListFieldIterator listFieldIterator = sender as ListFieldIterator;
        SPContext current = SPContext.Current;
        SPFieldUrlValue value = new SPFieldUrlValue(current.ListItem[SPBuiltInFieldId.URL].ToString());
        Uri uri = new Uri(value.Url);
        using (SPWeb web = current.Site.OpenWeb(uri.AbsolutePath))
        {
            SPListItem item = web.GetListItem(uri.PathAndQuery);
            if (null != item)
            {
                listFieldIterator.ItemContext = SPContext.GetContext(this.Context, item.ID, item.ParentList.ID, web);
            }
        }
    }

Everything works great if the target List Item is in the same site as the page. But once it points to a different site, all the fields appear, but the values all appear in the following format:

Failed to render "Title" column because of an error in the "Single line of text" field type control. See details in log. Exception message: List does not exist The page you selected contains a list that does not exist. It may have been deleted by another user..

If I change the ControlMode to Edit, the values are displayed correctly. So how do I get it to work while in Display mode?

+1  A: 

I did some more research here, here, and here. The last two led me to create the following custom control which properly renders the ListFieldIterator in Display mode:

public class RecursiveListFieldIterator : ListFieldIterator
{
    protected override void Render(System.Web.UI.HtmlTextWriter output)
    {
        SPWeb web = SPControl.GetContextWeb(this.Context);
        SPControl.SetContextWeb(this.Context, this.ItemContext.Web);
        base.Render(output);
        SPControl.SetContextWeb(this.Context, web);
    }
}
Rich Bennema