OSI
OSI

Reputation: 33

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

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?

Upvotes: 2

Views: 9702

Answers (2)

user4637314
user4637314

Reputation: 1

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

Upvotes: -2

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

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);

    }
}

Upvotes: 3

Related Questions