<?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>Florent Clairambault &#187; BaseFieldControl</title>
	<atom:link href="http://florent.clairambault.fr/tag/basefieldcontrol/feed" rel="self" type="application/rss+xml" />
	<link>http://florent.clairambault.fr</link>
	<description></description>
	<lastBuildDate>Sat, 04 Feb 2012 19:56:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha-19719</generator>
		<item>
		<title>Sharepoint : Using BaseFieldControl</title>
		<link>http://florent.clairambault.fr/sharepoint-using-basefieldcontrol?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sharepoint-using-basefieldcontrol</link>
		<comments>http://florent.clairambault.fr/sharepoint-using-basefieldcontrol#comments</comments>
		<pubDate>Tue, 28 Jul 2009 20:45:50 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[BaseFieldControl]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Sharepoint 2007]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=1519</guid>
		<description><![CDATA[What for ? Sharepoint&#8217;s API provides some standard form controls to render each column. This is the controls used to render the standard add and edit forms. And they all inherit the BaseFieldControl class. In other word : In any SPList, you have some SPField fields and each of these SPField has the super power [...]]]></description>
			<content:encoded><![CDATA[<p><strong>What for ?</strong><br />
Sharepoint&#8217;s API provides some standard form controls to render each column. This is the controls used to render the standard add and edit forms. And they all inherit the BaseFieldControl class.</p>
<p>In other word : In any SPList, you have some SPField fields and each of these SPField has the super power to create a BaseFieldControl. Each BaseFieldControl is a CompositeControl containing ASP.Net controls.</p>
<p>For a single line field, you will just have a wrapped TextBox. But for some more advanced fields like a multi-select lookup field (SPFieldLookup) or rich text field, it can generate some &#8220;complex&#8221; controls and their related javascript code.</p>
<p>The BaseFieldControl can be directly connected to your Sharepoint&#8217;s SPListItem and the BaseFieldControl.Value will match the format required to fill the SPListItem.</p>
<p>You can create your own BaseFieldControl controls, and you can directly mess up the BaseFieldControl.Controls property if you like to (that can be useful). I personally had to create a variation of the MultipleLookupField (that&#8217;s the BaseFieldControl used for a SPLookupField with the AllowMultipleValues property enabled) to create a CheckBoxList of selected items (that what the client wanted).</p>
<p><strong>How to use them to display data ?</strong><br />
You can get it by accessing the SPField.FieldRenderingControl property. It gives you everything you need. Each BaseFieldControl has a ControlMode property (SPControlMode enum) which can be set to New, Edit, Display or Invalid (I don&#8217;t know why this third one exists).</p>
<p>Here is a stripped down version of the necessary code :</p>

<div class="wp_codebox"><table><tr id="p15196"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p1519code6"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> CreateChildControls<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
     <span style="color: #008080; font-style: italic;">// The web used</span>
     var web <span style="color: #008000;">=</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Web</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #008080; font-style: italic;">// This is the code you could have easily guessed :</span>
     var list <span style="color: #008000;">=</span> web<span style="color: #008000;">.</span><span style="color: #0000FF;">Lists</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;MyList&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
     var item <span style="color: #008000;">=</span> list<span style="color: #008000;">.</span><span style="color: #0000FF;">GetItemById</span><span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     var field <span style="color: #008000;">=</span> item<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;DateOfEvent&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
     var bfc <span style="color: #008000;">=</span> field<span style="color: #008000;">.</span><span style="color: #0000FF;">FieldRenderingControl</span><span style="color: #008000;">;</span>
&nbsp;
     <span style="color: #008080; font-style: italic;">// This is when it becomes tricky :</span>
     var renderContext <span style="color: #008000;">=</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">GetContext</span><span style="color: #008000;">&#40;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Context</span>, <span style="color: #FF0000;">0</span>, list<span style="color: #008000;">.</span><span style="color: #0000FF;">ID</span>, web <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">ListId</span> <span style="color: #008000;">=</span> list<span style="color: #008000;">.</span><span style="color: #0000FF;">ID</span><span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">FieldName</span> <span style="color: #008000;">=</span> field<span style="color: #008000;">.</span><span style="color: #0000FF;">InternalName</span><span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">ID</span> <span style="color: #008000;">=</span> field<span style="color: #008000;">.</span><span style="color: #0000FF;">InternalName</span><span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">ControlMode</span> <span style="color: #008000;">=</span> SPControlMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Edit</span><span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderContext</span> <span style="color: #008000;">=</span> renderContext<span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">ItemContext</span> <span style="color: #008000;">=</span> renderContext<span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">EnableViewState</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
     bfc<span style="color: #008000;">.</span><span style="color: #0000FF;">Visible</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
&nbsp;
     Controls<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> bfc <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And if you want to use it as an input form, you have to get a non existing SPListItem :</p>

<div class="wp_codebox"><table><tr id="p15197"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1519code7"><pre class="csharp" style="font-family:monospace;">var item <span style="color: #008000;">=</span> list<span style="color: #008000;">.</span><span style="color: #0000FF;">GetItemById</span><span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
var field <span style="color: #008000;">=</span> item<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;DateOfEvent&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>By this :</p>

<div class="wp_codebox"><table><tr id="p15198"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1519code8"><pre class="csharp" style="font-family:monospace;">var item <span style="color: #008000;">=</span> list<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
var field <span style="color: #008000;">=</span> list<span style="color: #008000;">.</span><span style="color: #0000FF;">Fields</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;DateOfEvent&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>And then later add this reflection code to force the render context to take into account the temporary created item :</p>

<div class="wp_codebox"><table><tr id="p15199"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1519code9"><pre class="csharp" style="font-family:monospace;">var fiContextItem <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span> SPContext <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetField</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;m_item&quot;</span>, BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span> <span style="color: #008000;">|</span> BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">NonPublic</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
fiContextItem<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span> renderContext, item <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong>If you use multiple SPWeb&#8230;</strong><br />
In your Sharepoint code, you might need to use more than one SPWeb. In that case, you must absolutely take care of creating the right SPContext (using the SPWeb of the SPList of the source SPField) for each BaseFieldControl used.</p>
<p>And if the SPWeb used doesn&#8217;t have the same language as your current site, you can change it easily by doing something like that :</p>

<div class="wp_codebox"><table><tr id="p151910"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p1519code10"><pre class="csharp" style="font-family:monospace;">web<span style="color: #008000;">.</span><span style="color: #0000FF;">Locale</span> <span style="color: #008000;">=</span> SPContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Web</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Locale</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong>If you create your own BaseFieldControl&#8230;</strong><br />
If you create your own BaseFieldControl, you should really take a look on the disassembled code of the default BaseFieldControls. It could make you save a lot of time and efforts. And please note that the LookupField (which directly inherits BaseFieldControl) isn&#8217;t sealed. Inheriting from it might be a good way to do your own custom lookup BaseFieldControl.</p>
<p><strong>If you enable the ControlMode = SPControlMode.Display</strong><br />
If you want to use your BFC with the Display ControlMode and want to set a value, you must take care of setting the Value BEFORE setting the ControlMode. If you don&#8217;t do that it will work on the first render but fail on the first PostBack : Your value will not be used (and displayed) by the BFC.</p>
<p><strong>If you check the value of the BFC</strong><br />
If you check the value of the BFC, you have to remember that each BFC create its value for the SPField from which it has been created. For instance, empty values can be returned as String.Empty, null or even the 0 integer.</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/sharepoint-using-basefieldcontrol/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 1/11 queries in 0.004 seconds using disk: basic
Object Caching 516/519 objects using disk: basic

Served from: florent.clairambault.fr @ 2012-02-08 12:39:22 -->
