0

I am trying to create number field through code with the following schemaXML. Here, I am able to create field, without default value.

cretaedlist.Fields.AddFieldAsXml(
   "<Field ID='<Guid>'
           Type='Number' 
           Name='<Name>' 
           StaticName='<Name>' 
           DisplayName='<Name>' 
           Default='0' 
           Decimals='0'
    />");

Any help would be greatly appreciated..

1

You can try that method:

public SPFieldNumber EnsureIntegerField(SPList list, string internalName, string displayName, string description, bool required, bool includeInDefaultView, int? defaultValue)
{
    SPFieldNumber field = null;

    // We first ensure the column is not already there
    if (!list.Fields.ContainsField(internalName))
        list.Fields.Add(internalName, SPFieldType.Number, required);

    field = list.Fields.GetFieldByInternalName(internalName) as SPFieldNumber;
    field.Title = displayName;
    field.Description = description;
    if (defaultValue.HasValue)
        field.DefaultValue = defaultValue.Value.ToString();
    field.DisplayFormat = SPNumberFormatTypes.NoDecimal;

    field.Update();

    if (includeInDefaultView)
    {
        SPView view = list.DefaultView;
        if (!view.ViewFields.Exists(internalName))
        {
            view.ViewFields.Add(field);
            view.Update();
        }
    }

    return field;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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