{"id":227,"date":"2010-12-30T15:02:18","date_gmt":"2010-12-30T15:02:18","guid":{"rendered":"http:\/\/www.myfatblog.co.uk\/?p=227"},"modified":"2010-12-30T15:03:36","modified_gmt":"2010-12-30T15:03:36","slug":"end-of-the-year-already","status":"publish","type":"post","link":"http:\/\/www.myfatblog.co.uk\/index.php\/2010\/12\/end-of-the-year-already\/","title":{"rendered":"End of the year already?"},"content":{"rendered":"<p>Wow.. Christmas really came and went.. My last post was back in november when I had just returned from Gran Canaria after a nice relaxing week in the sun.<\/p>\n<p>After that short interlude, I had my first speaking engagement at a company seminar in front of 100 delegates. I was incredibly nervous and got a little lost during my presentation, but held it in check enough to deliver the 1 hour demo on SharePoint and the collaborative advantages of working with Microsoft Lync and Exchange.<\/p>\n<p>Since then I\u00e2\u20ac\u2122ve also been working on some Facebook and Twitter integration for SharePoint, which turned out to be not as straight forward as I hoped. Aiming to get some of this written up in the new year and posted here as certainly the Facebook API is not very .Net friendly at all!<\/p>\n<p>Finally I\u00e2\u20ac\u2122ve also been investigating the possibility of using the User profile service as a means of storing information about employees that could be synced with external systems such as SAP. in this example I\u00e2\u20ac\u2122m looking at storing an employees Annual Holiday entitlement, and then updating or reading that value through the object model. It\u00e2\u20ac\u2122s during this process that I\u00e2\u20ac\u2122ve come across a disturbing issue with updating a profile that\u00e2\u20ac\u2122s marked as Admin editable only with quite a hacky workaround. (Found at <a title=\"http:\/\/wss.boman.biz\/Lists\/Posts\/Post.aspx?ID=23\" href=\"http:\/\/wss.boman.biz\/Lists\/Posts\/Post.aspx?ID=23\">http:\/\/wss.boman.biz\/Lists\/Posts\/Post.aspx?ID=23<\/a>)<\/p>\n<p>So first off, I\u00e2\u20ac\u2122ve made the property available by creating it to the User Properties through Central Admin. It\u00e2\u20ac\u2122s a simple integer property called HolidayEntitlement. At present I\u00e2\u20ac\u2122m not importing the value from anywhere as I intended just pushing a value in from code. The key thing here though is that the user must NOT be able to edit this themselves. So, when we create the property, we make sure that the \u00e2\u20ac\u02dc\u00e2\u20ac\u009dDo not allow users to edit values for this property\u00e2\u20ac\u009d option is well and truly ticked!<\/p>\n<p>With the property created, a short section of code gets the User profile for the current user, and then iterates through the properties showing their name and values:- (outputzone is just an ASP:Label defined in the user control so that I can write quickly to the page output.)<\/p>\n<blockquote>\n<pre class=\"csharpcode\">SPUser currUser = SPContext.Current.Web.CurrentUser;\r\nSPServiceContext serviceContext = SPServiceContext.GetContext(SPContext.Current.Site);\r\nUserProfileManager upm = <span class=\"kwrd\">new<\/span> UserProfileManager(serviceContext);\r\nUserProfile up = upm.GetUserProfile(currUser.LoginName);<\/pre>\n<\/blockquote>\n<style type=\"text\/css\">\n.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<blockquote>\n<pre class=\"csharpcode\">ProfileSubtypePropertyManager pstpm = up.Properties;\r\n<span class=\"kwrd\">foreach<\/span> (ProfileSubtypeProperty pstp <span class=\"kwrd\">in<\/span> pstpm)\r\n{\r\n                    outputZone.Text += <span class=\"str\">&quot;Property:- &quot;<\/span> + pstp.Name + br;\r\n\r\n                    <span class=\"kwrd\">string<\/span> propVal = <span class=\"kwrd\">string<\/span>.Empty;\r\n\r\n                    <span class=\"kwrd\">if<\/span> (up[pstp.Name].Value != <span class=\"kwrd\">null<\/span>)\r\n                    {\r\n                        propVal = up[pstp.Name].Value.ToString();\r\n                    }\r\n                    outputZone.Text += propVal;\r\n                    outputZone.Text += br;\r\n\r\n                    outputZone.Text += <span class=\"str\">&quot;Unprotected:- &quot;<\/span> + pstp.IsUserEditable + br;\r\n}<\/pre>\n<\/blockquote>\n<style type=\"text\/css\">\n.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<p>So when you place this webpart on a page, you should see an output of all the properties currently mapped for the current user. If all went ok, You should see our \u00e2\u20ac\u0153HolidayEntitlement\u00e2\u20ac\u009d property with an empty value and \u00e2\u20ac\u0153Unprotected:- False\u00e2\u20ac\u009d<\/p>\n<p>If we now add a short section of code to update the Holiday Entitlement to 25, we\u00e2\u20ac\u2122ll receive a \u00e2\u20ac\u0153Property Not Editable. This property can only be modified by an administrator\u00e2\u20ac\u009d exception. Exactly what I would expect given that I marked the property as not editable by the end user. To get around this, we\u00e2\u20ac\u2122ll crack open an Elevated Priviledges section with a new SPSite object and perform the update:-<\/p>\n<blockquote>\n<pre class=\"csharpcode\">SPSecurity.RunWithElevatedPrivileges(<span class=\"kwrd\">delegate<\/span>()\r\n{\r\n\r\n     <span class=\"kwrd\">using<\/span> (SPSite elevSite = <span class=\"kwrd\">new<\/span> SPSite(SPContext.Current.Site.ID))\r\n     {\r\n\r\n          SPServiceContext elevServiceContext = SPServiceContext.GetContext(elevSite);\r\n          UserProfileManager elevUpm = <span class=\"kwrd\">new<\/span> UserProfileManager(serviceContext);\r\n          UserProfile elevUp = elevUpm.GetUserProfile(currUser.LoginName);\r\n         \r\n          <span class=\"kwrd\">if<\/span> (elevUp[<span class=\"str\">&quot;HolidayEntitlement&quot;<\/span>].Value == <span class=\"kwrd\">null<\/span>)\r\n          {\r\n               elevUp[<span class=\"str\">&quot;HolidayEntitlement&quot;<\/span>].Value = 25;\r\n               elevUp.Commit();\r\n          }\r\n});<\/pre>\n<\/blockquote>\n<style type=\"text\/css\">\n.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<p>That should do it, one quick page refresh later and we see a nice exception \u00e2\u20ac\u0153Property Not Editable. This property can only be modified by an administrator\u00e2\u20ac\u009d. Hang on, didn\u00e2\u20ac\u2122t we just solve this by wrapping our code in an elevated priviledges block? Well no, it would appear not. for some reason, the userProfile update is still trying to run under the context of the connected user, despite elevating priviledges. After a short search on the net, I cam across the post linked at the top regarding a bit of a dirty hack to get around this issue. Basically we have to Null the current HTTPContext before the update, then recreate it afterwards as per the code below. <\/p>\n<blockquote>\n<pre class=\"csharpcode\">\r\nSPSecurity.RunWithElevatedPrivileges(<span class=\"kwrd\">delegate<\/span>()\r\n{\r\n\r\n     <span class=\"kwrd\">using<\/span> (SPSite elevSite = <span class=\"kwrd\">new<\/span> SPSite(SPContext.Current.Site.ID))\r\n     {\r\n\r\n          SPServiceContext elevServiceContext = SPServiceContext.GetContext(elevSite);\r\n          UserProfileManager elevUpm = <span class=\"kwrd\">new<\/span> UserProfileManager(serviceContext);\r\n          UserProfile elevUp = elevUpm.GetUserProfile(currUser.LoginName);\r\n\r\n          HttpContext savedContext = HttpContext.Current;\r\n          HttpContext.Current = <span class=\"kwrd\">null<\/span>;\r\n         \r\n          <span class=\"kwrd\">if<\/span> (elevUp[<span class=\"str\">&quot;HolidayEntitlement&quot;<\/span>].Value == <span class=\"kwrd\">null<\/span>)\r\n          {\r\n               elevUp[<span class=\"str\">&quot;HolidayEntitlement&quot;<\/span>].Value = 25;\r\n               elevUp.Commit();\r\n          }\r\n\r\n          HttpContext.Current = savedContext\r\n});<\/pre>\n<\/blockquote>\n<style type=\"text\/css\">\n.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<p>This works and I\u00e2\u20ac\u2122m not exactly clear why, so kinda hoping some of my SharePoint colleagues can help me understand what\u00e2\u20ac\u2122s going on.<\/p>\n<p>Paul.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wow.. Christmas really came and went.. My last post was back in november when I had just returned from Gran Canaria after a nice relaxing week in the sun. After that short interlude, I had my first speaking engagement at a company seminar in front of 100 delegates. I was incredibly nervous and got a &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/www.myfatblog.co.uk\/index.php\/2010\/12\/end-of-the-year-already\/\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[14,3,15,13,31,38,35],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>End of the year already? - Blog of an overweight SharePoint addict<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.myfatblog.co.uk\/index.php\/2010\/12\/end-of-the-year-already\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"End of the year already? - Blog of an overweight SharePoint addict\" \/>\n<meta property=\"og:description\" content=\"Wow.. Christmas really came and went.. My last post was back in november when I had just returned from Gran Canaria after a nice relaxing week in the sun. After that short interlude, I had my first speaking engagement at a company seminar in front of 100 delegates. I was incredibly nervous and got a &hellip; Continue reading\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.myfatblog.co.uk\/index.php\/2010\/12\/end-of-the-year-already\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog of an overweight SharePoint addict\" \/>\n<meta property=\"article:published_time\" content=\"2010-12-30T15:02:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2010-12-30T15:03:36+00:00\" \/>\n<meta name=\"author\" content=\"Cimares\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@cimares\" \/>\n<meta name=\"twitter:site\" content=\"@cimares\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Cimares\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.myfatblog.co.uk\/index.php\/2010\/12\/end-of-the-year-already\/\",\"url\":\"http:\/\/www.myfatblog.co.uk\/index.php\/2010\/12\/end-of-the-year-already\/\",\"name\":\"End of the year already? - Blog of an overweight SharePoint addict\",\"isPartOf\":{\"@id\":\"http:\/\/www.myfatblog.co.uk\/#website\"},\"datePublished\":\"2010-12-30T15:02:18+00:00\",\"dateModified\":\"2010-12-30T15:03:36+00:00\",\"author\":{\"@id\":\"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/55ae8f6885bb5b8390dad001f3da83c6\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.myfatblog.co.uk\/index.php\/2010\/12\/end-of-the-year-already\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.myfatblog.co.uk\/#website\",\"url\":\"http:\/\/www.myfatblog.co.uk\/\",\"name\":\"Blog of an overweight SharePoint addict\",\"description\":\"The rantings of a (not so) food obsessed IT consultant!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.myfatblog.co.uk\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/55ae8f6885bb5b8390dad001f3da83c6\",\"name\":\"Cimares\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/About_D057\/TopOfTheWorld.jpg\",\"contentUrl\":\"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/About_D057\/TopOfTheWorld.jpg\",\"caption\":\"Cimares\"},\"sameAs\":[\"http:\/\/www.myfatblog.co.uk\"],\"url\":\"http:\/\/www.myfatblog.co.uk\/index.php\/author\/reginald\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"End of the year already? - Blog of an overweight SharePoint addict","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.myfatblog.co.uk\/index.php\/2010\/12\/end-of-the-year-already\/","og_locale":"en_US","og_type":"article","og_title":"End of the year already? - Blog of an overweight SharePoint addict","og_description":"Wow.. Christmas really came and went.. My last post was back in november when I had just returned from Gran Canaria after a nice relaxing week in the sun. After that short interlude, I had my first speaking engagement at a company seminar in front of 100 delegates. I was incredibly nervous and got a &hellip; Continue reading","og_url":"http:\/\/www.myfatblog.co.uk\/index.php\/2010\/12\/end-of-the-year-already\/","og_site_name":"Blog of an overweight SharePoint addict","article_published_time":"2010-12-30T15:02:18+00:00","article_modified_time":"2010-12-30T15:03:36+00:00","author":"Cimares","twitter_card":"summary_large_image","twitter_creator":"@cimares","twitter_site":"@cimares","twitter_misc":{"Written by":"Cimares","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.myfatblog.co.uk\/index.php\/2010\/12\/end-of-the-year-already\/","url":"http:\/\/www.myfatblog.co.uk\/index.php\/2010\/12\/end-of-the-year-already\/","name":"End of the year already? - Blog of an overweight SharePoint addict","isPartOf":{"@id":"http:\/\/www.myfatblog.co.uk\/#website"},"datePublished":"2010-12-30T15:02:18+00:00","dateModified":"2010-12-30T15:03:36+00:00","author":{"@id":"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/55ae8f6885bb5b8390dad001f3da83c6"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.myfatblog.co.uk\/index.php\/2010\/12\/end-of-the-year-already\/"]}]},{"@type":"WebSite","@id":"http:\/\/www.myfatblog.co.uk\/#website","url":"http:\/\/www.myfatblog.co.uk\/","name":"Blog of an overweight SharePoint addict","description":"The rantings of a (not so) food obsessed IT consultant!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.myfatblog.co.uk\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/55ae8f6885bb5b8390dad001f3da83c6","name":"Cimares","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.myfatblog.co.uk\/#\/schema\/person\/image\/","url":"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/About_D057\/TopOfTheWorld.jpg","contentUrl":"http:\/\/www.myfatblog.co.uk\/images\/BlogImages\/About_D057\/TopOfTheWorld.jpg","caption":"Cimares"},"sameAs":["http:\/\/www.myfatblog.co.uk"],"url":"http:\/\/www.myfatblog.co.uk\/index.php\/author\/reginald\/"}]}},"_links":{"self":[{"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/227"}],"collection":[{"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/comments?post=227"}],"version-history":[{"count":2,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/227\/revisions"}],"predecessor-version":[{"id":229,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/posts\/227\/revisions\/229"}],"wp:attachment":[{"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/media?parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/categories?post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.myfatblog.co.uk\/index.php\/wp-json\/wp\/v2\/tags?post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}