Sharepoint计算的字段公式语法

时间:2017-05-31 10:12:40

标签: sharepoint calculated-field

我必须以编程方式为列表中的计算字段设置公式。 该字段必须是另外两个字段的总和。你能为我提供我必须在代码中插入的公式的语法吗?我找不到任何例子。 你能否为我提供公式语法的参考,因为我还要创建另一个计算字段,它是两个字符串字段的串联。 谢谢

1 个答案:

答案 0 :(得分:0)

计算字段公式 reference

简单地使用您的任务:

=[Column1] + [Column2]

使用服务器代码reference

创建计算字段
using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["Website_Name"])
{
    SPList oList = oWebsite.Lists["MyList"];
    SPFieldCollection collFields = oList.Fields;

    string strNewFieldName = collFields.Add("MyNewColumn", 
        SPFieldType.Calculated, false);
    SPFieldCalculated strNewField =       
        (SPFieldCalculated)collFields[strNewFieldName];
    strNewField.Formula = "=[Column1]<[Column2]";
    strNewField.Update();
}

使用CSOM代码reference1reference2创建计算字段。如下所示,您需要记住escape special characters in xml

string formula = "<Formula>=FirstName&amp; \" \" &amp;LastName&amp; \" (id: \" &amp;EmployeeID&amp; \" \"</Formula>"
      + "<FieldRefs>"
      + "<FieldRef Name='FirstName' />"
      + "<FieldRef Name='LastName' />"
      + "<FieldRef Name='EmployeeID' />"
      + "</FieldRefs>";

string schemaCalculatedField = "<Field ID='<GUID>' Type='Calculated' Name='FullName' StaticName='FullName' 
   DisplayName='Full Name' ResultType='Text' Required='TRUE' ReadOnly='TRUE'>" + formula + "</Field>";
Field fullNameField = demoList.Fields.AddFieldAsXml(schemaCalculatedField, true, AddFieldOptions.AddFieldInternalNameHint);
clientContext.ExecuteQuery();

您的实施的工作代码(评论):

string formula = "<Formula>=Nome1&amp;&quot; &quot;&amp;Cognome1&amp;&quot;(id:&quot;&amp;Campo1&amp;&quot;)&quot;</Formula>" + "<FieldRefs>" + "<FieldRef Name='Nome1'/>" + "<FieldRef Name='Cognome1'/>" + "<FieldRef Name='Campo1'/>" + "</FieldRefs>";
string schemaCalculatedField = "<Field ID='1F2ABCC0-D243-40F0-A18D-E0AEF7FE3EB6' Type='Calculated' Name='FullName' StaticName='FullName' DisplayName='Full Name' ResultType='Text' Required='TRUE' ReadOnly='TRUE'>" + formula + "</Field>"; 
Field fullNameField = context.Web.Lists.GetByTitle("PnP Custom List3").Fields.AddFieldAsXml(schemaCalculatedField, true, AddFieldOptions.AddFieldInternalNameHint);
context.ExecuteQuery();