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 a custom New Form for a Link list. Depending on the view depends which columns are displayed.

 for(int i = 0; i < spView.ViewFields.Count; i++)
                {
                    string fieldName = spView.ViewFields[i];
                    if (fieldName.Contains("LinkTitle"))
                        fieldName = "Title";


                    SPField field = spList.Fields.GetFieldByInternalName(fieldName);

                    if (field == null || field.FieldRenderingControl == null || field.Hidden)
                        continue;
                    Control cntrl = GetSharePointControls(field, spList, itemId);
                    if(cntrl == null) continue;
                   //Add control to webpart.
               }

Now to create the control I use the following code.

 internal static Control GetSharePointControls(SPField field, SPList list, int itemId)
    {
        // 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;
        SPControlMode mode = SPControlMode.Invalid;

        if (itemId > 0)
            mode = SPControlMode.Edit;
        else
            mode = SPControlMode.New;


        var controlContext = SPContext.GetContext(System.Web.HttpContext.Current, itemId, list.ID,
                                                  SPContext.Current.Web);



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

Now if I add a URL to a generic list, then this code works out the field is a URL field and displays the control correctly on the page. However, when using a Link list, the URL field type is a calculated column, which doesn't end up rendering on the page. I also found it strange that webControl.Visible = false; and if you change it in code to true, it still remains false.

I have also tried converting directly to a ComputedField instead of BaseFieldControl, but I end up with the same result. Has anyone else encountered this, or can see a fix for me? I might have to add extra logic for link lists, so that the field name URL is converted to a URLField.

share|improve this question

1 Answer 1

I've solved my own question. (I seem to do that alot on here) The problem was that the URL field read in from my view had the internal name of UrlwMenu, where I actually wanted the internal name URL. Therefore I wrote a line of code that if field read in was UrlwMenu, then replace it with the Url field instead.

Also the line webControl.FieldName = field.Title should be webControl.FieldName = field.InternalName

I found that when using Title, if a column was renamed differently to the internal name some controls threw error messages of Cast Exceptions.

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.