<?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>matthewkenny.com &#187; .net</title>
	<atom:link href="http://www.matthewkenny.com/tag/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.matthewkenny.com</link>
	<description></description>
	<lastBuildDate>Sun, 20 Dec 2009 23:10:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Setting Sitecore ImageField</title>
		<link>http://www.matthewkenny.com/2009/12/setting-sitecore-imagefield/</link>
		<comments>http://www.matthewkenny.com/2009/12/setting-sitecore-imagefield/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 23:10:28 +0000</pubDate>
		<dc:creator>Matthew Kenny</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[sitecore]]></category>

		<guid isPermaLink="false">http://www.matthewkenny.com/?p=106</guid>
		<description><![CDATA[Recently I had to write something to allow users to upload their own content on a Sitecore site. Not an unusual, or especially difficult, piece of functionality but one thing that did give me a bit of trouble was associating uploaded images (in the media library with the Sitecore item&#8217;s image field. Originally I tried [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had to write something to allow users to upload their own content on a Sitecore site.  Not an unusual, or especially difficult, piece of functionality but one thing that did give me a bit of trouble was associating uploaded images (in the media library with the Sitecore item&#8217;s image field.<span id="more-106"></span></p>
<p>Originally I tried setting the media ID and the media path properties, but that didn&#8217;t quite work.  Eventually I found that you have to set the media ID, media path, source, and a custom attribute (&#8220;<tt>showineditor</tt>&#8220;).  I was surprised that it wasn&#8217;t easier to assign a media file to an <tt>ImageField</tt> object, but that&#8217;s easily resolved using extension methods introduced in .Net 3.5.  For the lazier among us, I&#8217;ve included a small snippet of code that does the important stuff.  This code is written for Sitecore 5.3, but should work on Sitecore 6.x with minor tweaking if some nicer methods haven&#8217;t already been put in place.  Obviously, production code should be made a bit robust than what&#8217;s below, which has no exception handling and <em>assumes the item is already in editing mode</em>.</p>
<pre name="code" class="c-sharp">
public void AssignMediaItem(ImageField field, Item mediaItem)
{
    field.MediaID = mediaItem.ID;
    MediaUrlOptions options = new MediaUrlOptions();
    field.Src = MediaManager.GetMediaUrl(mediaItem, options);
    field.MediaPath = mediaItem.Paths.MediaPath;
    field.SetAttribute("showineditor", "1");
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.matthewkenny.com/2009/12/setting-sitecore-imagefield/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
