I have written a code in event receiver which validates the user entered. My code is complete but the message I want to pass comes in the sinle line. I want to display the message in multiple lines.

My code is:

if (coll.Count > 0)
    foreach (SPListItem item in coll)
        SPFieldUserValueCollection userColl = (SPFieldUserValueCollection)item[FieldNames.Man];

        var taxonomyField = item.Fields.GetFieldByInternalName(FieldNames.Dept) as TaxonomyField;
        var fieldValue = item[taxonomyField.Id] as TaxonomyFieldValue;

        string dept = fieldValue.Label;

        foreach (SPFieldUserValue val in userColl)
            itemPos = Users.FindIndex(v => v.ID == val.User.ID);
            if(itemPos > -1)
                ErrorMsg += val.User.Name + " has Department: " + dept + System.Environment.NewLine;

properties.ErrorMessage = "Please Check Users: " + ErrorMsg;
properties.Status = SPEventReceiverStatus.CancelWithError;

The error message appears like: enter image description here

I want to display message like:

Please Check Users: Riyaz Kalva has Department: Department 1 Kalpesh Vaghela has Department: Department 1

What should I do?


simple options are:

• string.Format:

string x = string.Format("first line{0}second line", Environment.NewLine);

•String concatenation:

string x = "first line" + Environment.NewLine + "second line";

You could also use \n everywhere, and replace:

string x = "first line\nsecond line\nthird line".Replace("\n",

Note that you can't make this a string constant, because the value of Environment.NewLine will only be available at execution time.

In case of string builder, you can use the follwoing,

var sb = new StringBuilder();
sb.AppendLine(); // which is equal to Append(Environment.NewLine);
return sb.ToString();

In place of assigning the string to the event receiver, you can do it in the follwoing way,

properties.ErrorMessage = "Please Check Users: " + System.Environment.NewLine + ErrorMsg;



Environment.NewLine is just going to insert the appropriate white space for a newline, but browsers generally ignore extraneous white space. If you want the browser to interpret it as a newline, you need to insert the HTML tag for a newline, or <br/>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.