Add Newline in “Note” field programmatically

Hello everyone ,

Today I’ll talk about a request that I had recently:  Update a field note with a return line to each entry.

### Context presentation region ###

I had a content type on a list item with one of the fields used as a “Status” field (“Draft”, “Pending”, etc. ..). The change in status was done through a workflow.

It wass needed to log every status change and every information from the workflow (who approved / rejected a task, who approved for a task assigned to a group, etc. ..) and all that being visible from the first glance at the item.

To do it, I used a field of type “note” on the item in which I added the informations.

I first used the parameter “AppendOnly” in my field definition.

With this parameter when filling the field, you have the name of the current user, the date and the text added.

It can be usefull but in my case the Workflow data displayed were preceded by “System Account”.

I could not update the log every task change (the end of update Workflow would be generated because the element was changed), I recovered all the data from the Workflow History list on the event WorkflowCompleted. So I refrained from using the “AppendOnly.”

But then, I realized that when I update my field, my text has no return line and the visual was not goof. So I searched how to simulate a newline in a string.

### End Context presentation region ###

Being naive, I tried to put a <br/> tag in the string.

Obviously, that did not work.

I did some research and I came across an article saying that I should use <![CDATA[<br/>]]> to generate a new line. I tested it without success. It seems that it works on the Note fields with HTML (I’ll have to test this on occasion).

Looking for a little more, I found the right solution, use a System.Environment.NewLine to generate a newline!

Summary :

  •  « AppendOnly= ‘True’ » parameter :
    • On each modification, adds an entry with the format (User – Date – content).
  • Insert « System.Environment.NewLine » :
    • Add a newline in the field “note” with type “plain text.”
  • Insert <![CDATA[<br/>]]> :
    • Add a newline in the field “note” with type « Rich text » (still need to test this).

Hope this help !



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s