CSOM take delimited list and store in sharepoint multiline text metadata field

gsaunders

Member
Joined
Mar 30, 2021
Messages
16
Programming Experience
5-10
Target .NET4.7.2, Windows Forms App template

Environment:
  • Windows 10
  • Visual Studio 2019 16.9.4
Goal:
  • Take a comma delimited list from a table field and then store it to a multiline text metadata field.
  • So list in field may be "Text1,Text2,Text3,Text4) and would need to be stored to the multiline text metadata field as:
    • Text1
    • Text2
    • Text3
    • Text4
  • This is just plain text. Not XML or HTML or any other formats
Code Setup:
  • We have added Microsoft.SharePoint.Online.CSOM (16.1.2111.12000) Nuget Package
  • We have added Microsoft .Identity.Client (4.29.0) Nuget Package
Questions:
  • I have searched and searched and can't find a clear answer on how to store multiple lines to a multiline text field. How can I do this? At least not through straight CSOM vs REST or other methodologies.
  • I would imagine I could convert the delimited data from a field in my database table into a list and then assign each value of the list into the mutliline metadata text field. Just not clear on how to make this happen.
  • The metadata field is just a multiline text field added via the library settings add column link.
Thanks in advance for your assistance in figuring this out.
 
Solution
Here's my suggestion: Go to the multiline text field in the SharePoint web UI form. Enter a few lines of text. Retrieve the data from the field using CSOM. Determine how the line breaks are encoded. Use the same type of encoding when you are storing your own data.
When you store a multiline field, just store the value as: "line1\r\nlin2\r\nline3\r\n". If you need to append an existing value, you'll need to first retrieve the field, then append your new text, and set the field value. It's the standard way WIndows has done things from the beginning, and SharePoint has followed suit.

Now if it was a multiline, append only field, I don't quite remember the magic to make things work. I believe that you would still do the same as above, and not have to worry about any previous text that may have been in the field -- SharePoint would take care of preserving older values as versions of the list item.
 
When you store a multiline field, just store the value as: "line1\r\nlin2\r\nline3\r\n". If you need to append an existing value, you'll need to first retrieve the field, then append your new text, and set the field value. It's the standard way WIndows has done things from the beginning, and SharePoint has followed suit.

Now if it was a multiline, append only field, I don't quite remember the magic to make things work. I believe that you would still do the same as above, and not have to worry about any previous text that may have been in the field -- SharePoint would take care of preserving older values as versions of the list item.

Thanks for feedback... was pulled of project for something else and now back on it.

I stored the field as you have it, but when you look at the actual value it is stored with the \r\n in the values and not showing as multiple lines. So must require more than just having the \r\n in between each text value.

Right now I have a value of "test1\r\ntest2\r\ntest3\r\n" and what I am seeing is this exact value on one line of the multiline field and not individual items.

Researching and testing now, but not finding a clear answer yet.
 
Here's my suggestion: Go to the multiline text field in the SharePoint web UI form. Enter a few lines of text. Retrieve the data from the field using CSOM. Determine how the line breaks are encoded. Use the same type of encoding when you are storing your own data.
 
Solution
Here's my suggestion: Go to the multiline text field in the SharePoint web UI form. Enter a few lines of text. Retrieve the data from the field using CSOM. Determine how the line breaks are encoded. Use the same type of encoding when you are storing your own data.
I did that and in the end it showed the text as "test1\ntest2\ntest3\n". So I tried that and it still shows the \n in the text.

For the sake of time I just let it be an unlimited field size and stored as semicolon delimitted. The field is still searchable if needed via the Managed Property setup.

Still would love to know how to make it work, but as I have found there are roadblocks every where. Grrr.
 
Show us your code where you are updating the text. Perhaps you are accidentally escaping the "\n" to be "\\n".
 
Show us your code where you are updating the text. Perhaps you are accidentally escaping the "\n" to be "\\n".
I am reading from a database field and that field value is "test1\ntest2\ntest3\ntest4"

C#:
foreach (DataRow row in spDataTable.Rows)
    {
    .
    .
    string recipientsp = row["RecipientSP"].ToString();
    .
    .
    }

The value in the field is "test1\ntest2\ntest3\ntest4".

Now viewing it by hovering over the variable recipientsp it displayed as "test1\\ntest2\\ntest3\\ntest4".

But I think that is just they way you see it in the debugger.

I did try adding the following code just in case, but when you viewed in debugger it still displayed as "test1\\ntest2\\ntest3\\ntest4" after the replace.
C#:
recipientsp.Replace("\\n", "\n");
 
Recall that Replace() returns a new string with the replacements. It does not do an in-place replacement because C# strings are immutable.
 
The value in the field is "test1\ntest2\ntest3\ntest4".

Now viewing it by hovering over the variable recipientsp it displayed as "test1\\ntest2\\ntest3\\ntest4".

But I think that is just they way you see it in the debugger.
And there lies your problem. A linefeed character is '\n' -- a single character. But what your database is giving you is the string "\\n" which is as you know of two characters: '\\' and 'n'. What you need to send to SharePoint is the single character, not a backslash and an 'n'.
 

Latest posts

Back
Top Bottom