0

I am new to SharePoint and C#. Here is my code to create a text type column in my list :-

SPFieldText fldName = (SPFieldText)lst.Fields.CreateNewField(
                                    SPFieldType.Text.ToString(), txtColumnName.Text);
                                    fldName.MaxLength = 200;
                                    lst.Fields.Add(fldName);

but i don't know how to set validation and where in my code that this field cannot be empty or something like that. Any work-around?

2 Answers 2

0

You can create field from SchemaXml. For enforcing required, you can set Required="TRUE". Have a look on the following SchemaXml, it will help more.

<Field Type="Text" 
         DisplayName="FirstName" 
         Required="TRUE" 
         EnforceUniqueValues="TRUE" 
         Indexed="TRUE" 
         MaxLength="255" 
        ID="{7cccfa31-ae19-4fa7-8a87-6cff7b01979e}" 
        SourceID="{18c38424-48dd-4ac5-b32e-f3cf7b5a3042}" 
        StaticName="FirstName" 
        Name="FirstName" 
        ColName="nvarchar4" 
        RowOrdinal="0" 
        Version="1" />
0

You can set the property ValidationFormula of you Field as you would provide the Formula in the Field Settings, for example:

fldName.ValidationFormular = "="+txtColumnName.Text+"<> 'SomeText'";

(Prevents the value "SomeText" in your text column)

If only a value is required you can the the field to "Required":

fld.Required = true;

You should set the values before adding the field to the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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