<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>vf &#8211; Hamza Siddiqui</title>
	<atom:link href="https://www.mhamzas.com/blog/tag/vf/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mhamzas.com</link>
	<description>4x Salesforce MVP &#124; 26x Certified &#124; Salesforce App &#38; System Architect</description>
	<lastBuildDate>Thu, 09 Jan 2020 15:42:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
<site xmlns="com-wordpress:feed-additions:1">233526040</site>	<item>
		<title>Use Salesforce files (image file) on LWC, Aura or VF page as img tag</title>
		<link>https://www.mhamzas.com/blog/2020/01/09/use-salesforce-files-image-file-on-lwc-aura-or-vf-page-as-tag/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=use-salesforce-files-image-file-on-lwc-aura-or-vf-page-as-tag</link>
					<comments>https://www.mhamzas.com/blog/2020/01/09/use-salesforce-files-image-file-on-lwc-aura-or-vf-page-as-tag/#respond</comments>
		
		<dc:creator><![CDATA[hamza]]></dc:creator>
		<pubDate>Thu, 09 Jan 2020 14:13:57 +0000</pubDate>
				<category><![CDATA[Salesforce]]></category>
		<category><![CDATA[apex]]></category>
		<category><![CDATA[aura]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[guest]]></category>
		<category><![CDATA[vf]]></category>
		<guid isPermaLink="false">https://www.mhamzas.com/?p=3058</guid>

					<description><![CDATA[Salesforce Files had been introduced with replacement of Attachment/Document object and with some great features like version controlling, sharing, preview and what not. We used to use Attachment/Document object for file hosting like images and pdf (specially on Classic). And if we <br /><a href="https://www.mhamzas.com/blog/2020/01/09/use-salesforce-files-image-file-on-lwc-aura-or-vf-page-as-tag/" class="more-link btn btn-primary">Read More</a>]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-image"><figure class="aligncenter"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/i.stack.imgur.com/p2i0t.png?w=640&#038;ssl=1" alt="enter image description here"/></figure></div>



<p>Salesforce Files had been introduced with replacement of Attachment/Document object and with some great features like version controlling, sharing, preview and what not.</p>



<p>We used to use Attachment/Document object for file hosting like images and pdf (specially on Classic).</p>



<p>And if we needed to use that image on any VF page or AURA component, we simply use record id and below code to generate url:</p>



<pre class="wp-block-code"><code>imageURL='/servlet/servlet.FileDownload?file=&lt;document_name>';</code></pre>



<p>But with Salesforce Files object, we have to maintain 3 records in 3 different objects &#8220;ContentDocument, ContentVersion, ContentLink&#8221; and for Public Link =&#8221;ContentDistribution&#8221;.</p>



<p>If you&#8217;re going to use your custom component/vf page internally, then you can use below code:</p>



<pre class="wp-block-code"><code>imageURL='/sfc/servlet.shepherd/document/download/&lt;YOUR FILE ID>';</code></pre>



<p>But when you need it to be visible for guest users, then you&#8217;ll need to generate PUBLIC URL which you can do manually.</p>



<h3 class="wp-block-heading">Generating Public Link: </h3>



<p>  Code below will generate public url automatically. You can use this code on a trigger on Files object</p>



<pre class="wp-block-code"><code>ContentDocumentLink cdl = [select contentdocument.id, contentdocument.title, contentdocument.filetype from contentdocumentlink where linkedentityid = '&lt;Opportunity Id>'];
ContentVersion cv = [select id from contentversion where contentdocumentid = :cdl.contentdocument.id];
ContentDistribution cd = new ContentDistribution();
cd.Name = 'Test';
cd.ContentVersionId = cv.id;
cd.PreferencesAllowViewInBrowser= true;
cd.PreferencesLinkLatestVersion=true;
cd.PreferencesNotifyOnVisit=false;
cd.PreferencesPasswordRequired=false;
cd.PreferencesAllowOriginalDownload= true;
insert cd;</code></pre>



<h3 class="wp-block-heading">&lt;img&gt; tag URL Generation</h3>



<p>Now the ContentDistribution object will store the public URL but it&#8217;ll contain the preview and we cannot use it as &lt;img src&gt; tag. Therefore we need actual image URL which you can get manually by properties of image and click &#8220;Copy image address&#8221;.</p>



<p>Below public link will be generated by code</p>



<pre class="wp-block-code"><code>https://&lt;domain>.my.salesforce.com/sfc/p/#q00000000sNA/a/q00000008a0b/6woo3x9YQNR7vTXR2oOOmknAOjpytHMEnZc2nGQ3F54</code></pre>



<p>Below correct link we need to use as &lt;img&gt; tag.</p>



<pre class="wp-block-code"><code>https://&lt;domain>--c.documentforce.com/sfc/dist/version/renditionDownload?rendition=ORIGINAL_Jpg&amp;versionId=068q0000000RAdv&amp;operationContext=DELIVERY&amp;contentId=05Tq0000001RiQf&amp;page=0&amp;d=/a/q00000008a0b/6woo3x9YQNR7vTXR2oOOmknAOjpytHMEnZc2nGQ3F54&amp;oid=&lt;organization Id>&amp;dpt=null&amp;viewId=</code></pre>



<p>Use following code to convert the public url to actual url we need.</p>



<pre class="wp-block-code"><code>List&lt;Domain> domainList = &#091;SELECT Domain FROM Domain];
                String domainString = domainList&#091;0].Domain;
                String domainNameSubstring = domainString.substringBefore('.');
                String secondPart = domainNameSubstring.substringBefore('-');
                String firstPart = domainNameSubstring.substringAfter('-');
                String customDomainName = 'https://' + firstPart + '--' + secondPart + '--c.documentforce.com';</code></pre>



<p>Happy coding!</p>



<p>Sources :<br> <a href="https://developer.salesforce.com/forums/?id=9060G0000005kE0QAI">https://developer.salesforce.com/forums/?id=9060G0000005kE0QAI</a> <br> <a href="https://salesforce.stackexchange.com/questions/236980/how-to-display-image-in-a-lightning-component">https://salesforce.stackexchange.com/questions/236980/how-to-display-image-in-a-lightning-component</a> <br> <a href="https://salesforce.stackexchange.com/questions/187424/display-image-which-is-stored-in-files">https://salesforce.stackexchange.com/questions/187424/display-image-which-is-stored-in-files</a> </p>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.mhamzas.com/blog/2020/01/09/use-salesforce-files-image-file-on-lwc-aura-or-vf-page-as-tag/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3058</post-id>	</item>
	</channel>
</rss>
