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 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);
        this.pnlNew.Controls.Add(ctrl);

the called method:

public static Control GetSharePointControls(string fieldname, int itemId, SPControlMode mode){
        BaseFieldControl webControl = null;
        try
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                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);
                        }
                        else
                        {
                            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;
        }
    }
share|improve this question
    
You mentioned issues with RunWithElevatedPrivileges. Where is the code executed and what is "kw3l1d65:45678";? –  SPArchaeologist Apr 3 '13 at 12:34
add comment

1 Answer

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

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

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.