I have a Note Field with RichTextMode=FullHtml. We can Set the Value of this field to a string.

string aString = string.Format("<a href={0}>{1}</a>", url, name);
listItem["field"] = aString;

But when I try to add let's say a onclick=alert(this) to the string the that part of the string is not part of the tag.

string javascript = "alert(this)"
string aString = string.Format("<a href={0} onclick={1}>{2}</a>", url, javascript, bla);

The Tag should look like this.

<a href="url" onclick="alert(this)">name</a>
insted of: <a href="url">name</a>

My questions are:

  • Is it possible to add Javascript to SharePoint Fields / Notes Field?
  • Why do SharePoint "Trim" or disguard onclick or onmousedown when I add Javascript to the string that really should just be a string?

The code that reads in the text input from the form and saves it to the list will be doing a HTMLEncode operation that replaces the html tags.

This occurs 'server side' so you won't be able to change this using JavaScript.

This is because as soon as you are taking input from a user and then rendering it back to your web page you open yourself up to all sorts of nasties such as Cross Site Scripting if you are not filtering out potential bad stuff (the very reason why the HtmlEncode is used) - less of a problem in a typical SharePoint site on an Intranet than a public facing site.

  • I'm not trying to change the value with Javascrpt. Server-side. I'm not creating a listitem from the form. Im just trying to change the value of a field by using c#. – George Norberg May 22 '14 at 11:02
  • 1
    This value is changed by sharepoint on server side. While saving this value its value in DB, Sharepoint encodes that value – Aanchal May 22 '14 at 11:03
  • Okey, thanks for the answer. I came up with an idea to create a JS file with the functionallity I need and enter a class or somthing so i can easely find it and use JS-Link on the list/Webpart that displays the list to get the functionallity. Does this seem doable? – George Norberg May 22 '14 at 11:08
  • 1
    Yes, You can also check stackoverflow.com/questions/1611968/… for other options. – Aanchal May 22 '14 at 11:10

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.