+ Start a Discussion
paul-lmipaul-lmi 

API access to Knowledge article content inline images

When pulling Knowledge sobjects from the API, is there a way to get a "working" image URL when the article has embedded images?


Right now, when I pull an article via the API, any embedded images in that article's rich text fields are referenced with relative URL's, like /servlet/rtaImage?eid=ka130000000MFK1&feoid=00N30000004XYzF&refid=0EM30000000fDZ5 . In order to get a working URL, I need the fully qualified path to that image. I "could" run a regex against the HTML content of the rich texts fields, append a Sites hostname to the image src URL, and then it'd work, but this presents a layer of complexity to the whole thing.


Is there a way to make images in rich text fields have a fully qualified URL out of the box?

Best Answer chosen by Admin (Salesforce Developers) 
paul-lmipaul-lmi

Temp solution. I don't know what I was thinking I'd need regex, as long as Salesforce doesn't change the approach they use to serving up rich text embedded images, it's a simple string.replace.

 

 

C#

/// <summary>
/// fixImages uses a regex to find any embedded salesforce servlet images with relative URLs, and replaces them with Sites URLs
/// </summary>
/// <param name="html">Pass in the HTML that represents a rich text field in Salesforce, e.g., Body__c from Documentation__kav</param>
/// <returns></returns>
private string fixImages(string html)
{
	string pattern = "<img src=\"/servlet/rtaImage";
    string newpattern = "<img src=\"https://comapnyname.secure.force.com/sitename/servlet/rtaImage";
    return html.Replace(pattern,newpattern);            
}

 

 

 

I will note though, that the Force.com Site MUST have access to the rich text field in question, which means Public Access Settings must be configured to access the Object, and Field, in question. For me, it was a custom Knowledge Article Type, and this is tested and working.

 

I went with the SSL host to ensure I could be flexible with it.  You can always use companyname.force.com instead if your implementation doesn't need SSL.

All Answers

paul-lmipaul-lmi

Temp solution. I don't know what I was thinking I'd need regex, as long as Salesforce doesn't change the approach they use to serving up rich text embedded images, it's a simple string.replace.

 

 

C#

/// <summary>
/// fixImages uses a regex to find any embedded salesforce servlet images with relative URLs, and replaces them with Sites URLs
/// </summary>
/// <param name="html">Pass in the HTML that represents a rich text field in Salesforce, e.g., Body__c from Documentation__kav</param>
/// <returns></returns>
private string fixImages(string html)
{
	string pattern = "<img src=\"/servlet/rtaImage";
    string newpattern = "<img src=\"https://comapnyname.secure.force.com/sitename/servlet/rtaImage";
    return html.Replace(pattern,newpattern);            
}

 

 

 

I will note though, that the Force.com Site MUST have access to the rich text field in question, which means Public Access Settings must be configured to access the Object, and Field, in question. For me, it was a custom Knowledge Article Type, and this is tested and working.

 

I went with the SSL host to ensure I could be flexible with it.  You can always use companyname.force.com instead if your implementation doesn't need SSL.

This was selected as the best answer
Hemant shuklaHemant shukla

Hi,

Can u tell me please ,what query you have used to fetch the Article detail or url of Embedded image..??