0

I have a field formula which is working as intended which I've set in my list settings for the respective list.

Now, my question is if it's possible to set this formula programmatically somewhere so that all lists of this type will have the formula when I create a new list? Now, I can add just add the calculated formula for each list, but I'd prefer if there was a way to set it in the code, but I'm not sure where to begin with that. Any hints appreciated!

enter image description here

  • for that purpose you can create the template of this list. And then use the same template for creating same types of calculated formula columns. Or can use the column as Site Column. – Niranjan Kulkarni Jul 13 '16 at 12:19
0

Sample code I have used:

string formulaAmigable = listaRiesgosNueva.Fields.Add("Formula", SPFieldType.Calculated, false);
SPFieldCalculated formulaAmigableCF = listaRiesgosNueva.Fields["Formula"] as SPFieldCalculated;
string formula = "=\"(\"";
formula += "&[Valor_PH_Mayor]&[Signo_PH_Mayor]";
formula += "&\" Promedio por hito \"";
formula += "&[Signo_PH_Menor]&\" \"&[Valor_PH_Menor]";
formula += "&\") AND ( \"";
formula += "&[Valor_IC_Mayor]&[Signo_IC_Mayor]";
formula += "&\" Indice criticidad \"";
formula += "&[Signo_IC_Menor]&\" \"&[Valor_IC_Menor]";
formula += "&\") => \"";
formula += "&[Valor]";
formulaAmigableCF.Formula = formula;
formulaAmigableCF.OutputType = SPFieldType.Text;
formulaAmigableCF.Update();
0

You can try this below code:

Here, I've used console app.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint; 

namespace CalculatedField
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://shashankpc:2012/sites/test"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists.TryGetList("Employees");
                    if (list != null)
                    {
                        SPFieldCalculated calculatedField = list.Fields.GetFieldByInternalName("FullName") as SPFieldCalculated;
                        calculatedField.Formula="=[LastName]&\",\"&[FirstName]";           //added formula
                        calculatedField.Update();
                    }
                }
            }
        }
    }
}

Reference:

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.