{"id":1282,"date":"2012-09-11T11:21:54","date_gmt":"2012-09-11T11:21:54","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=1282"},"modified":"2024-04-26T12:28:02","modified_gmt":"2024-04-26T12:28:02","slug":"adding-a-web-part-to-a-list-view","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/adding-a-web-part-to-a-list-view\/","title":{"rendered":"Adding a web part to a List View"},"content":{"rendered":"<h4>Adding a web part to a List View<\/h4>\n<p>It\u2019s quite straightforward to add a web part to a specific library view. This can be extended to deploy a web part to every library in every web of a site collection, for example.<\/p>\n<p>I developed this to deploy a web part I wrote that extracts the folder URL from the query string in the URL, and builds a table dynamically of folder metadata, as well as a useful button for user action.<\/p>\n<p>First we get the View URL, and use the LimitedWebPartManager, which works great for customizing ghosted Views. We then loop through the web parts to make sure we are not adding a web part for a second time. I found the Contains() method doesn\u2019t work, as it compares the actual GUID, and as each web part is a new object, it will always have a new GUID.<\/p>\n<p>Below is the PowerShell script to achieve it:<\/p>\n<div><\/div>\n<pre lang=\"php\">$FullyQualifiedWebPartName= \"FolderMetaView.VisualWebPart1.VisualWebPart1\" #trick: get from .DWP XML, this is the name of my web part\r\n[Reflection.Assembly]::LoadWithPartialName(\"FolderMetaView.VisualWebPart1.VisualWebPart1\")\r\n \r\n$JPWeb=get-spweb \"SharePoint\/Div\/TestWeb\"\r\n$JPLibs=$JPWeb.lists\r\n$JPLib=$JPLibs[\"SimpleList\"]\r\n \r\n$Views=$JPLib.Views\r\n$View=$Views[\"Specific View\"]\r\n$WebPartMgr=$JPWeb.GetLimitedWebPartManager($View.Url,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)\r\n \r\n$wps=$webpartmgr.get_WebParts() #get web parts\r\n \r\n$found=$false;  #let's make sure we don't add the web part a second time\r\nfor ($wpi=0; $wpi -lt $wps.Count; $wpi++)\r\n{\r\n    if ($WebPartMgr.WebParts[$wpi].webbrowsableobject.tostring() -eq $FullyQualifiedWebPartName)\r\n    {\r\n        $found=$true;\r\n    }\r\n}\r\n \r\nif (!$found) #if not found, then add it\r\n{\r\n    $webpart=new-object  $FullyQualifiedWebPartName\r\n    $WebPartMgr.AddWebPart($webpart, \"Main\",0); \r\n}\r\n<\/pre>\n<p>Note Views have one web part zone called \u201cMain\u201d. By deploying to ZoneIndex 0, I\u2019m putting the web part on top<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adding a web part to a List View It\u2019s quite straightforward to add a web part to a specific library view. This can be extended to deploy a web part to every library in every web of a site collection, for example. I developed this to deploy a web part I wrote that extracts the [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":1283,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[26],"tags":[],"class_list":["post-1282","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=1282"}],"version-history":[{"count":2,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1282\/revisions"}],"predecessor-version":[{"id":1287,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/1282\/revisions\/1287"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/1283"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=1282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=1282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=1282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}