{"id":2505,"date":"2017-12-22T17:40:06","date_gmt":"2017-12-22T16:40:06","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=2505"},"modified":"2017-12-22T17:40:06","modified_gmt":"2017-12-22T16:40:06","slug":"spfx-associate-listview-command-set-to-a-content-type","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/spfx-associate-listview-command-set-to-a-content-type\/","title":{"rendered":"SPFx associate listview command set to a content type"},"content":{"rendered":"<p>At the time of writing there is no support to associate a listview command set to a content type using the elements.xml.<\/p>\n<p>In previous versions of SharePoint this could be accomplished using RegistrationType=&#8221;ContentType&#8221; in combination with for example RegistrationId=&#8221;0x01&#8243;. In SPFx this leads to an error during the installation of the application. The exception thrown is quite clear, the RegistrationType is not supported.<\/p>\n<blockquote><p>&#8220;\u2026Microsoft.SharePoint.SPException: The specified RegistrationType value is not supported for client-side custom actions. at Microsoft.SharePoint.SPCustomActionElement.UpdateCustomActionsTable\u2026&#8221;<\/p><\/blockquote>\n<p>An example of an elements.xml file used by the SharePoint Framework looks like:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\"> \r\n&lt;version=&quot;1.0&quot; encoding=&quot;utf-8&quot;&gt;\r\n&lt;Elements xmlns=&quot;http:\/\/schemas.microsoft.com\/sharepoint\/&quot;&gt;\r\n &lt;CustomAction \r\n  Title=&quot;ShowAtContentType&quot; \r\n  RegistrationId=&quot;100&quot; \r\n  RegistrationType=&quot;List&quot; \r\n  Location=&quot;ClientSideExtension.ListViewCommandSet.CommandBar&quot; \r\n  ClientSideComponentId=&quot;d61116f8-011e-4e71-9f9d-d06c9ae024cf&quot; \r\n  ClientSideComponentProperties=&quot;{}&quot;&gt;\r\n &lt;\/CustomAction&gt;\r\n&lt;\/Elements&gt;\r\n<\/pre>\n<p>Which associates the custom action with a generic list.<\/p>\n<p>Fortunately there is another possibility to create the association other than using the elements.xml file.<\/p>\n<h3>Apply a command set to a list where an item of a specific content type is <em>selected<\/em><\/h3>\n<h4>What to accomplish?<\/h4>\n<p>The only situation when the listview command set has to show is when an item of content type &#8216;ITIdeaDev Item&#8217; is selected in a list.<\/p>\n<h4>How to associate the listview command set to this content type?<\/h4>\n<p>To prepare for the solution a custom content type &#8216;ITIdeaDev Item&#8217; is created with Id 0x01000575B0D2D2390447AFB5D4E17C260BE0.<br \/>\nA standard list is created with the standard Item content type and the newly created content type added.<\/p>\n<p>Creating a listview command set using the generator results in example code to show a command set when exactly one item is selected:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@override\r\n public onListViewUpdated(event: IListViewCommandSetListViewUpdatedParameters): void {\r\n  const compareOneCommand: Command = this.tryGetCommand('COMMAND_1');\r\n  if (compareOneCommand) {\r\n    compareOneCommand.visible = event.selectedRows.length === 1;\r\n  }\r\n }\r\n<\/pre>\n<p>The currently selected ListView rows at the time when the event occurred are represented by the event.selectedRows property. A single &#8216;row&#8217; is the presentation of a list item and it stores an array of cell values for the associated columns.<br \/>\nFortunately the column ContentType as well as ContentTypeId are part of this array as shown in Figure 1.<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/1-SPFx-command-set-contenttype.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2517\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/1-SPFx-command-set-contenttype.png\" alt=\"\" width=\"318\" height=\"370\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/1-SPFx-command-set-contenttype.png 318w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/1-SPFx-command-set-contenttype-258x300.png 258w\" sizes=\"auto, (max-width: 318px) 100vw, 318px\" \/><\/a><\/p>\n<p>Figure 1 &#8211; Array of cell values<\/p>\n<p>So it&#8217;s quite easy to check if the selected item is of the custom content type:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n@override\r\n public onListViewUpdated(event: IListViewCommandSetListViewUpdatedParameters): void {\r\n  const compareOneCommand: Command = this.tryGetCommand('COMMAND_1');\r\n   if (compareOneCommand) {\r\n    if (event.selectedRows.length === 1) {\r\n     compareOneCommand.visible = false;\r\n     if(event.selectedRows&#x5B;0].getValueByName(&quot;ContentTypeId&quot;).startsWith(ctId)){\r\n      compareOneCommand.visible = true;\r\n     }\r\n    }\r\n   }\r\n  }\r\n<\/pre>\n<p>The figures below show the result: the command set is only shown when the item of the specific content type is selected.<br \/>\n<a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/2-SPFx-command-set-contenttype.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2516\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/2-SPFx-command-set-contenttype.png\" alt=\"\" width=\"598\" height=\"245\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/2-SPFx-command-set-contenttype.png 598w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/2-SPFx-command-set-contenttype-300x123.png 300w\" sizes=\"auto, (max-width: 598px) 100vw, 598px\" \/><\/a><\/p>\n<p>Figure 2 &#8211; Command set is shown when item of specific content type is selected<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/3-SPFx-command-set-contenttype.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2515\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/3-SPFx-command-set-contenttype.png\" alt=\"\" width=\"501\" height=\"244\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/3-SPFx-command-set-contenttype.png 501w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/3-SPFx-command-set-contenttype-300x146.png 300w\" sizes=\"auto, (max-width: 501px) 100vw, 501px\" \/><\/a><br \/>\nFigure 3 &#8211; Command set isn&#8217;t visible when an item based on another content type is selected<\/p>\n<h3>Apply a command set to a list that has a specific content type <em>available<\/em><\/h3>\n<p>I can think of another situation: apply a command set to a list that has a specific content type <i>available<\/i>. This can be useful if the command set updates, removes or adds content of a specific content type.<\/p>\n<p>To accomplish this pnp js is used to question the list about available content types. The result of this is stored in a Promise in an early stage, the onInit method, which can be used in the onListViewUpdated method to determine if the command set has to be visible.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n public existsContentType(ctId: string): Promise&lt;any&gt; {\r\n  return pnp.sp.web.getList(this.context.pageContext.list.serverRelativeUrl).contentTypes.get().then(result =&gt; {\r\n   for (var i = 0; i &lt; result.length; i++) {\r\n    if (result&#x5B;i].StringId.startsWith(ctId)) {\r\n     return true;\r\n    }\r\n   }\r\n  return false;\r\n });\r\n}\r\n\r\n@override\r\npublic onInit(): Promis&lt;void&gt; {\r\n return this.ctPromise = this.existsContentType(ctId);\r\n}\r\n\r\n@override\r\npublic onListViewUpdated(event: IListViewCommandSetListViewUpdatedParameters): void {\r\n const compareOneCommand: Command = this.tryGetCommand('COMMAND_1');\r\n if (compareOneCommand) {\r\n   this.ctPromise.then((result: boolean) =&gt; {\r\n    compareOneCommand.visible = result;\r\n   });\r\n }\r\n}\r\n<\/pre>\n<p>It doesn&#8217;t matter when zero, one or more items are selected, the command set is available as long as the content type is available at the list.<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/4-SPFx-command-set-contenttype.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2519\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/4-SPFx-command-set-contenttype.png\" alt=\"\" width=\"305\" height=\"251\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/4-SPFx-command-set-contenttype.png 305w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/4-SPFx-command-set-contenttype-300x247.png 300w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/a><\/p>\n<p>Figure 4 &#8211; Command set is visible when more than one item is selected<\/p>\n<p><a href=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/5-SPFx-command-set-contenttype.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2518\" src=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/5-SPFx-command-set-contenttype.png\" alt=\"\" width=\"722\" height=\"232\" srcset=\"https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/5-SPFx-command-set-contenttype.png 722w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/5-SPFx-command-set-contenttype-300x96.png 300w, https:\/\/www.itidea.nl\/wp-content\/uploads\/2017\/12\/5-SPFx-command-set-contenttype-600x193.png 600w\" sizes=\"auto, (max-width: 722px) 100vw, 722px\" \/><\/a><br \/>\nFigure 5 &#8211; Command set is visible when nothing is selected<\/p>\n<h3>Summary<\/h3>\n<p>The SharePoint Framework has lot of possibilities, even if some things aren&#8217;t implemented yet or don&#8217;t work as you&#8217;re used to or expect. Think outside of your experience and think in the new framework possibilities!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At the time of writing there is no support to associate a listview command set to a content type using the elements.xml. In previous versions of SharePoint this could be accomplished using RegistrationType=&#8221;ContentType&#8221; in combination with for example RegistrationId=&#8221;0x01&#8243;. In &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/spfx-associate-listview-command-set-to-a-content-type\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39,41],"tags":[37,48],"class_list":["post-2505","post","type-post","status-publish","format-standard","hentry","category-office-365","category-sharepoint","tag-office365","tag-spfx"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2505","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\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=2505"}],"version-history":[{"count":22,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2505\/revisions"}],"predecessor-version":[{"id":2565,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2505\/revisions\/2565"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=2505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=2505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=2505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}