Thursday, September 22, 2011

Iterating against Multiline Field

We all know how to iterate on single line of text field in SharePoint.But its a bit tricky when it comes to Multiline Text field.I think its more hassle than Lockup column.
Here is how we get the data from the Field.
SPListItemCollection itemCol = List.GetItems(query);
 SPListItem item = itemCol[i];
SPFieldMultiLineText myField =item.Fields.GetField("fieldName") as SPFieldMultiLineText;
string myString=myField.GetFieldValueAsText(item["fieldName"]);

Even though the string gets with \r\n in order to diplay on console here is how to code
string temp = item["myField"].ToString();
string[] fieldValue = temp.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
The above method helps in removing the \r and \n present.
If you are show on web .use a textbox and set the textmode property to "multiline".This will automatically display in same format present as in SharePoint List.
Ex:<asp:TextBox ID="txtField" runat="server" TextMode="MultiLine"></asp:TextBox>
txtField.Text=myField.GetFieldValueAsText(item["fieldName"]);