Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Setting the formula of a calculated field using the Object Model

  Asked By: Tucker    Date: Nov 24    Category: Sharepoint    Views: 4425

I'm using the object model to add a field to a form library. To set
the formula I retreive the SPField object and then assign a value to
the SchemaXml property. I keep getting the following error: "The
formula refers to a column that does not exist".

As a test I'm just trying the make this new field display a value
from another field. Here's an example of the Xml I use.

<Field Name="Status" FromBaseType="FALSE" Type="Calculated"
DisplayName="Status" Format="DateOnly" LCID="1033" ResultType="Text"
ReadOnly="TRUE"
ColName="sql_variant1"><Formula>=xd__x007b_2D1C08BF_x002d_6020_x002d_
4C06_x002d_9A32_x002d_E7F9C5ADC380_x007d_</Formula><FieldRefs><FieldR
ef
Name="xd__x007b_2D1C08BF_x002d_6020_x002d_4C06_x002d_9A32_x002d_E7F9C
5ADC380_x007d_" /></FieldRefs></Field>

I did this manually using the UI and this is the SchemaXml that I
get for that calculated field. However if I try to use this exact
Xml to set the SchemaXml property through code I get the error.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Aditiya Kapale     Answered On: Nov 24

As a work around I created the field  using the object  model and then
setting it's schema to reference the field in the infopath form.
The key is to not call the Update() on the field, if you do it
doesn't save the new value for some reason. It doesn't make sense.
See below.

<Field ReadOnly=""TRUE"" DisplayName=""ActionStatus""
Name=""ActionStatus"" Node=""/MonthlySafeActionPlan/Action/@Status""
Type=""Text"" ColName=""nvarchar10"" />

After this I would create the calculated  field using the object
model and then set  it's schema to something like this:



<Field Type=""Calculated"" DisplayName=""Status""
Format=""DateOnly"" LCID=""1033"" ResultType=""Text""
ReadOnly=""TRUE"" Name=""Status""
ColName=""sql_variant1""><Formula>=IF
(ActionStatus=""O"",""Open"",""Closed"")
</Formula><FieldRefs><FieldRef
Name=""ActionStatus"" /></FieldRefs></Field>

Do not call the update() on the field after you do this.

When you publish an infopath form  to a form library  you have the
option to create fields in the form library that will display  data
from the infopath form. If you do this the name of your field will
be an ugly system generated name. Then I wasn't able to use this
system generated name to set the formula. It's much easier to just
create the fields using the object model  and then pointing it to the
correct field in the infopath form.

 




Tagged: