{"id":1324,"date":"2011-12-22T13:57:31","date_gmt":"2011-12-22T12:57:31","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=1324"},"modified":"2015-09-08T20:36:44","modified_gmt":"2015-09-08T18:36:44","slug":"ever-tried-to-update-a-user-subtype-from-code","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/ever-tried-to-update-a-user-subtype-from-code\/","title":{"rendered":"Ever tried to update a user subtype from code?"},"content":{"rendered":"<p>User subtypes are one of the many new and nice things in SharePoint 2010.<br \/>\nThe other day I was editing user profiles and changed the subtype for this profile in code. After changing the user subtype Commit() was called and I thought I was done.<br \/>\nBut then it just started, because the user profile wasn&#8217;t updated at all. No exception, no message at all, just the old user subtype.<br \/>\nThe used code:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nProfileSubtypeManager psubm = ProfileSubtypeManager.Get(context);\r\nProfileSubtype newSubtype = psubm.GetProfileSubtype(newSubtypeName);\r\ncurrentUserProfile.ProfileSubtype = newSubtype;\r\ncurrentUserProfile.Commit();\r\n<\/pre>\n<p>Changing the user subtype in the UI really updated the setting, so it was time to debug the SharePoint UserProfiles assembly.<br \/>\nThe short version of what the debugging session(s) told me:<br \/>\nA UserProfileUpdateWrapper was created with the following xml<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-16&quot;?&gt;\r\n&lt;MSPROFILE&gt;\r\n &lt;PROFILE ProfileName=&quot;UserProfile&quot;&gt;\r\n &lt;USER NewUser=&quot;0&quot; NTAccount=&quot;account&quot; UserID=&quot;3db01e67-abb0-4946-8fa8-85943768cb79&quot;&gt;\r\n<\/pre>\n<p>The next step is iterating through the user profile fields to check if the value &#8216;IsDirty&#8217; aka the value has been changed. This is the only trigger to actually update a user profile.<br \/>\nWhen adjusting the user subtype from the UI a few properties are updated, even when they&#8217;re not changed. The final XML looks like the following:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-16&quot;?&gt;\r\n&lt;MSPROFILE&gt;\r\n &lt;PROFILE ProfileName=&quot;UserProfile&quot;&gt;\r\n &lt;USER NewUser=&quot;0&quot; NTAccount=&quot;account&quot; UserID=&quot;3db01e67-abb0-4946-8fa8-85943768cb79&quot;&gt;\r\n &lt;PROPERTY PropertyName=&quot;Assistant&quot; Privacy=&quot;1&quot; PropertyValue=&quot;&quot; \/&gt;\r\n &lt;PROPERTY PropertyName=&quot;PictureURL&quot; Privacy=&quot;1&quot; PropertyValue=&quot;&quot; \/&gt;\r\n &lt;PROPERTY PropertyName=&quot;SPS-TimeZone&quot; Privacy=&quot;1&quot; PropertyValue=&quot;&quot; \/&gt;\r\n &lt;\/USER&gt;\r\n &lt;\/PROFILE&gt;\r\n&lt;\/MSPROFILE&gt;\r\n<\/pre>\n<p>and a real update of the user profile occurred.<\/p>\n<p>To make the update work from code appearently another property has to be updated together with the change of subtype. After a test with an update of a random property, the user subtype was updated too.<\/p>\n<h3>Summary<\/h3>\n<p>Changing only the user subtype from code, does trigger the Commit() method, but doesn&#8217;t call the update profile, because no user profile property was changed. Use a dummy property to update every time the user subtype has changed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>User subtypes are one of the many new and nice things in SharePoint 2010. The other day I was editing user profiles and changed the subtype for this profile in code. After changing the user subtype Commit() was called and &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/ever-tried-to-update-a-user-subtype-from-code\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[42,6],"class_list":["post-1324","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010","tag-sharepoint-2010","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=1324"}],"version-history":[{"count":7,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1324\/revisions"}],"predecessor-version":[{"id":1331,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/1324\/revisions\/1331"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=1324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=1324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=1324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}