Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm creating my own Display Form based on a view in a webpart. I'm getting the list (Document Library), and view, and then iterating through the fields, and creating a BaseFieldControl and then adding this control to the page. I'm finding that the display value always looks like the following: ["Task Name" column value]

It doesn't display any value at all! For any type of field. Just the field Name, and the words column value afterwards. Has anyone else solved this problem?

My code is below.

 internal static Control GetSharePointControlsForDisplay(SPField field, SPList list, int itemId, System.Web.HttpContext context)
    {
        // check if the field is a buildIn field, or can be rendered by a SharePoint Control
        if (field == null || field.FieldRenderingControl == null || field.Hidden) return null;

        Control ctrl = null;

        var mode = SPControlMode.Display;


        var controlContext = SPContext.GetContext(context, itemId, list.ID, list.ParentWeb);

        SPContext.Current.FormContext.SetFormMode(mode, true);
        controlContext.FormContext.SetFormMode(mode, true);


        try
        {
                BaseFieldControl webControl = field.FieldRenderingControl;
                webControl.ListId = list.ID;
                webControl.ControlMode = mode;
                webControl.ItemId = itemId;
                webControl.FieldName = field.Title;
                webControl.ID = GetControlID(field);

                webControl.RenderContext = controlContext;
                webControl.ItemContext = controlContext;

                ctrl = webControl;
            }
            return ctrl;
        }
        catch (Exception ex)
        {
            var errorLabel = new Label
            {
                ID = "ErrorLabel",
                Text = String.Format("Error in GetSharePointControls:<br/>{0}", ex)
            };
            return errorLabel;
        }
    }
share|improve this question
    
It appears to be a problem with my page, as this webpart works on other sites, and other pages. Need to find out what is different about the page I'm trying to put the webpart on. –  Cann0nF0dder May 10 '12 at 16:08

1 Answer 1

The problem appears to be with reuse of the SPWeb (wrapped in SPContext) used by ItemContext and RenderContext I had a number of different problems including the one here, they all finally went away when I created my own SPWeb every time I need one.

using (SPSite site = new SPSite(list.ParentWeb.Url))
{
    using (SPWeb web = site.OpenWeb()) 
    {
        SPContext renderContext = SPContext.GetContext(HttpContext.Current, itemId, list.ID, web);
        webControl.ItemContext = renderContext;
        webControl.RenderContext = renderContext;
        ...
    }
}

There is clearly going to be a performance penalty for creating all these SPWeb's this is not an issue for me however since my application will only be showing a few list items at a time. I wonder if there's a more economical way to clone the SPWeb in a satisfactory manner.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.