SharePoint - get value of calculated field without manual parsing

旧城冷巷雨未停 提交于 2019-11-27 23:49:15

问题


I have a calculated field in a list with this formula:
=CID & " - " & Title

When viewing the list, it might display as: "2 - Big Meeting". When I grab the value from code like so:
myItem["CIDandTitle"]

the value comes back as: "string;#2 - BigMeeting". Is there a "correct" way in sharepoint to extract the value or should i simply split on the semicolon and pound sign?

I am using MOSS2007.


回答1:


You have to cast it to an SPCalculatedField:

SPFieldCalculated cf = (SPFieldCalculated)myItem.Fields["CIDandTitle"];
string value = cf.GetFieldValueForEdit(myItem["CIDandTitle"]);

or

string value = cf.GetFieldValueAsText(myItem["CIDandTitle"]);



回答2:


The answer given by @Nathan does not specify that you need to provide the display name of the field. It wont work with internalName. Moreover, I'm likely to use as to cast the result.

var cf = list.Fields["calculatedfieldDisplayName"] as  SPFieldCalculated;
String value = cf.GetFieldValueAsText(item["calculatedfieldDisplayName"]);


来源:https://stackoverflow.com/questions/898291/sharepoint-get-value-of-calculated-field-without-manual-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!