none
Custom Field Type that inherits from SPFieldLookup RRS feed

  • Question

  • hello

    I am creatinga customfieldtypethatinherits from alookup.Wellbelowthe codeofVaireclasses:

    -Theclass that derives fromSPFieldLookup

               

    namespace CustomTypeFieldLkUpFilter.CustomControlsCTFFilter
    {
        class CustomTypeFieldLkUpFilter : SPFieldLookup
        {
            public CustomTypeFieldLkUpFilter(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { }

            public CustomTypeFieldLkUpFilter(SPFieldCollection fields, string fieldName, string displayName) : base(fields, fieldName, displayName) { }


            /// <summary>
            /// metodo che definisce come renderizza il controllo sulla form page
            /// </summary>
            public override BaseFieldControl FieldRenderingControl
            {
                [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
                get
                {

                    BaseFieldControl ctr = new CustomTypeFieldLkUpFilterControl();
                    ctr.FieldName = this.InternalName;

                    return ctr;
                }
            }

            public override object GetFieldValue(string value)
            {
                if (string.IsNullOrEmpty(value))
                    return null;
                SPFieldLookupValueCollection field = new SPFieldLookupValueCollection();
                SPFieldLookupValue newValue = new SPFieldLookupValue(1, "aaa");
                field.Add(newValue);
                newValue = new SPFieldLookupValue(2, "bbb");
                field.Add(newValue);
                return field;
            }

            public override void OnAdded(SPAddFieldOptions op)
            {
                base.OnAdded(op);
                Update();
            } 
            public override string GetValidatedString(object value)
            {
                if ((this.Required == true)
                   &&
                   ((value == null)
                    ||
                   ((String)value == "")))
                {
                    throw new SPFieldValidationException(this.Title
                        + " must have a value.");
                }

                return String.Format("{0}", value);

            }

            public override void Update()
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPList list = web.Lists["Misure"];
                            base.LookupList = list.ID.ToString();
                            base.LookupField = "Title";
                            base.AllowMultipleValues = true;
                            base.LinkToItemAllowed = ListItemMenuState.Allowed;
                            base.LinkToItem = true;
                            base.Update();
                        }
                    }
                });


            }
        }
    }

    -- the  XML file fldtypes_CustomTypeFieldLkUpFilter.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <FieldTypes>
      <FieldType>
        <Field Name="TypeName">CustomTypeFieldLkUpFilter</Field>
        <Field Name="TypeDisplayName">Custom Type Field LkUp Filter</Field>
        <Field Name="TypeShortDescription">CustomTypeFieldLkUpFilter</Field>
        <Field Name="ParentType">LookupMulti</Field>
        <Field Name="UserCreatable">TRUE</Field>
        <Field Name="FieldTypeClass">CustomTypeFieldLkUpFilter.CustomControlsCTFFilter.CustomTypeFieldLkUpFilter, $SharePoint.Project.AssemblyFullName$</Field>
        <Field Name="Sortable">TRUE</Field>
        <Field Name="Filterable">TRUE</Field>
      </FieldType>
    </FieldTypes>

    -- the user control ascx CustomTypeFieldLkUpFilterControl.ascx

        

    <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
    <%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
    <%@ Import Namespace="Microsoft.SharePoint" %> 
    <%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Control Language="C#" %>


    <SharePoint:RenderingTemplate runat="server" ID="SRBaseFiledControlforEdit">
        <Template>
            <h1>SRBaseFiledControlforEdit</h1>
            <asp:Literal runat="server" ID="ltrlFieldValue"></asp:Literal>
            <asp:TextBox ID="inputTexTBox" runat="server"></asp:TextBox>
        </Template>

    </SharePoint:RenderingTemplate>
    <SharePoint:RenderingTemplate runat="server" ID="SRBaseFiledForDisplay">
        <Template>
                <h1>SRBaseFiledForDisplay</h1>
            <asp:Literal runat="server" ID="ltrlFieldValue"></asp:Literal>
       </Template>
    </SharePoint:RenderingTemplate>

    -- la class control CustomTypeFieldLkUpFilterControl.cs

    namespace CustomTypeFieldLkUpFilter.CustomControlsCTFFilter.Controls
    {
        class CustomTypeFieldLkUpFilterControl : BaseFieldControl
        {

            protected Literal ltrlFieldValue;
            protected TextBox inputTexTBox;
            #region Metodi in override

            protected override void CreateChildControls()
            {
                base.CreateChildControls();


                if (ControlMode == SPControlMode.Display)
                {
                    InitializeView();
                    return;
                }

                if (this.Field != null && this.ControlMode != SPControlMode.Invalid)
                {
                    InitializeEdit();
                    inputTexTBox.Text = "cornuto";
                }

            }

            protected override string DefaultTemplateName
            {
                get
                {
                    if (this.ControlMode == SPControlMode.Display)
                    {
                        //visualizza nel Displayform
                        return this.DisplayTemplateName;
                    }
                    else
                    {
                        //visualizza negli altri form
                        return "SRBaseFiledControlforEdit";
                    }
                }
            }

            public override string DisplayTemplateName
            {
                get
                {
                    return "SRBaseFiledForDisplay";
                }
            }

            public override object Value
            {
                get
                {
                    EnsureChildControls();
                    SPFieldLookupValueCollection field = new SPFieldLookupValueCollection();
                    SPFieldLookupValue newValue = new SPFieldLookupValue(1, "aaa");
                    field.Add(newValue);
                    newValue = new SPFieldLookupValue(2, "bbb");
                    field.Add(newValue);
                    //ltrlFieldValue.Text = "set Interno";
                    return field.ToString(); 
                }
                set
                {
                    //SPFieldLookupValueCollection lkpValueCollection = (SPFieldLookupValueCollection)this.ItemFieldValue;
                    //extendedLookupValue.Value = lkpValueCollection.ToSemicolumnSeparatedLookupIds();
                    //extendedLookupTextBox.Text = lkpValueCollection.ToSemicolumnSeparatedLookupValues();
                    EnsureChildControls();
                    ltrlFieldValue.Text = value.ToString();
                }
            }


            public override void UpdateFieldValueInItem()
            {
                //string ppp = inputTexTBox.Text;



                base.UpdateFieldValueInItem();
            }

            #endregion


            private void InitializeView()
            {
                ltrlFieldValue = (Literal)this.TemplateContainer.FindControl("ltrlFieldValue");
                if (ltrlFieldValue == null) throw new ArgumentException("Literal ltrlFieldValue non trovata");
            }

            private void InitializeEdit()
            {
                ltrlFieldValue = (Literal)this.TemplateContainer.FindControl("ltrlFieldValue");
                if (ltrlFieldValue == null) throw new ArgumentException("Literal ltrlFieldValue non trovata");
                inputTexTBox = (TextBox)this.TemplateContainer.FindControl("inputTexTBox");
                if (inputTexTBox == null) throw new ArgumentException("TextBox inputTexTBox non trovata");
            }
        }
    }

    AS YOU CAN SEE IN THE METHOD GET THE VALUE CREATED A SPFieldLookupValueCollection

    WELL THE COLUMN DOES BRING THE VALUES CORRECTLY, BUT WHEN I GO ON THE SHOW IS ALLITEMS.APSX LIST BELOW

    HAVE SOME TIPS

    HELLO


    Friday, October 31, 2014 8:46 AM

