As part of my upgrade I needed to be able to delete some web parts (closed or open) and close and open some web parts. In many respects this command, gl-setwebpartstate, is basically the twin to the gl-movewebpart command which I recently posted about. In fact, I created that command as a template for this command – the only difference from a code standpoint is that instead of calling MoveWebPart I’m calling either DeleteWebPart, CloseWebPart, or OpenWebPart (based on a user provided parameter). Everything else is exactly the same so once I had the gl-movewebpart command done creating this command took me less than 5 minutes. Below is the bit of code that differs from the gl-movewebpart code (I won’t show the rest as it’s identical to the gl-movewebpart command):

1if (Params["delete"].UserTypedIn)
2    manager.DeleteWebPart(wp);
3else if (Params["close"].UserTypedIn)
4    manager.CloseWebPart(wp);
5else if (Params["open"].UserTypedIn)
6    manager.OpenWebPart(wp);
7
8if (!Params["delete"].UserTypedIn)
9    manager.SaveChanges(wp);

The syntax of the command I created can be seen below.

C:\>stsadm -help gl-setwebpartstate

stsadm -o gl-setwebpartstate


Opens, Closes, Adds, or Deletes a web part on a page.

Parameters:
        -url <web part page URL>
        {-id <web part ID> | -title <web part title>}
        {-delete | -open | -close | -add}
        {[-assembly <assembly name>] | [-typename <type name>] | [-webpartfile <web part file if adding>]}
        [-zone <zone ID>]
        [-zoneindex <zone index>]
        {[-properties <comma separated list of key value pairs: "Prop1=Val1,Prop2=Val2">] |
         [-propertiesfile <path to a file with xml property settings (<Properties><Property Name="Name1">Value1</Property><Property Name="Name2">Value2</Property></Properties>)>]}
        [-publish]

Here’s an example of how to close a web part on a given page:

stsadm -o gl-setwebpartstate -url "http://intranet/hr/pages/default.aspx" -title "Grouped Listings" -close -publish

Update 12/10/2007: I’ve updated this command to now support the adding of web parts to a page. In doing this I also added the ability to set the zone and zone ID of a web part (thus encapsulating the gl-movewebpart command – I needed this in order to know where to add the part to so I figured I’d just allow the user to provide the same information for any other changes). I also added the ability to set properties of the web part. This is done using a comma separated list of key value pairs for simple data or an XML file with any encoded data. Note that only primitive data types are supported so if you try to set a property that requires a complex data type it will error out. Also – if you only wish to set the properties of an existing web part then simply pass in a command that matches the current state of the web part (so if the web part is open already then use the -open parameter and then pass in any desired properties).

Here’s an example of how to add a web part to a page using simple properties:

stsadm -o gl-setwebpartstate -url "http://intranet/testweb1/default.aspx" -title "Table of Contents" -add -assembly "Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" -typename "Microsoft.SharePoint.Publishing.WebControls.TableOfContentsWebPart" -zone "Left" -zoneindex 0 -properties "ShowPages=false,LevelsToShow=3" -publish

Here’s an example of how to add a web part to a page using an XML file containing properties:

stsadm -o gl-setwebpartstate -url "http://teamsites/pages/default.aspx" -title "I need to…" -add -assembly "Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" -typename "Microsoft.SharePoint.Portal.WebControls.TasksAndToolsWebPart" -zone "MiddleRightZone" -zoneindex 0 -propertiesfile "c:\webpartprops.xml" -publish

The webpartprops.xml file will look like this:

