使用Powershell,如何设置基于下拉列表的自定义webpart属性?

时间:2016-05-27 19:47:20

标签: powershell sharepoint-2013

我有以下Powershell功能:

function Add-WebPartToPage {
[CmdletBinding()]
Param( 
   [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)] 
   [System.Object]$web, 

   [Parameter(Mandatory=$true, Position=1)] 
   [string]$pageTitle,
   [Parameter(Mandatory=$true, Position=2)] 
   [Microsoft.SharePoint.SPList]$list,       

   [Parameter(Mandatory=$false, Position=3)]
   [System.Object]$webpart,

   [Parameter(Mandatory=$false, Position=4)]       
   [string]$webPartTitle,

   [Parameter(Mandatory=$false, Position=5)]       
   [string]$panelTitle,

   [Parameter(Mandatory=$false, Position=6)]       
   [int]$CSPUElevel,

   [Parameter(Mandatory=$false, Position=7)]       
   [int]$panelType 
) 
   Process 
{ 
    #if webpart specified then add it to the page
    if($webpart)
    {
        $pageName = $list.Title + "/$pageTitle.aspx"
        $pageName = $pageName -replace " ",""
        $page = $web.GetFile($pageName)
        $page.CheckOut()
        $mgr = $web.GetLimitedWebPartManager($pageName, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

        if($webPartTitle)
        {
            $webpart.Title = $webPartTitle
        }
        $webpart.ChromeType = "None"
        #++ Extended Settings
        if($webpart.GetType().Name -eq "DynamicWebPart")
        {
        # 
            $pinProperties = $webpart.GetType().GetProperties()
            foreach ($prop in $pinProperties)
            {
                if(($panelTitle) -and ($prop.Name -eq "PanelTitle"))
                {   
                    $webpart.PanelTitle = $panelTitle
                }
                if(($CSPUElevel) -and ($prop.Name -eq "CSPUElevel"))
                {
                    $webpart.CSPUElevel = $CSPUElevel
                }
                if(($panelType) -and ($prop.Name -eq "PanelType"))
                {
                    $webpart.PanelType = $panelType
                }
            }
        }
        #-- End of Extended Settings
        $mgr.AddWebPart($webpart, "FullPage", 0)
        Write-Host -f Green "Webpart $webpart.WebBrowsableObject added to the page"

        $page.CheckIn("Checked in Automatically")
        #$page.Publish("Published Automatically")
    }
 }

}   

一切正常,直到达到webpart的扩展属性。在该逻辑中,面板标题将正确设置。这是对自定义属性的直接字符串分配。但是,CSPUElevel和PanelType是下拉列表,永远不会进行赋值。更有趣的是,CSPUElevel和PanelType似乎没有以与Panel Title相同的方式公开为自定义属性。换句话说,$ prop.Name的测试永远不会等于“CSPUElevel”或“PanelType”。

来自webpart(DynamicWebPart.cs):

        protected override void CreateChildControls()
    {
        DynamicWebPartUserControl control = (DynamicWebPartUserControl)Page.LoadControl(_ascxPath);
        control.Panel = (DynamicWebPartUserControl.PanelOption)this.Panel;
        control.PanelTitle = this.PanelTitle;
        control.CSPUEBreadCrumbSelected = (DynamicWebPartUserControl.Breadcrumb)this.BreadcrumbSelected;
        Controls.Add(control);
    }

来自webpart(DynamicWebPartUserControl.ascx.cs):

    public enum PanelOption
    {
        None = 0,
        AdministrativeInfo = 1,
        GeneralInfo = 2,
        Engineering = 3
    }

    public PanelOption Panel
    {
        get;
        set;
    }

面板类型定义:

        private DynamicWebPartUserControl.PanelOption _Panel = 0;

    [Category("SPS - Extended Settings"),
    Personalizable(PersonalizationScope.Shared),
    WebBrowsable(true),
    WebDisplayName("Panel Type"),
    WebDescription("Please select the type of panel to display")]
    public DynamicWebPartUserControl.PanelOption Panel
    {
        get { return this._Panel; }
        set { this._Panel = value; }
    }

0 个答案:

没有答案