Answers

All replies

  • Hi,

    According to your post, the custom Field Type displays the HTML source code when in the list view.

    SharePoint 2013 uses client side rendering to render the fields, you will need to enable the “Server Render” for your list in the “Edit Web Part Properties” panel.

    More information about this issue can be found in this blog:

    http://www.sharepointnadeem.com/2013/12/sharepoint-2013-upgrade-sharepoint-2010.html

    A demo about creating custom Lookup Field for SharePoint 2013 for your reference:

    https://autocompletelookupsp2013.codeplex.com/

    Feel free to reply if there any progress.

    Thanks

    Patrick Liang

    Forum Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    Patrick Liang
    TechNet Community Support

    Monday, November 3, 2014 7:46 AM
    Moderator
  • I am sorry that you have not marked as answered before, but I've been busy with work. anyway thanks
    Friday, November 7, 2014 7:58 AM
  • I am sorry that you have not marked as answered before, but I've been busy with work. anyway thanks

    Hi,

    I'm also extending the SPFieldLookup and Patricks solution is not what I'm looking for. Months ago I found a way to format also the client rendered fields. I already know abour CSR, but it requires to specify a javascript file in each view, which is not possible for personal views.

    Did you find anythind about it?


    • Proposed as answer by Emaborsa Monday, August 20, 2018 11:23 AM
    • Unproposed as answer by Emaborsa Monday, August 20, 2018 11:58 AM
    • Edited by Emaborsa Monday, August 20, 2018 11:58 AM
    Monday, August 20, 2018 7:28 AM