none
SPFieldMultiLineText values separated by comma store into an array RRS feed

  • Question

  • Hello,

    I have multilinetext field where storing data from JSON object, got store in below format, if I try to get it as text, using below code:

    SPFieldMultiLineText field = list.Fields.GetField("field") as SPFieldMultiLineText;
    string field1= field .GetFieldValueAsText(newItem["field"]);

    and getting below result: (original value : PriceList_New_12Mar2017 & 1100019269)

    "[{\"Name\":\"PriceList_New_12Mar2017\"},{\"Name\":\"1100019269\"}]"

    so how to extract (PriceList_New_12Mar2017 & 1100019269) into an array?

    Please assist.

    Thanking you in advance.

    Regardsm

    Jayashri

    Tuesday, March 28, 2017 11:19 AM

Answers

  • Thanks for your assistance.

    But above conversion did not help me.

    I did it in following way:

    SPFieldMultiLineText fieldAllCustomerNumbers = list.Fields.GetField("AllCustomerNumbers") as SPFieldMultiLineText;
    string customerNumbers = fieldAllCustomerNumbers.GetFieldValueAsText(newItem["AllCustomerNumbers"]);
    SPFieldMultiLineText fieldAllCustomerNames = list.Fields.GetField("AllCustomerNames") as SPFieldMultiLineText;
    string customerNames = fieldAllCustomerNumbers.GetFieldValueAsText(newItem["AllCustomerNames"]);
    JavaScriptSerializer dSerializerCustomer = new JavaScriptSerializer();
    MultiFieldValue[] customersListValue = dSerializerCustomer.Deserialize<MultiFieldValue[]>(customerNumbers);
    MultiFieldValue[] customerNameListValue = dSerializerCustomer.Deserialize<MultiFieldValue[]>(customerNames);
    List<string> customerslistName = new List<string>();
    List<string> customerslistNumber = new List<string>();
    if (customersListValue != null)
    {
    for (int i = 0; i < customersListValue.Length; i++)
    {
    customerslistName.Add(customersListValue[i].Name); 
    customerslistNumber.Add(customersListValue[i].Number);
    }
    }
    if (customerNameListValue != null)
    {
    for (int i = 0; i < customerNameListValue.Length; i++)
    {
    customerslistName.Add(customerNameListValue[i].Name);
    customerslistNumber.Add(customerNameListValue[i].Number);                            }
    }
    string[] arrayCustomerName = customerslistName.ToArray(); //convert into an array
    string[] arrayCustomerNumber = customerslistNumber.ToArray(); //convert into an array
    string customerName = string.Join(",", arrayCustomerName); //convert into string
    string customerNumber = string.Join(",", arrayCustomerNumber); //convert into string

    Thanks for all your help.

    Regards,

    Jayashri

    • Marked as answer by Jayashri10 Monday, May 15, 2017 4:47 AM
    Monday, May 15, 2017 4:47 AM

All replies

  • Hi,

    We can use Split method to get a string array, and add a struct in the code.

    The code snippet below for your reference:

    public struct MultipleLineText
    {
    	public string name;         
    }
    static  void Main(string[] args)
    {
    	using (SPSite site = new SPSite("http://sp13/sites/test1"))
    	{ 
    		using (SPWeb web = site.OpenWeb()) 
    		{ 
    
    			SPList list = web.Lists["CustomList19"]; 
    			SPListItem item= list.GetItemById(1);
    			SPFieldMultiLineText field = list.Fields.GetField("field") as SPFieldMultiLineText;
    			string field1= field .GetFieldValueAsText(item["field"]);
    			var array = field1.Split('&');
    			var arrayList = new List<MultipleLineText>();
    			for (var i = 0; i < array.Length;i++)
    			{
    				MultipleLineText text;
    				text.name = array[i];
    				arrayList.Add(text);                           
    			}
    
    			var jsonString = JsonConvert.SerializeObject(arrayList);                       
    		}
    	}
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, March 29, 2017 2:46 AM
    Moderator
  • Hi,

    Any update?

    If the reply help you, please mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum.

    Best regards,
    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, April 11, 2017 8:37 AM
    Moderator
  • Thanks for answer.

    Sorry for late reply.

    Could you please assist further, how to store those values into list column(Multiline )field again?

    Tuesday, April 25, 2017 6:54 AM
  • Hi,

    Modify the code like this:

    public struct MultipleLineText
    {
    	public string name;         
    }
    static  void Main(string[] args)
    {
    	using (SPSite site = new SPSite("http://sp13/sites/test1"))
    	{ 
    		using (SPWeb web = site.OpenWeb()) 
    		{ 
    
    			SPList list = web.Lists["CustomList19"]; 
    			SPListItem item= list.GetItemById(1);
    			SPFieldMultiLineText field = list.Fields.GetField("field") as SPFieldMultiLineText;
    			string field1= field .GetFieldValueAsText(item["field"]);
    			var array = field1.Split('&');
    			var arrayList = new List<MultipleLineText>();
    			for (var i = 0; i < array.Length;i++)
    			{
    				MultipleLineText text;
    				text.name = array[i];
    				arrayList.Add(text);                           
    			}
    
    			var jsonString = JsonConvert.SerializeObject(arrayList);  
    			item["field"] = jsonString;
    			item.Update();			
    		}
    	}
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, April 25, 2017 7:16 AM
    Moderator
  • Thanks for your assistance.

    But above conversion did not help me.

    I did it in following way:

    SPFieldMultiLineText fieldAllCustomerNumbers = list.Fields.GetField("AllCustomerNumbers") as SPFieldMultiLineText;
    string customerNumbers = fieldAllCustomerNumbers.GetFieldValueAsText(newItem["AllCustomerNumbers"]);
    SPFieldMultiLineText fieldAllCustomerNames = list.Fields.GetField("AllCustomerNames") as SPFieldMultiLineText;
    string customerNames = fieldAllCustomerNumbers.GetFieldValueAsText(newItem["AllCustomerNames"]);
    JavaScriptSerializer dSerializerCustomer = new JavaScriptSerializer();
    MultiFieldValue[] customersListValue = dSerializerCustomer.Deserialize<MultiFieldValue[]>(customerNumbers);
    MultiFieldValue[] customerNameListValue = dSerializerCustomer.Deserialize<MultiFieldValue[]>(customerNames);
    List<string> customerslistName = new List<string>();
    List<string> customerslistNumber = new List<string>();
    if (customersListValue != null)
    {
    for (int i = 0; i < customersListValue.Length; i++)
    {
    customerslistName.Add(customersListValue[i].Name); 
    customerslistNumber.Add(customersListValue[i].Number);
    }
    }
    if (customerNameListValue != null)
    {
    for (int i = 0; i < customerNameListValue.Length; i++)
    {
    customerslistName.Add(customerNameListValue[i].Name);
    customerslistNumber.Add(customerNameListValue[i].Number);                            }
    }
    string[] arrayCustomerName = customerslistName.ToArray(); //convert into an array
    string[] arrayCustomerNumber = customerslistNumber.ToArray(); //convert into an array
    string customerName = string.Join(",", arrayCustomerName); //convert into string
    string customerNumber = string.Join(",", arrayCustomerNumber); //convert into string

    Thanks for all your help.

    Regards,

    Jayashri

    • Marked as answer by Jayashri10 Monday, May 15, 2017 4:47 AM
    Monday, May 15, 2017 4:47 AM