{"id":1031,"date":"2012-06-14T17:24:21","date_gmt":"2012-06-14T16:24:21","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1031"},"modified":"2012-06-14T17:24:21","modified_gmt":"2012-06-14T16:24:21","slug":"spd-webpart-definition-code","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2012\/06\/14\/spd-webpart-definition-code\/","title":{"rendered":"SPD WebPart Definition Code"},"content":{"rendered":"<p>Using SharePoint Designer, users can update WebPart properties directly via the <em>code<\/em> pane.  Now, if this is a requirement for you, you&#8217;ll notice that when editing a custom version three WebPart (<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.web.ui.webcontrols.webparts.webpart(v=vs.90).aspx\" title=\"System.Web.UI.WebControls.WebParts.WebPart\" target=\"_blank\">System.Web.UI.WebControls.WebParts.WebPart<\/a>) that the properties are displayed as attributes of the WebPart element:<\/p>\n<pre lang=\"XML\">\r\n<WebPart:Test_WebPart runat=\"server\" CustomProperty=\"custom value\"><\/WebPart:Test_WebPart>\r\n<\/pre>\n<p>I&#8217;ve trimmed the above to make it more readable, the real element would be far longer and all on one line.<\/p>\n<p>To make it easier for users to update properties via SPD and to move the properties to their own child elements, one option available is to make a few changes to your WebPart.<\/p>\n<ul>\n<li>Change the WebPart to inherit from the version 2 class &#8211; <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.webpartpages.webpart.aspx\" title=\"Microsoft.SharePoint.WebPartPages.WebPart\" target=\"_blank\">Microsoft.SharePoint.WebPartPages.WebPart<\/a><\/li>\n<li>Add the <em>SupportsAttributeMarkup(false)<\/em> attribute to the WebPart class<\/li>\n<li>For the property you want displayed as a child element, ensure the property attribute used for storage is <em>WebPartStorage(Storage.Shared)<\/em> and not <em>Personalizable(PersonalizationScope.Shared)<\/em>.  The storage type used is not important.<\/li>\n<\/ul>\n<p>For example:<br \/>\n<!--more--><\/p>\n<pre lang=\"csharp\">\r\n[SupportsAttributeMarkup(false)]\r\npublic class TestWebPart : Microsoft.SharePoint.WebPartPages.WebPart\r\n{\r\n    [WebPartStorage(Storage.Shared)]\r\n    public string TestProperty\r\n    { get; set; }\r\n}\r\n<\/pre>\n<p>This will give something like the following:<br \/>\n<a href=\"http:\/\/www.stuartroberts.net\/wp-content\/uploads\/2012\/06\/SPD.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.stuartroberts.net\/wp-content\/uploads\/2012\/06\/SPD.jpg\" alt=\"\" title=\"SPD Code Pane\" width=\"1286\" height=\"495\" class=\"aligncenter size-full wp-image-1033\" srcset=\"https:\/\/www.stuartroberts.net\/wp-content\/uploads\/2012\/06\/SPD.jpg 1286w, https:\/\/www.stuartroberts.net\/wp-content\/uploads\/2012\/06\/SPD-300x115.jpg 300w, https:\/\/www.stuartroberts.net\/wp-content\/uploads\/2012\/06\/SPD-1024x394.jpg 1024w\" sizes=\"(max-width: 1286px) 100vw, 1286px\" \/><\/a><\/p>\n<p>If you don&#8217;t want to, or can&#8217;t change to the version two WebPart, a better solution would be to decorate your WebPart property with the <em>PersistenceMode(PersistenceMode.InnerProperty)<\/em> attribute.  This will render the property as a child element of the WebPart, without the need to follow the steps described above.<\/p>\n<pre lang=\"csharp\">\r\n[SupportsAttributeMarkup(true), ParseChildren(true),]\r\npublic class TestWebPart : System.Web.UI.WebControls.WebParts.WebPart\r\n{\r\n    [Personalizable(PersonalizationScope.Shared),\r\n     PersistenceMode(PersistenceMode.InnerProperty)]\r\n    public string TestProperty\r\n    { get; set; }\r\n}\r\n<\/pre>\n<p>The added bonus of this method is that if, for instance, your property was created to store HTML, when editing the property value in SharePoint Designer&#8217;s code pane, IntelliSense will be provided, which is rather neat.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using SharePoint Designer, users can update WebPart properties directly via the code pane. Now, if this is a requirement for you, you&#8217;ll notice that when editing a custom version three WebPart (System.Web.UI.WebControls.WebParts.WebPart) that the properties are displayed as attributes of &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2012\/06\/14\/spd-webpart-definition-code\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[50,3],"tags":[81,11],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-gD","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1031"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1031"}],"version-history":[{"count":17,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1031\/revisions"}],"predecessor-version":[{"id":1050,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1031\/revisions\/1050"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}