How to get the text value from Multiline text field in sharepoint using C#

此生再无相见时 提交于 2020-01-04 05:59:26

问题


I tried this code:

using (SPSite oSite = new SPSite("http://omar:2020/Lists/Calendar1/AllItems.aspx"))
{
    using (SPWeb oWeb = oSite.OpenWeb())
    {
        SPList oList = oWeb.Lists["Calendar"];
        SPListItem item = oList.GetItemById(7);

        txtArea_desc.InnerText = item["Description"].ToString();

    }
}

But it gave me "class="ExternalClassD6E6296DE90F457892C156ABE9631AC6Hello" in the TextArea.

Any suggestions please?


回答1:


Description field in Event content type has the following declaration:

<Field ID="{9da97a8a-1da5-4a77-98d3-4bc10456e700}"
    Type="Note"
    RichText="TRUE"
    Name="Comments"
    Group="_Hidden"
    DisplayName="$Resources:core,Comments;"
    Sortable="FALSE"
    SourceID="http://schemas.microsoft.com/sharepoint/v3"
    StaticName="Comments">
</Field> 

Since RichText attribute is set to true, it's value contains html content.

Use SPField.GetFieldValueAsText Method to get the field value as plain text.

Example

using (var site = new SPSite(siteUrl))
{
    using (var web = site.OpenWeb())
    {
        var list = web.Lists.TryGetList(listTitle);
        var item = list.GetItemById(itemId);
        var eventDescField = list.Fields.GetFieldByInternalName("Description");
        var eventDesc = item[eventDescField.Id];
        var eventDescText = eventDescField.GetFieldValueAsText(eventDesc);

    }
}



回答2:


Your multiline-text field must be in gross text mode in the settings, if not you will have the css class like your example



来源:https://stackoverflow.com/questions/28579695/how-to-get-the-text-value-from-multiline-text-field-in-sharepoint-using-c-sharp

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