I have a list and it has a content type called employee, i would like to render a particular column from the list and its values in a webpart. The column is a multivalue checkbox. It has some values selected.

When I try to render this control, its rendered without the actual values check, all the checkbox are defaulted/ unchecked.

Here is the code that I am using.

var ctrl = GetSharePointControls(Constants.FieldNewStarters, itemId, SPControlMode.Edit);

the called method:

public static Control GetSharePointControls(string fieldname, int itemId, SPControlMode mode){
        BaseFieldControl webControl = null;
                using (var site = new SPSite("http://kw3l1d65:45678/"))
                using (SPWeb web = site.OpenWeb())
                    SPList list = web.Lists["test1"];
                    SPField field = list.Fields.GetField(fieldname);

                    // check if the field is a buildIn field
                    // or can be rendered by a SharePoint Control
                    if (field != null && field.FieldRenderingControl != null && !field.Hidden)
                        SPField newField = list.Fields[field.Id];

                        webControl = newField.FieldRenderingControl;
                        webControl.ListId = list.ID;
                        webControl.ItemId = itemId;
                        webControl.FieldName = newField.Title;

                        if (itemId != 0)
                            var controlContext = SPContext.GetContext(HttpContext.Current,
                             list.DefaultView.ID, list.ID, list.ParentWeb);

                            //SPContext.Current.FormContext.SetFormMode(mode, true);
                            //controlContext.FormContext.SetFormMode(mode, true);
                            webControl.ItemContext = controlContext;
                            webControl.RenderContext = controlContext;

                        if (mode == SPControlMode.Display)
                            webControl.ID = Guid.NewGuid() + PrepareId(newField.Title);
                            webControl.ID = PrepareId(newField.Title);

                        webControl.ControlMode = mode;
            return webControl;
        catch (Exception ex)
            LoggingService.LogError(LogCategories.General, ex.StackTrace);

            var errorLabel = new Label()
                ID = "ErrorLabel",
                Text = string.Format("Error in GetSharePointControls:<br/>{0}", ex)
            return errorLabel;
  • You mentioned issues with RunWithElevatedPrivileges. Where is the code executed and what is "kw3l1d65:45678"? – Hitodama Apr 3 '13 at 12:34

Try this SPContext.Current.FormContext.FieldControlCollection.Add(webControl);

  • looks like SPSecurity.RunWithElevatedPrivileges(delegate is the issue. if i remove this , it works fine. – Prakash Apr 3 '13 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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