5 Replies Latest reply on Dec 20, 2011 9:40 PM by pkaiser

    PI TreeView WebPart deployment using Windows PowerShell Script


      Please find the attached power shell script to add PI tree view webpart and set the tree view webpart parameter( using PI WebParts 2010). I want to add this webpart on SharePoint webpart page and set the below mentioned properties using power shell. With the attached script, I am able to add the webpart but not able to set the mentioned properties. I dig into OSIsoft.RtWebParts.RtTreeView.TreeViewWP assembly using reflector and tried few options to set the properties. All this properties are public and should able to do set/get




      # core part of the code for reference.




      [System.Web.UI.WebControls.WebParts.WebPart] $webpart


            $webpart = new-object  OSIsoft.RtWebParts.RtTreeView.TreeViewWP


            OSIsoft.RtWebParts.RtTreeView.TreeViewWebPartProperties] $webpartProp


            $webpartProp = $webpart.TreeViewProperties




            $webpart.TreeViewProperties.LevelsToDisplay.CurrentValue ="1"


            $level = $webpart.TreeViewProperties.LevelsToDisplay






      $webpartmanager.AddWebPart($webpart, $zone, $index)






      But it is not working.




      Properties that need to be set using power Shell.





      Root Node Of Tree:


      Invalid path to AF object

      Default Selected Node:


      AF element not found


           Display Properties



      Number Of Levels Initially Loaded

      Must be greater than zero.


      Number Of Levels Initially Displayed

      Must be greater than or equal to zero.


      Maximum Number Of Levels To Display



      To run the Script:


      1.       Upload the attached psTest.aspx in SharePoint document library


      2.       Use the name of your SharePoint document library in – SPLibrary parameter


      3.       Copy the attached Add-PITreeViewWebPart.ps1 to local folder and r Go to power shell command prompt and type


      ./Add-PITreeViewWebPart -url "http://bhihouscr01:5555/sites/EncanaTest" -SPLibrary "Site%20Pages"  -pageName "psTest.aspx" -zone "Left" -index 1


      More details are included in the script.




      Please see the below screenshots showing tree view properties.








      I also want to know in case of connecting WebParts which properties need to be set here.

        • Re: PI TreeView WebPart deployment using Windows PowerShell Script

          (moved this discussion thread to the SharePoint Development and Integration forum, since it does not exactly pertain to programming with PI SDK)

            • Re: PI TreeView WebPart deployment using Windows PowerShell Script

              One of the challenges with configuring PI WebParts using PowerShell is that some of our classes are not CLS-Compliant. Frankly, ease of use in PowerShell was not a product design consideration at inception, since PowerShell didn't release until about a year after the first RtWebParts release, and even then it was not obviously useful for administering SharePoint. You can easily see this CLS compliance issue for yourself. In the submitted code above, after you use "new-object" to create the web part in PowerShell, just type $webpart and press enter. You'll get back an error message because of CLS compliance issues with the TreeViewWP class, the key component of which is the statement "Failed to use non CLS compliant type." CLS Compliance issues are not uncommon, even Microsoft has them. You see the same error message when you work with Microsoft.SharePoint.Administration.SPContentDatabase, and probably for the same reason -- neither SharePoint nor PI WebParts were designed for administration by PowerShell, because PowerShell didn't exist at the time.


              The most common workaround for this type of issue (actually, the only one I've encountered), is to use Reflection. It can make your code lengthier, and more difficult to comprehend, but it obviates the CLS compliance issue. Reflection is a much larger topic than can be addressed in this post. If you're not familiar with the concept, there are many articles available to explain. Here's a nice one from Tech Republic: http://www.techrepublic.com/article/c-sharp-reflection-save-development-time-throughout-the-project-life-cycle/6090667


              In short, Reflection is used to get metadata about classes at runtime. For example, you can use the System.Type.GetProperty method to get a PropertyInfo object to learn about the properties of a class at runtime, such as the TreeViewProperties of the TreeViewWP class. Our goal is to configure a web part, so metadata isn't necessarily what we're after, but the GetValue method of the PropertyInfo class is the trick. Here's the example from above, rewritten to use Reflection to create a new instance of the PI TreeView web part on a page, and to set its LevelsToLoad property to 1 instead of the default of 2:

              $site = new-object Microsoft.SharePoint.SPSite("http://YourSiteURL")
              $web = $site.OpenWeb()
              $mgr = $web.GetLimitedWebPartManager("http://YourPageURL", `
              $wp = new-object OSIsoft.RtWebParts.RtTreeView.TreeViewWP
              # Here's where we use Reflection to set the property value...# First, get the PropertyInfo object for the TreeViewProperties
              # property of the TreeViewWP class$ref = [OSIsoft.RtWebParts.RtTreeView.TreeViewWP].GetProperty("TreeViewProperties")# Next, use the GetValue method of the PropertyInfo for TreeViewProperties
              # to actually get the TreeViewProperties$props = $ref.GetValue($wp, $null)# Now you can manipulate the properties of the TreeViewProperties, such as LevelsToLoad$props.LevelsToLoad.CurrentValue = 1
              $mgr.AddWebPart($wp, "YourZone", 0)

              The URLs and zone name in the  code above (the strings that start with "Your") need to be replaced with the appropriate values for your environment.