1<Properties>
2	<Property Name="TasksAndToolsWebUrl">/SiteDirectory</Property>
3	<Property Name="FilterFieldValue">Top Tasks</Property>
4	<Property Name="FilterCategory">TasksAndTools</Property>
5	<Property Name="TasksAndToolsListName">Sites</Property>
6	<Property Name="Xsl">&lt;xsl:stylesheet xmlns:x=&quot;http://www.w3.org/2001/XMLSchema&quot; version=&quot;1.0&quot; exclude-result-prefixes=&quot;xsl ddwrt slwrt msxsl&quot; xmlns:ddwrt=&quot;http://schemas.microsoft.com/WebParts/v2/DataView/runtime&quot; xmlns:slwrt=&quot;http://schemas.microsoft.com/WebParts/v3/SummaryLink/runtime&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:msxsl=&quot;urn:schemas-microsoft-com:xslt&quot; xmlns:tnt=&quot;urn:schemas-microsoft-com:sharepoint:TasksAndToolsWebPart&quot; &gt; &lt;xsl:param name=&quot;tasksAndtools_IsRTL&quot; /&gt; &lt;xsl:param name=&quot;tasksAndTools_Width&quot; /&gt; &lt;xsl:template match=&quot;/&quot;&gt; &lt;xsl:call-template name=&quot;MainTemplate&quot;/&gt; &lt;/xsl:template&gt; &lt;xsl:template name=&quot;MainTemplate&quot; xmlns:ddwrt=&quot;http://schemas.microsoft.com/WebParts/v2/DataView/runtime&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:msxsl=&quot;urn:schemas-microsoft-com:xslt&quot;&gt; &lt;xsl:variable name=&quot;Rows&quot; select=&quot;/dsQueryResponse/NewDataSet/Row&quot;/&gt; &lt;xsl:variable name=&quot;RowCount&quot; select=&quot;count($Rows)&quot;/&gt; &lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; style=&quot;border-collapse:collapse; margin:0px;&quot;&gt; &lt;tr style=&quot;margin-top:3px;margin-bottom:1px;height:28px;border:0px;&quot;&gt; &lt;td style=&quot;padding-left:4px; white-space:nowrap ;&quot;&gt; &lt;xsl:if test=&quot;string-length($tasksAndTools_Width) != 0&quot;&gt; &lt;select id=&quot;TasksAndToolsDDID&quot; class=&quot;ms-selwidth&quot; style=&quot;width:{$tasksAndTools_Width}&quot; size=&quot;1&quot; title=&quot;Choose a task that you need to perform&quot; &gt; &lt;option selected=&quot;true&quot; value=&quot;0&quot;&gt;Choose task&lt;/option&gt; &lt;xsl:call-template name=&quot;MainTemplate.body&quot;&gt; &lt;xsl:with-param name=&quot;Rows&quot; select=&quot;$Rows&quot;/&gt; &lt;/xsl:call-template&gt; &lt;/select&gt; &lt;/xsl:if&gt; &lt;xsl:if test=&quot;string-length($tasksAndTools_Width) = 0&quot;&gt; &lt;select id=&quot;TasksAndToolsDDID&quot; class=&quot;ms-selwidth&quot; size=&quot;1&quot; title=&quot;Choose a task that you need to perform&quot;&gt; &lt;option selected=&quot;true&quot; value=&quot;0&quot;&gt;Choose task&lt;/option&gt; &lt;xsl:call-template name=&quot;MainTemplate.body&quot;&gt; &lt;xsl:with-param name=&quot;Rows&quot; select=&quot;$Rows&quot;/&gt; &lt;/xsl:call-template&gt; &lt;/select&gt; &lt;/xsl:if&gt; &lt;/td&gt; &lt;xsl:if test=&quot;$tasksAndtools_IsRTL = true()&quot;&gt; &lt;td style=&quot;padding-right:5px; padding-left: 14px;white-space:nowrap ;&quot;&gt; &lt;a id=&quot;TasksAndToolsGo&quot; accesskey=&quot;G&quot; title=&quot;Go&quot; href=&quot;javascript:TATWP_jumpMenu()&quot;&gt; &lt;img title=&quot;Go&quot; alt=&quot;Go&quot; border=&quot;0&quot; src=&quot;/_layouts/images/icongo01RTL.gif&quot; style=&quot;border-width:0px;&quot; onmouseover=&quot;this.src='/_layouts/images/icongo02RTL.gif'&quot; onmouseout=&quot;this.src='/_layouts/images/icongo01RTL.gif'&quot;/&gt; &lt;/a&gt; &lt;/td&gt; &lt;/xsl:if&gt; &lt;xsl:if test=&quot;$tasksAndtools_IsRTL = false()&quot;&gt; &lt;td style=&quot;padding-right:14px; padding-left: 5px;white-space:nowrap ;&quot;&gt; &lt;a id=&quot;TasksAndToolsGo&quot; accesskey=&quot;G&quot; title=&quot;Go&quot; href=&quot;javascript:TATWP_jumpMenu()&quot;&gt; &lt;img title=&quot;Go&quot; alt=&quot;Go&quot; border=&quot;0&quot; src=&quot;/_layouts/images/icongo01.gif&quot; style=&quot;border-width:0px;&quot; onmouseover=&quot;this.src='/_layouts/images/icongo02.gif'&quot; onmouseout=&quot;this.src='/_layouts/images/icongo01.gif'&quot; /&gt; &lt;/a&gt; &lt;/td&gt; &lt;/xsl:if&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/xsl:template&gt; &lt;xsl:template name=&quot;MainTemplate.body&quot; xmlns:ddwrt=&quot;http://schemas.microsoft.com/WebParts/v2/DataView/runtime&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:msxsl=&quot;urn:schemas-microsoft-com:xslt&quot;&gt; &lt;xsl:param name=&quot;Rows&quot;/&gt; &lt;xsl:for-each select=&quot;$Rows&quot;&gt; &lt;xsl:variable name=&quot;GroupStyle&quot; select=&quot;'auto'&quot;/&gt; &lt;option style=&quot;display:{$GroupStyle}&quot; value=&quot;{ddwrt:EnsureAllowedProtocol(substring-before(@URL, ‘, ‘))}&quot; &gt; &lt;xsl:value-of select=&quot;@Title&quot;/&gt; &lt;/option&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt;</Property>
7</Properties>

Update 7/8/2008: I’ve added a new parameter, -webpartfile, which can be used to effectively import an exported web part file. Just use it in conjunction with the -add option (do not use the -assembly or -typename parameters). I also adjusted the code so that if it fails to add the web part using the object model it will revert to the web service – this is to account for some web parts (like the KPI web part) that require a valid SPContext object.