<?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>SharePoint Fun &#187; Object Model</title>
	<atom:link href="http://blog.qumsieh.ca/tag/object-model/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.qumsieh.ca</link>
	<description>Developer's blog related to ASP.NET, SharePoint and Telerik Web Controls</description>
	<lastBuildDate>Fri, 27 Jan 2012 19:08:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Invalid Data Updating List Item &#8211; FREE TSHIRT GIVEAWAY</title>
		<link>http://blog.qumsieh.ca/2009/09/15/invalid-data-updating-list-item-free-tshirt-givewaway/</link>
		<comments>http://blog.qumsieh.ca/2009/09/15/invalid-data-updating-list-item-free-tshirt-givewaway/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 20:06:26 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Object Model]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=474</guid>
		<description><![CDATA[If you&#8217;re like me and you do a fair bit of object model programming, you&#8217;ve likely seen this error before. More specifically, the error is as follows: Unhandled Exception: Microsoft.SharePoint.SPException: Invalid data has been used to update the list item. The field you are trying to update may be read only. There are probably more [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re like me and you do a fair bit of object model programming, you&#8217;ve likely seen this error before. More specifically, the error is as follows:</p>
<blockquote><p><span style="color: #ff0000;">Unhandled Exception: Microsoft.SharePoint.SPException: Invalid data has been used to update the list item. The field you are trying to update may be read only.</span></p></blockquote>
<p>There are probably more than one cause for this particular issue, but in my case, it always comes down to an error in the way I&#8217;ve assigned a field of type <strong>Lookup</strong>.</p>
<p>So to set up our scenario, let&#8217;s say we have a custom list called <strong>Types</strong>, and it contains 3 items: </p>
<ol>
<li>Employee</li>
<li>Contractor</li>
<li>Manager</li>
</ol>
<p>Now let&#8217;s assume we&#8217;ve created a custom list called <strong>Users</strong> and it contains a column called <strong>User Type</strong> of type <strong>Lookup</strong> that points to the <strong>Types</strong> list using the <strong>Title</strong> column.</p>
<p>If we&#8217;re doing some updates to our list programmatically, we&#8217;ll need to be able to set the <strong>Type</strong> column. Your typical scenario is that you&#8217;re reading this from a form control and populating this field. In my case, I knew specifically what value I wanted to set. So let&#8217;s start with something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPListItem item <span style="color: #008000;">=</span> list.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
item<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Type&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Employee&quot;</span><span style="color: #008000;">;</span>
item.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Unfortunately, the above bit of code will fail with the error outlined at the beginning of this post. The data we&#8217;re attempting to assign is not valid. What we need to do is create another object, an <strong>SPFieldLookupValue</strong> object to be more specific, and use one of the constructors to build the lookup that we&#8217;ll then assign to our field.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPListItem item <span style="color: #008000;">=</span> list.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
SPFieldLookupValue lookup <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPFieldLookupValue<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #666666;">&quot;Employee&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
item<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Type&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> lookup<span style="color: #008000;">;</span>
item.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>I&#8217;m not satisfied with the above, because it&#8217;s forcing me to hard code the id and the value. I am interested in some feedback from my readers &#8212; does anyone have a better approach to this problem? I&#8217;ll be awarding a free <a href="http://www.blackninjasoftware.com">Black Ninja</a> TSHIRT to the reader who can provide me the most elegant workaround.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2009/09/15/invalid-data-updating-list-item-free-tshirt-givewaway/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How To Change the Default EditForm, NewForm and DispForm</title>
		<link>http://blog.qumsieh.ca/2009/05/15/how-to-change-the-default-editform-newform-and-dispform/</link>
		<comments>http://blog.qumsieh.ca/2009/05/15/how-to-change-the-default-editform-newform-and-dispform/#comments</comments>
		<pubDate>Fri, 15 May 2009 22:35:51 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Object Model]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=439</guid>
		<description><![CDATA[Problem You&#8217;ve developed some custom new, edit and display application pages that are stored in the _layouts directory. Let&#8217;s assume the filenames are newform.aspx, dispform.aspx and editform.aspx. You now want to change the properties of your custom list so that any new, edit or display requests point to your custom pages. If you open up [...]]]></description>
			<content:encoded><![CDATA[<h2>Problem</h2>
<p>You&#8217;ve developed some custom new, edit and display application pages that are stored in the <strong>_layouts</strong> directory. Let&#8217;s assume the filenames are <strong>newform.aspx</strong>, <strong>dispform.aspx</strong> and <strong>editform.aspx</strong>. You now want to change the properties of your custom list so that any new, edit or display requests point to your custom pages.</p>
<p>If you open up your site within <strong>SharePoint Designer</strong> and expand the <strong>Lists</strong> folder, from there you can access the properties of that custom list by right clicking on it and selecting <strong>Properties</strong>. Once the <strong>List Properties</strong> pane is open, click on the <strong>Supporting Files</strong> tab.</p>
<p><img class="alignnone size-medium wp-image-440" title="list_properties" src="http://blog.qumsieh.ca/wp-content/uploads/2009/05/untitled-300x286.png" alt="" width="300" height="286" /></p>
<p>You&#8217;ll see there that you can actually choose what display, edit and new forms you want your list to be using. So if you&#8217;ve developed something custom, you would click <strong>Browse&#8230;</strong>, point to your new location and select the file. Here is where it starts to fail, I am only able to browse within the site itself and cannot navigate to my <strong>_layouts</strong> directory to select the files I mentioned above.</p>
<h2>Solution</h2>
<p>I suspected this was a limitation of the SharePoint Designer UI and not actually a limitation of the Object Model. I did a bit of fiddling with my <strong>SPList</strong> object and was not able to find anything that let me change those properties. If you look at the screenshot above, there is a key piece of information that&#8217;ll make the light turn on (at least it did for me). The <strong>Supporting Files</strong> tab has a drop down selector for the <strong>Content type specific forms</strong>. If you think about that a moment, you&#8217;ll remember that <strong>ALL</strong> lists within SharePoint inherit from a default <strong>content type</strong>.</p>
<p>So armed with that knowledge, I created an <strong>SPContentType</strong> object and took a look at it&#8217;s properties and methods. Sure enough, there are 3 properties I can set to change these forms: <strong>EditFormUrl</strong>, <strong>NewFormUrl</strong> and <strong>DisplayFormUrl</strong>. Here is some sample code I used to change the forms for my custom list called &#8216;My List&#8217; that inherits from the <strong>Item</strong> content type:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPWeb web <span style="color: #008000;">=</span> SPContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Web</span><span style="color: #008000;">;</span>
&nbsp;
web.<span style="color: #0000FF;">AllowUnsafeUpdates</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
&nbsp;
SPList list <span style="color: #008000;">=</span> web.<span style="color: #0000FF;">Lists</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;My List&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
SPContentType ct <span style="color: #008000;">=</span> list.<span style="color: #0000FF;">ContentTypes</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Item&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
ct.<span style="color: #0000FF;">EditFormUrl</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;_layouts/editform.aspx&quot;</span><span style="color: #008000;">;</span>
ct.<span style="color: #0000FF;">NewFormUrl</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;_layouts/newform.aspx&quot;</span><span style="color: #008000;">;</span>
ct.<span style="color: #0000FF;">DisplayFormUrl</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;_layouts/dispform.aspx&quot;</span><span style="color: #008000;">;</span>
&nbsp;
ct.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
list.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>So in the example code above, I had to determine what default content type my list inherited from. In this case it was the <strong>Item</strong> content type.</p>
<p><strong>NOTE</strong>: If you need to change the content type directly, you can definitely do that, however, I did a bit of testing and found that any lists ALREADY inheriting from that content type did not pickup my changes to the form locations. Any NEW list that I created that was inheriting from that content type did pickup the changes. In order for me to change the form locations for the existing list I had to use the code above. For reference, here is how you would change the content type directly. The code has only a subtle difference.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPWeb web <span style="color: #008000;">=</span> SPContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Web</span><span style="color: #008000;">;</span>
&nbsp;
web.<span style="color: #0000FF;">AllowUnsafeUpdates</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
&nbsp;
SPContentType ct <span style="color: #008000;">=</span> web.<span style="color: #0000FF;">ContentTypes</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Name Of Your Content Type&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
ct.<span style="color: #0000FF;">EditFormUrl</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;_layouts/editform.aspx&quot;</span><span style="color: #008000;">;</span>
ct.<span style="color: #0000FF;">NewFormUrl</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;_layouts/newform.aspx&quot;</span><span style="color: #008000;">;</span>
ct.<span style="color: #0000FF;">DisplayFormUrl</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;_layouts/dispform.aspx&quot;</span><span style="color: #008000;">;</span>
&nbsp;
ct.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>As always, any questions, let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2009/05/15/how-to-change-the-default-editform-newform-and-dispform/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Performance Considerations With the PeopleEditor Control</title>
		<link>http://blog.qumsieh.ca/2009/02/20/performance-considerations-with-the-peopleeditor-control/</link>
		<comments>http://blog.qumsieh.ca/2009/02/20/performance-considerations-with-the-peopleeditor-control/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 22:02:36 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Object Model]]></category>
		<category><![CDATA[PeopleEditor]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=353</guid>
		<description><![CDATA[I was recently working on a Black Ninja Software project for a client of ours where performance became an issues for one of the custom application pages we had developed. I plan to write up a more detailed report on how I managed to cut the load times of this particular page in half using [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently working on a <a href="http://blackninjasoftware.com">Black Ninja Software</a> project for a client of ours where performance became an issues for one of the custom application pages we had developed. I plan to write up a more detailed report on how I managed to cut the load times of this particular page in half using the <a href="http://www.red-gate.com/Products/ants_profiler/index.htm">ANTS Profiler</a>, but for now I wanted to highlight a piece of the puzzle that I discovered today.</p>
<p>I won&#8217;t cover how to load values into a PeopleEditor control from within a SharePoint list, you can view that in more detail <a href="http://blog.qumsieh.ca/2008/07/21/how-to-use-the-people-editor-control-loading-data-to-a-column-of-type-person-or-group/">here</a>.</p>
<p>Take a look at the example below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPFieldUserValue user <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPFieldUserValue<span style="color: #000000;">&#40;</span>web, Convert.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span>listItem<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Employee]));
&nbsp;
peEmployee.CommaSeparatedAccounts = user.LookupValue;</span></pre></td></tr></table></div>

<p>If we have an <strong>SPFieldUserValue</strong> object, calling upon the <strong>LookupValue</strong> property will return the <strong>Name</strong> of that user. This is not to be confused with the <strong>LoginName</strong> property. Assigning that to the <strong>CommaSeparatedAccounts</strong> property may do the trick and will load that user account into the control but not without a performance hit.</p>
<p>A better approach would be:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPFieldUserValue user <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPFieldUserValue<span style="color: #000000;">&#40;</span>web, Convert.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span>listItem<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Employee&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
peEmployee.<span style="color: #0000FF;">CommaSeparatedAccounts</span> <span style="color: #008000;">=</span> user.<span style="color: #0000FF;">User</span>.<span style="color: #0000FF;">LoginName</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>The difference is minor. Instead of using the <strong>LookupValue</strong> property, we leverage the <strong>SPUser</strong> object and call upon the <strong>LoginName</strong> property. In all of my testing, I noticed an improvement in speed when using the <strong>LoginName</strong> property.</p>
<p>Depending on your environment, the output from either of those properties will differ and that&#8217;s the heart of the performance issues. In my environment, <strong>LoginName</strong> and <strong>Name</strong> outputted the following:</p>
<p>Name &#8211; &#8220;Joe User&#8221;<br />
LoginName &#8211; &#8220;domain\juser&#8221;</p>
<p>Having the domain specified seems to speed this whole process up. I would be interested to hear from anyone else who&#8217;s encountered anything similar.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2009/02/20/performance-considerations-with-the-peopleeditor-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validating a PeopleEditor Control on PostBack</title>
		<link>http://blog.qumsieh.ca/2009/02/12/validating-a-peopleeditor-control-on-postback/</link>
		<comments>http://blog.qumsieh.ca/2009/02/12/validating-a-peopleeditor-control-on-postback/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 18:54:59 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Object Model]]></category>
		<category><![CDATA[PeopleEditor]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=328</guid>
		<description><![CDATA[Let&#8217;s talk about validation and the PeopleEditor control. There doesn&#8217;t seem to be a consensus on how this is supposed to be done so I&#8217;ll outline my findings and what eventually worked for me. I&#8217;ll start by explaining what I was attempting to do. I have a PeopleEditor control on a custom application page. My [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s talk about validation and the <strong>PeopleEditor</strong> control. There doesn&#8217;t seem to be a consensus on how this is supposed to be done so I&#8217;ll outline my findings and what eventually worked for me.</p>
<p>I&#8217;ll start by explaining what I was attempting to do. I have a <strong>PeopleEditor</strong> control on a custom application page. My custom application page is located in the <strong>_layouts</strong> directory. On this page, I also have a submit button that saves my data to a SharePoint list upon submit.</p>
<p>Now, I need to ensure before submit, that the PeopleEditor control contains a valid entry. Blank and invalid values are not permitted.</p>
<p>I first attempted to set the <strong>AlllowEmpty</strong> property to <strong>false</strong>. However, that didn&#8217;t seem to help. If I clicked submit, the page would post back and then the control would display an error once the page reloaded. At this point, it was too late to be informing the user that there was a problem with their entry, the page had already posted back, so this was not useful.</p>
<p>I also tried setting the <strong>ValidationEnabled</strong> property to <strong>true</strong>. That didn&#8217;t seem to make any difference either. I am confused as to how these two properties are supposed to work.</p>
<p>My next attempt was to add an ASP.NET required field validator to the page as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>wssawc<span style="color: #008000;">:</span>PeopleEditor AllowEmpty<span style="color: #008000;">=</span><span style="color: #666666;">&quot;false&quot;</span> ValidatorEnabled<span style="color: #008000;">=</span><span style="color: #666666;">&quot;true&quot;</span> Width<span style="color: #008000;">=</span><span style="color: #666666;">&quot;250px&quot;</span> ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;pePR&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> SelectionSet<span style="color: #008000;">=</span><span style="color: #666666;">&quot;User&quot;</span> MultiSelect<span style="color: #008000;">=</span><span style="color: #666666;">&quot;false&quot;</span> <span style="color: #008000;">/&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>RequiredFieldValidator ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;rfvPR&quot;</span> ControlToValidate<span style="color: #008000;">=</span><span style="color: #666666;">&quot;pePR&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> ErrorMessage<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Project Requestor (Cannot be blank)&quot;</span> Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Cannot be blank.&quot;</span> ValidationGroup<span style="color: #008000;">=</span><span style="color: #666666;">&quot;SubmitForm&quot;</span> Display<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Dynamic&quot;</span><span style="color: #008000;">&gt;&lt;/</span>asp<span style="color: #008000;">:</span>RequiredFieldValidator<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

<p>This took care of the client side validation that prevented the user from clicking submit without first entering a value into the control.</p>
<p>However, this did not handle the scenario when a user entered an invalid value in the control and hit submit. In this case, the form will attempt to submit and create the item, but will fail to save the value to the list item field because the value was invalid. An example of an invalid value would be bad data in that field.</p>
<p>To fix this, in my submit method, I added an <strong>IF</strong> statement to check how many resolved entities there were.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>pePR.<span style="color: #0000FF;">ResolvedEntities</span>.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// continue with the submit</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// output an error that the user did not enter a valid user</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>If anyone has any feedback, or has had success using the <strong>AllowEmpty</strong> property, please comment on this post or drop me a note. I&#8217;m interested in hearing how others were able to get around this issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2009/02/12/validating-a-peopleeditor-control-on-postback/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Understanding BreakRoleInheritance() Can Reset AllowUnsafeUpdates</title>
		<link>http://blog.qumsieh.ca/2009/01/31/understanding-breakroleinheritance-can-reset-allowunsafeupdates/</link>
		<comments>http://blog.qumsieh.ca/2009/01/31/understanding-breakroleinheritance-can-reset-allowunsafeupdates/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 15:35:07 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Object Model]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=319</guid>
		<description><![CDATA[I ran into a problem recently where I was attempting to update the permissions on an SPListItem within code but ran into the following error: Updates are currently disallowed on GET requests. To allow updates on a GET, set the &#8216;AllowUnsafeUpdates&#8217; property on SPWeb. Now I&#8217;m quite familiar with this error, and it&#8217;s one of [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a problem recently where I was attempting to update the permissions on an <strong>SPListItem </strong>within code but ran into the following error:</p>
<blockquote><p><span style="color: #ff0000;">Updates are currently disallowed on GET requests.  To allow updates on a GET, set the &#8216;AllowUnsafeUpdates&#8217; property on SPWeb.</span></p></blockquote>
<p>Now I&#8217;m quite familiar with this error, and it&#8217;s one of the few errors in SharePoint that gives you a hint in terms of what you need to do: set the <strong>AllowUnsafeUpdates </strong>property to <span style="color: #0000ff;"><strong>true </strong></span>on your <strong>SPWeb </strong>object.</p>
<p>Well, I did that, but without success. I was still receiving this error. I did a bit of digging and came across some useful information on <a href="http://hristopavlov.wordpress.com/2008/05/16/what-you-need-to-know-about-allowunsafeupdates/" target="_blank">Hristo Pavlov&#8217;s</a> blog. His article is titled <em>What You Need To Know About AllowUnsafeUpdates (Part 1)</em> and is a must read for anyone working with these objects.</p>
<p>The root of my issue turned out to be that my <strong>AllowUnsafeUpdates </strong>property was being reset to <span style="color: #0000ff;"><strong>false</strong></span>, even after I had already set it to <span style="color: #0000ff;">true</span>:</p>
<blockquote><p>When any object that implements <strong>ISecurable </strong>(those are SPWeb, SPList and SPListItem) breaks or reverts their role definition inheritance. This means every time you call <strong>SPRoleDefinitionCollection.BreakInheritance()</strong>, <strong>BreakRoleInheritance()</strong>, <strong>ResetRoleInheritance()</strong> or set the value of <strong>HasUniquePerm</strong> the <strong>AllowUnsafeUpdates</strong> property of the parent web will reset to its default value and you may need to set it back to true in order to do further updates to the same objects.</p></blockquote>
<p>To resolve my issue, I had to reset the <strong>AllowUnsafeUpdates </strong>property to <span style="color: #0000ff;"><strong>true </strong></span>after executing <strong>BreakRoleInheritance() </strong>in my code. I encourge you to read the rest of Hristo&#8217;s blog as it really helped me understand this property a little better.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2009/01/31/understanding-breakroleinheritance-can-reset-allowunsafeupdates/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Add a Rich Text Editor to your Custom Application Pages</title>
		<link>http://blog.qumsieh.ca/2009/01/13/how-to-add-a-rich-text-editor-to-your-custom-application-pages-or-web-parts/</link>
		<comments>http://blog.qumsieh.ca/2009/01/13/how-to-add-a-rich-text-editor-to-your-custom-application-pages-or-web-parts/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 18:01:10 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Object Model]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=256</guid>
		<description><![CDATA[This article will talk specifically about how to add a Rich Text Editor to your custom application pages. When initially doing some research on this topic, I found a lot of information, but not anything useful that I could actually implement. Here are my steps for adding a control of this type to your pages: [...]]]></description>
			<content:encoded><![CDATA[<p>This article will talk specifically about how to add a <strong>Rich Text Editor</strong> to your custom application pages. When initially doing some research on this topic, I found a lot of information, but not anything useful that I could actually implement. Here are my steps for adding a control of this type to your pages:</p>
<ol>
<li> If you would like to leverage the default sharepoint rich text editor control within your own custom pages, there&#8217;s one class you need to get familiar with: <strong><a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.inputformtextbox.aspx">InputFormTextBox</a></strong></li>
<li> The actual MSDN reference is not very helpful so to get started with a control of this type, you&#8217;ll need to know a couple things. Let&#8217;s begin by adding the control to our page as follows: 

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>wssawc<span style="color: #008000;">:</span>InputFormTextBox ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;iftxtDescription&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> TextMode<span style="color: #008000;">=</span><span style="color: #666666;">&quot;MultiLine&quot;</span> Rows<span style="color: #008000;">=</span><span style="color: #666666;">&quot;20&quot;</span>
RichTextMode<span style="color: #008000;">=</span><span style="color: #666666;">&quot;FullHtml&quot;</span> RichText<span style="color: #008000;">=</span><span style="color: #666666;">&quot;true&quot;</span><span style="color: #008000;">&gt;&lt;/</span>wssawc<span style="color: #008000;">:</span>InputFormTextBox<span style="color: #008000;">&gt;</span></pre></td></tr></table></div>

</li>
<li> The reason I know it&#8217;s wssawc is because at the top of the custom application page, <strong>Microsoft.SharePoint.WebControls</strong> namespace should be registered with a tagprefix of <strong>wssawc</strong>: 

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Register TagPrefix<span style="color: #008000;">=</span><span style="color: #666666;">&quot;wssawc&quot;</span> <span style="color: #0600FF;">Namespace</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;Microsoft.SharePoint.WebControls&quot;</span> Assembly<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;</span> <span style="color: #008000;">%&gt;</span></pre></td></tr></table></div>

</li>
<li> Some properties to be aware of:
<ul>
<li> <strong>ID</strong> &#8211; all controls need this. You&#8217;ll use the ID to work with the control in the code behind or the inline code</li>
<li> <strong>TextMode</strong> &#8211; this specifies whether the control is a Password control, a MultiLine control or SingleLine control. If youâ€™re attempting to create a rich text editor, you&#8217;ll want to set this to MultiLine.</li>
<li> <strong>Rows</strong> &#8211; specifies the rows/height of this control</li>
<li> <strong>RichTextMode</strong> &#8211; specifies whether the mode for the rich text. Options are: Compatible, FullHtml, HtmlAsXml.</li>
<li> <strong>RichText</strong> &#8211; true or false. Don&#8217;t forget to set this to true!</li>
</ul>
</li>
<li> To save data from this type of control:

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">calendarListItem<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Job Description&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> iftxtDescription.<span style="color: #0000FF;">Text</span><span style="color: #008000;">;</span></pre></div></div>

</li>
<li> To read data from a rich text column into this control:

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPFieldMultiLineText multiDescription <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>SPFieldMultiLineText<span style="color: #000000;">&#41;</span>listItem.<span style="color: #0000FF;">Fields</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Job Description&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
iftxtDescription.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> multiDescription.<span style="color: #0000FF;">GetFieldValueForEdit</span><span style="color: #000000;">&#40;</span>listItem<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Job Description&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

</li>
</ol>
<p>That should do it! I should point out that the <strong>multiDescription</strong> object that we created above actually has a couple useful methods beyond the one that I used. <strong>GetFieldValueForEdit()</strong> will grab the contents of the field in rich text and display it in my control quite nicely. I could also have used <strong>GetFieldValueAsHTML()</strong> for html or <strong>GetFieldValueAsText()</strong> for plain text.</p>
<blockquote><p>Note: If you attempt to set the Enabled or ReadOnly properties of this control, they won&#8217;t work. I ran into this issue myself and confirmed it with several other users having the same problem. Microsoft has acknowledged this as a bug (<strong>37846</strong>) but I have yet to find any documentation on this bug. If you do find anything, please let me know!</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2009/01/13/how-to-add-a-rich-text-editor-to-your-custom-application-pages-or-web-parts/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Write an SPQuery to Sort Your List</title>
		<link>http://blog.qumsieh.ca/2008/11/06/how-to-write-an-spquery-to-sort-your-list/</link>
		<comments>http://blog.qumsieh.ca/2008/11/06/how-to-write-an-spquery-to-sort-your-list/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 01:07:34 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Object Model]]></category>
		<category><![CDATA[SPQuery]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=71</guid>
		<description><![CDATA[If you&#8217;re working with an SPListItemCollection, you might have the need to sort the data that stored in the collection. The best way I&#8217;ve found to do this is to build an SPQuery object and use that to actually query for the information. Using an object of this type makes it possible to send in [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re working with an <strong>SPListItemCollection</strong>, you might have the need to sort the data that stored in the collection. The best way I&#8217;ve found to do this is to build an <strong>SPQuery</strong> object and use that to actually query for the information. Using an object of this type makes it possible to send in whatever <strong>sort</strong> and/or <strong>orderby</strong> clause we&#8217;d like to use.</p>
<p>For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;OrderBy<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;FieldRef</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">'EventDate'</span> <span style="color: #000066;">Ascending</span>=<span style="color: #ff0000;">'FALSE'</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/FieldRef<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/OrderBy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The full query would look something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">SPQuery oQuery <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPQuery<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
oQuery.<span style="color: #0000FF;">Query</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name='AP_x0020__x002f__x0020_O'/&gt;&quot;</span> <span style="color: #008000;">+</span>
<span style="color: #666666;">&quot;&lt;Value Type='Text'&gt;&quot;</span> <span style="color: #008000;">+</span> fruitName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&quot;</span> <span style="color: #008000;">+</span>
<span style="color: #666666;">&quot;&lt;OrderBy&gt;&lt;FieldRef Name='EventDate' Ascending='FALSE'&gt;&lt;/FieldRef&gt;&lt;/OrderBy&gt;&quot;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>If you receive an error similar to:</p>
<p><font color="red">One or more field types are not installed properly. Go to the list settings page to delete these fields.</font></p>
<p>Then you&#8217;ve set the FieldRef Name incorrectly. The trick to resolving this is:</p>
<ol>
<li>Navigate to your list that the column/field is contained within</li>
<li>Click the <strong>New</strong> button as you normally would to create a new item in this list</li>
<li>Click on <strong>View</strong>, <strong>Source</strong> from the toolbar in your browser window.</li>
<li>Finally, do a find on the phrase <strong>fieldinternalname</strong> and locate the field you&#8217;re trying to query on</li>
<li>Whatever value is stored in <strong>fieldinternalname</strong> is what you&#8217;ll want to use in your query</li>
</ol>
<p>Any questions, let me know.</p>
<p><strong>UPDATE</strong>: I recently discovered another trick to this. If you want to avoid having to seek out what the internal name of a particular field is, when you first name your column, do not include any spaces or special characters. Once the field (column) has been created, go back and rename the field to include the spaces or special characters as desired. SharePoint will still retain the original field name without spaces and you can use that directly in your query without issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2008/11/06/how-to-write-an-spquery-to-sort-your-list/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Use the PeopleEditor Control: Saving Data</title>
		<link>http://blog.qumsieh.ca/2008/07/21/how-to-use-the-people-editor-control-inside-sharepoint/</link>
		<comments>http://blog.qumsieh.ca/2008/07/21/how-to-use-the-people-editor-control-inside-sharepoint/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 01:50:45 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Object Model]]></category>
		<category><![CDATA[PeopleEditor]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=134</guid>
		<description><![CDATA[The PeopleEditor control is a common control that you&#8217;ll find implemented throughout SharePoint. If you&#8217;re building any custom web parts or application pages, you may want users to enter people specific information. The PeopleEditor is a good choice, however, there isn&#8217;t much out there in terms of documenting it&#8217;s use. Hopefully the information below will [...]]]></description>
			<content:encoded><![CDATA[<p>The PeopleEditor control is a common control that you&#8217;ll find implemented throughout SharePoint. If you&#8217;re building any custom web parts or application pages, you may want users to enter people specific information. The PeopleEditor is a good choice, however, there isn&#8217;t much out there in terms of documenting it&#8217;s use. Hopefully the information below will help get you started. If you are having issues, or if there is something I&#8217;ve missed in my post, please feel free to leave me a comment.</p>
<p>NOTE: One thing I found was that in order for this control to successfully save a user, that user has to exist within the site collection. If they don&#8217;t exist, the web.SiteUsers call will fail with an exception &#8216;user cannot be found&#8217;.</p>
<p><strong>UPDATE</strong>: You can use the <strong>EnsureUser</strong> method to verify that the specified user exists  and is a valid user of the web site. The neat thing about this method is that it will add the user to the site if they do not already exist, thus making the <strong>SiteUsers</strong> call unnecessary. Keep in mind that if you do use this method, you may need to elevate permissions to make that call, as not all users have access to add other users to your site collection.</p>
<p>To get started, create a new <strong>Custom List</strong> named <strong>Demo</strong>. Within <strong>List Settings</strong>, create a column of type <strong>Person or Group</strong> named <strong>Managers</strong>.</p>
<p>In this particular example, I&#8217;ll be building a custom application page with a single PeopleEditor control and a submit button. Create a new page (or download the files I&#8217;ve prepared for this article <a href='http://blog.qumsieh.ca/wp-content/uploads/2008/12/peopleeditor.zip'>here</a>), called <strong>Demo.aspx</strong> and store it in the <strong>_layouts</strong> directory.</p>
<p>Since the people editor control is located within the <strong>Microsoft.SharePoint.WebControls</strong> namespace, you&#8217;ll have to register the tag prefix at the top of your page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Register TagPrefix<span style="color: #008000;">=</span><span style="color: #666666;">&quot;wssawc&quot;</span> <span style="color: #0600FF;">Namespace</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;Microsoft.SharePoint.WebControls&quot;</span> Assembly<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;</span> <span style="color: #008000;">%&gt;</span></pre></td></tr></table></div>

<p>Insert the control on the page. If you look at my <strong>Demo.aspx</strong> page, I&#8217;ve put my control within a table tag, but you can format this however you&#8217;d like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>wssawc<span style="color: #008000;">:</span>PeopleEditor AllowEmpty<span style="color: #008000;">=</span><span style="color: #666666;">&quot;false&quot;</span> Width<span style="color: #008000;">=</span><span style="color: #666666;">&quot;300px&quot;</span> id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;peManagers&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> SelectionSet<span style="color: #008000;">=</span><span style="color: #666666;">&quot;User&quot;</span> <span style="color: #008000;">/&gt;</span></pre></td></tr></table></div>

<p><strong>AllowEmtpy</strong> &#8211; let&#8217;s you specify if blank values are permitted for this control<br />
<strong>SelectionSet</strong> &#8211; can be User or SecGroup or SPGroup or all three: User, SecGroup, SPGroup<br />
<strong>MultiSelect</strong> &#8211; set to true or false, false if you don&#8217;t want the user to be able to select more than one</p>
<p>Insert an <strong>asp:Button</strong> control onto the page; we&#8217;ll use this to submit the people data to the list.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>Button ID<span style="color: #008000;">=</span><span style="color: #666666;">&quot;btnSubmit&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> OnClick<span style="color: #008000;">=</span><span style="color: #666666;">&quot;btnSubmit_Click&quot;</span> ValidationGroup<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Main&quot;</span> Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;OK&quot;</span> CssClass<span style="color: #008000;">=</span><span style="color: #666666;">&quot;ms-ButtonHeightWidth&quot;</span> <span style="color: #008000;">/&gt;</span></pre></td></tr></table></div>

<p>In order to save values from the control to the column we created, you&#8217;ll need the following code in the button submit event:</p>

<div class="wp_syntax"><table><tr><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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> btnSubmit_Click<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// create a web object with context for the current site</span>
&nbsp;
    SPWeb web <span style="color: #008000;">=</span> SPContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Web</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// get the entries that were entered into the people editor and store them in a string</span>
&nbsp;
    <span style="color: #FF0000;">string</span> managers <span style="color: #008000;">=</span> peManagers.<span style="color: #0000FF;">CommaSeparatedAccounts</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// commaseparatedaccounts returns entries that are comma separated. we want to split those up</span>
&nbsp;
    <span style="color: #FF0000;">char</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> splitter <span style="color: #008000;">=</span> <span style="color: #000000;">&#123;</span> <span style="color: #666666;">','</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> splitPPData <span style="color: #008000;">=</span> managers.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span>splitter<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// this collection will store the user values from the people editor which we'll eventually use</span>
    <span style="color: #008080; font-style: italic;">// to populate the field in the list</span>
&nbsp;
    SPFieldUserValueCollection values <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPFieldUserValueCollection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// for each item in our array, create a new sp user object given the loginname and add to our collection</span>
&nbsp;
    <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> splitPPData.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">string</span> loginName <span style="color: #008000;">=</span> splitPPData<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>loginName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            SPUser user <span style="color: #008000;">=</span> web.<span style="color: #0000FF;">SiteUsers</span><span style="color: #000000;">&#91;</span>loginName<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// you could also use SPUser user = web.EnsureUser(loginName);</span>
&nbsp;
            SPFieldUserValue fuv <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SPFieldUserValue<span style="color: #000000;">&#40;</span>web, user.<span style="color: #0000FF;">ID</span>, user.<span style="color: #0000FF;">LoginName</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            values.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>fuv<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// set the Person or Group column</span>
&nbsp;
    SPListItemCollection listItems <span style="color: #008000;">=</span> web.<span style="color: #0000FF;">Lists</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Demo&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Items</span><span style="color: #008000;">;</span>
&nbsp;
    SPListItem manager <span style="color: #008000;">=</span> listItems.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    manager<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Managers&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> values<span style="color: #008000;">;</span>
&nbsp;
    manager.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>That should do it. You should be able to save the entries to the list. If you&#8217;d like to learn how to read values from a list column into a PeopleEditor control, you can read that post <a href="http://blog.qumsieh.ca/2008/07/21/how-to-use-the-people-editor-control-loading-data-to-a-column-of-type-person-or-group/">here</a>. Any feedback?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2008/07/21/how-to-use-the-people-editor-control-inside-sharepoint/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to Use the PeopleEditor Control: Loading Data</title>
		<link>http://blog.qumsieh.ca/2008/07/21/how-to-use-the-people-editor-control-loading-data-to-a-column-of-type-person-or-group/</link>
		<comments>http://blog.qumsieh.ca/2008/07/21/how-to-use-the-people-editor-control-loading-data-to-a-column-of-type-person-or-group/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 01:39:35 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Object Model]]></category>
		<category><![CDATA[PeopleEditor]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=116</guid>
		<description><![CDATA[This post was really planned as a Part 2 to the first post I made about saving data from the PeopleEditor control. My aim for this entry is to tackle the reverse scenario: reading data from a field of type Person or Group and loading it into a PeopleEditor control. If you have any troubles [...]]]></description>
			<content:encoded><![CDATA[<p>This post was really planned as a <strong>Part 2</strong> to the first post I made about saving data from the PeopleEditor control. My aim for this entry is to tackle the reverse scenario: reading data from a field of type <strong>Person or Group</strong> and loading it into a PeopleEditor control. If you have any troubles with Part 1 or Part 2 of this series or if there&#8217;s something I&#8217;ve missed, I would love to hear about it. Leave a comment or drop me an email. Thanks!</p>
<p>If you&#8217;ve followed <a href="http://blog.qumsieh.ca/2008/7/21/how-to-use-the-people-editor-control-inside-sharepoint">Part 1</a> of this series, you should already have a list named <strong>Demo</strong> created with a column of type <strong>Person or Group</strong> named <strong>Managers</strong>. If not, begin by creating a new <strong>Custom List</strong> named <strong>Demo</strong>. Within <strong>List Settings</strong>, create a column of type <strong>Person or Group</strong> and name it <strong>Managers</strong>. Set the <strong>Allow multiple selections</strong> checkbox to <strong>Yes</strong>.</p>
<p>Once you&#8217;ve created the list and the column, add a few new entries so we have some data to load.</p>
<p>Now let&#8217;s create a custom application page with a single PeopleEditor control on it. Create a new page (or download it <a href='http://blog.qumsieh.ca/wp-content/uploads/2008/12/peopleeditorload.zip'>here</a>) called Load.aspx and store it in the layouts directory.</p>
<p>Since the people editor control is located within the <strong>Microsoft.SharePoint.WebControls</strong> namespace, you&#8217;ll have to register the tag prefix at the top of your page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Register TagPrefix<span style="color: #008000;">=</span><span style="color: #666666;">&quot;wssawc&quot;</span> <span style="color: #0600FF;">Namespace</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;Microsoft.SharePoint.WebControls&quot;</span> Assembly<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;</span> <span style="color: #008000;">%&gt;</span></pre></td></tr></table></div>

<p>Insert the control on the page. This is the control we&#8217;ll be loading the data into:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>wssawc<span style="color: #008000;">:</span>PeopleEditor AllowEmpty<span style="color: #008000;">=</span><span style="color: #666666;">&quot;false&quot;</span> Width<span style="color: #008000;">=</span><span style="color: #666666;">&quot;300px&quot;</span> id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Managers&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span> SelectionSet<span style="color: #008000;">=</span><span style="color: #666666;">&quot;User&quot;</span> <span style="color: #008000;">/&gt;</span></pre></td></tr></table></div>

<p>The next step will require that we wire up our <strong>Page_Load</strong> event to load the necessary data from the list to the control.</p>

<div class="wp_syntax"><table><tr><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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> Page_Load<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF;">try</span>
    <span style="color: #000000;">&#123;</span>
&nbsp;
        SPWeb web <span style="color: #008000;">=</span> SPContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Web</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// get a handle to the list weâ€™ll be pulling values from</span>
        SPList list <span style="color: #008000;">=</span> web.<span style="color: #0000FF;">Lists</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Demo&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// using the querystring parameter containing the id, get the list item weâ€™ll be dealing with</span>
        SPListItem listItem <span style="color: #008000;">=</span> list.<span style="color: #0000FF;">GetItemById</span><span style="color: #000000;">&#40;</span>Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>Request<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;id&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// the managers column is of type Person or Group, so we can use a spfielduservaluecollection to     store the values from it</span>
        SPFieldUserValueCollection users <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>SPFieldUserValueCollection<span style="color: #000000;">&#41;</span>listItem<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;Managers&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// our array will hold the entities weâ€™ll use to eventually assign to the people editor control</span>
        ArrayList entityArrayList <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ArrayList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// loop through each use in the collection, set the key, add to the array</span>
        <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> users.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
&nbsp;
            PickerEntity entity <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PickerEntity<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            entity.<span style="color: #0000FF;">Key</span> <span style="color: #008000;">=</span> users<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">User</span>.<span style="color: #0000FF;">LoginName</span><span style="color: #008000;">;</span>
            entityArrayList.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>entity<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        Managers.<span style="color: #0000FF;">UpdateEntities</span><span style="color: #000000;">&#40;</span>entityArrayList<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
&nbsp;
        Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>ex.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Now when you navigate to your page, pass the id of a valid item in the list, and the entries in the manager column should load: http://servername/_layouts/Load.aspx?id=1 (replace servername with the name of your MOSS install)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2008/07/21/how-to-use-the-people-editor-control-loading-data-to-a-column-of-type-person-or-group/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>SPWeb.SiteUsers vs SPWeb.Users</title>
		<link>http://blog.qumsieh.ca/2008/06/26/spweb-siteusers-vs-spweb-users/</link>
		<comments>http://blog.qumsieh.ca/2008/06/26/spweb-siteusers-vs-spweb-users/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 01:35:23 +0000</pubDate>
		<dc:creator>shereen</dc:creator>
				<category><![CDATA[SharePoint 2007]]></category>
		<category><![CDATA[Object Model]]></category>

		<guid isPermaLink="false">http://blog.qumsieh.ca/?p=109</guid>
		<description><![CDATA[Update: I recently ran into this issue again and discovered that there is in fact a way to ensure that the user exists before creating the user object &#8212; the EnsureUser method of the SPWeb class. According to MSDN this method &#8220;Checks whether the specified login name belongs to a valid user of the Web [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> I recently ran into this issue again and discovered that there is in fact a way to ensure that the user exists before creating the user object &#8212; the EnsureUser method of the SPWeb class. According to MSDN this method &#8220;Checks whether the specified login name belongs to a valid user of the Web site, and if the login name does not already exist, adds it to the Web site.&#8221; Check it out <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.ensureuser.aspx">here</a>. I tried this and it seems to resolve my issue. If the user is not a member of the web site, this method will add it which is effectively what I needed it to do.</p>
<p>Please note that you&#8217;ll need to set the AllowUnsafeUpdates property to true in order for the EnsureUser method to do it&#8217;s thing.</p>
<p>When trying to use <strong>SPWeb.Users</strong> to find a particular user in a site, I was receiving a &#8220;User cannot be found&#8221; error within the site that my method was being called from. I took a look at the site permissions and discovered that the user account I was attempting to add did not actually exist as a member of that site. At first glance, I didn&#8217;t actually think that this would be a problem, but apparently there is a key disctinction between the various SPWeb methods.</p>
<p>If I added the user to the site manually, the <strong>SPWeb.Users</strong> call worked. Any users automatically added to the site upon creation, will not be found with a <strong>SPWeb.Users</strong> call. This is an important disctinction.</p>
<p>My next attempt was to use <strong>SPWeb.SiteUsers</strong> which was only successful if the user existed at the site collection level. However, if the user was not a member of any group at the site collection level, my method would still fail with a &#8220;User cannot be found&#8221; error.</p>
<p>Alternatively, I might be able to leverage the user profile database to get a list of all <strong>possible</strong> users to choose from but I haven&#8217;t done any experimenting with this yet. If anyone has any additional information or feedback on this, please drop me a note.</p>
<p><em>Windows SharePoint Services 3.0 SDK</em></p>
<p><strong>SPWeb.AllUsers</strong> &#8211; Gets the collection of user objects that represents all users who are either members of the site or who have browsed to the site as authenticated members of a domain group in the site.</p>
<p><strong>SPWeb.SiteUsers</strong> &#8211; Gets the collection of all users that belong to the site collection.</p>
<p><strong>SPWeb.Users</strong> &#8211; Gets the collection of user objects that are explicitly assigned permissions on the Web site.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.qumsieh.ca/2008/06/26/spweb-siteusers-vs-spweb-users/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

