1

I need to migrate a list to SharePoint 2013 to SharePoint 2010. It has a calculated column. I have written below code but not able to find what is still missing.

 if (field.FieldTypeKind.ToString() == "Calculated")
                                {

                                    FieldCalculated calculatedField = (FieldCalculated)field;
                                    var ResultType= calculatedField.OutputType;
                                    var formula = calculatedField.Formula;


                                    string fieldSchema = "<Field Type='" + field.FieldTypeKind + "' DisplayName='" + field.Title + "'  ResultType='" + ResultType + "' Name='" + field.InternalName + "' Hidden='" + field.Hidden + "'>" + formula + "</Field>";
                                    targetcollField.AddFieldAsXml(fieldSchema, true, AddFieldOptions.AddToDefaultContentType);
                                    targetContext.Load(targetcollField);
                                    srcFieldCollections.Add(field);
                                }
  • Why convert the FieldTypeKind to a string? The field type is available as a string if you use the TypeAsString property, but you could and should always try to compare to the enum if possible (e.g., FieldTypeKind.Calculated). – Dinerdo Jul 6 '17 at 14:44
1

The code snippet mentioned by above user is correct, so just adding on to it. To create it programmatically, use it as below. Important thing to note is that you need to add the Field name in the Field ref for the columns used in the formula

if (field.FieldTypeKind.ToString() == "Calculated")
{

    FieldCalculated calculatedField = (FieldCalculated)field;
    var ResultType= calculatedField.OutputType;
    string formula = calculatedField.Formula;
    formula+="<FieldRefs><FieldRef Name='Created' /><FieldRef Name='Modified' /></FieldRefs>";//add the internal names of columns used in the formula 

    string fieldSchema = "<Field Type='" + field.FieldTypeKind + "' DisplayName='" + field.Title + "'  ResultType='" + ResultType + "' Name='" + field.InternalName + "' Hidden='" + field.Hidden + "'>" + formula + "</Field>";
    targetcollField.AddFieldAsXml(fieldSchema, true, AddFieldOptions.AddToDefaultContentType);
    targetContext.Load(targetcollField);
    srcFieldCollections.Add(field);
}

The static code would be as below:

string formula = "<Formula>=Created</Formula>"
      + "<FieldRefs>"
      + "<FieldRef Name='Created' />"      
      + "</FieldRefs>";

string schemaCalculatedField = "<Field Type='Calculated' Name='MyName' StaticName='MyName' 
   DisplayName='MyName' ResultType='Text' Required='TRUE' ReadOnly='TRUE'>" + formula + "</Field>";
Field fullNameField = demoList.Fields.AddFieldAsXml(schemaCalculatedField, true, AddFieldOptions.AddInternalNameHint);
clientContext.ExecuteQuery();
  • Just small change I made string formula ="<Formula>" + calculatedField.Formula + "</Formula>"; and now it is working. – KumarV Nov 17 '16 at 9:10
  • how would I get the column names used in the formula and add it dynamically – KumarV Nov 18 '16 at 9:11
1

Well i cannot help with exact code but following tags needs to be available in fieldSchema :

<Field   Type="Calculated"   DisplayName="Name"  
EnforceUniqueValues="FALSE"   Indexed="FALSE"  
Format="Requiredformat"   LCID="1033"   ResultType="Text"  
ReadOnly="TRUE"   ID="{85353f64-8c8f-4424-ba81-bdc07b9880d6}"  
SourceID="{b9c7c8c2-7106-4219-84f9-b3d7cd0a9760}"   StaticName="Name" 
Name="Name"   ColName="Name"   RowOrdinal="0"   Required="FALSE"  
Percentage="FALSE"   Version="3"> <Formula>=[Created]</Formula></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.