Blog

Programmatically create calculated field in SharePoint 2010

Posted by Vijai Anand Blogs | SharePoint 2010 Dec 04, 2012
In this blog you will see how to create calculated field in SharePoint 2010 using SharePoint Object Model.

I have a custom list named “Custom”. I need to create a calculated field to this list using SharePoint Object Model.

In this blog we will see how to create calculated field in the custom list using SharePoint Object Model.

Code Snippet:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

using System.Collections;

 

namespace CalculatedField

{

    class Program

    {

        static void Main(string[] args)

        {

            using (SPSite site = new SPSite("http://serverName/sites/Vijai/"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    SPList list = web.Lists.TryGetList("Custom");

                    if (list != null)

                    {

                        ////Create a calculated field

                        string calculatedColumn = list.Fields.Add("CalculatedColumn", SPFieldType.Calculated, false);

                        ////Get the newly created calculated field

                        SPFieldCalculated calculatedField = list.Fields[calculatedColumn] as SPFieldCalculated;

                        ////Set the calculated field formula

                        calculatedField.Formula = "=[Title]";

                        ////Set the data type returned from this formula

                        calculatedField.OutputType = SPFieldType.Text;

                        ////Update the calculated field

                        calculatedField.Update();

                    }

                }

            }

        }

    }

}



Calculated Field created successfully:



CalculatedField.png
comments
COMMENT USING
PREMIUM SPONSORS
Nevron Software is a global leader in component based data visualization technology for a diverse range of Microsoft centric platforms. Nevron Data Visualization components are used by many companies, educational and government organizations around the world.
HTML 5 + JQUERY CONTROLS
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
HTML 5 + JQUERY CONTROLS