Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Custom Field Type that inherits from SPFieldLookup

$
0
0

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 SEEIN THEMETHODGETTHEVALUECREATEDASPFieldLookupValueCollection

WELLTHECOLUMNDOESBRINGTHE VALUESCORRECTLY, BUT WHENI GOON THESHOWISALLITEMS.APSX LISTBELOW

HAVE SOMETIPS

HELLO



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>