0

enter image description here Clicking this button in the list's ribbon, I can properly embed HTML so SharePoint doesn't strip anything out.

I have some HTML that I need to insert into a list item field via C# code. How do I simulate the Embed Code ribbon functionality via C#?

So imagine i'm doing something:

listItem["Body"] = "HTML String";

How do I "Embed" "HTML string" before I insert it?

UPDATE

My apologies, I failed to mention that listItem["Body"] is the body field of a discussion board. Users should still be allowed to manually contribute to the discussion outside of my C# entries.

0

You can use a calculated column to achieve this. Create a calculated column and set return type as number. Then, simply change the calculated column formula from C#.

Update
A similar way to do this would be using JSLink. You can include the below script in a JS file and place it in your VS solution (typically in your _layouts mapped folder). You wouldn't be able to dynamically change the HTML string though.

(function () {
// Using the fields override leaves the rest of the rendering intact but allows control over one or more fields in the view   
var overrideCtx = {};
overrideCtx.Templates = {};
overrideCtx.Templates.Fields = {
'Body':{'View':EmbedHTML}
};
//Register the template override
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();
function EmbedHTML(){  
var retVal = ctx.CurrentItem.Body; //to retrieve the field
var HTMLAppendedString;
if(ctx.CurrentItem.ID == ID) //id of item to change
{
   HTMLAppendedString = "<b>"+retVal+"</b>";  //Custom HTML goes here. 
}
else
{
   HTMLAppendedString = retVal;
}
return HTMLAppendedString;
}

You can call this using
spfield.JSLink="location_of_JS_file"
spfield.Update();

Hope this helps you.

  • You are referering to an old (may I say ancient) SharePoint 2007 solution which, since 2010, has been surpassed by viewmaster365.com/#/How – Danny '365CSI' Engelman Jul 29 '15 at 8:23
  • @DannyEngelman, Thanks for notifying. Didn't know about this. Will modify my answer. – Akhoy Jul 29 '15 at 9:04
  • @uberz91 I have never worked with a calculated column outside of, of course calculating a numeric value. Can you elaborate on "change the calculated formula from C#"? And how will that accommodate an HTML string? – KidBatman Jul 29 '15 at 16:28
  • Since SP 2010, calculated column can render HTML in its formula. Since you're using SSOM, you can get the calculated field using SPFieldCalculated and modify the formula to include HTML. Refer : devdotnotes.wordpress.com/2012/01/29/… and c-sharpcorner.com/Blogs/10545/… – Akhoy Jul 29 '15 at 17:36
  • @uberz91 Ah I see what is going on now. However, I failed to mention (and will update my original post) that the listItem["Body"] that i'm inserting into is the Body field of a discussion board, so users should still be allowed to enter replies and create discussions ad hoc. I'm not sure if your suggestion can still be used in this case. – KidBatman Jul 29 '15 at 18:25

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.