开发者

How do I point a ListFieldIterator to a subweb when ControlMode = Display?

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?


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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