【问题标题】:How to add custom webparts to pages in powershell scripts?如何将自定义 Web 部件添加到 Powershell 脚本中的页面?
【发布时间】:2011-05-20 04:50:54
【问题描述】:

我创建了一个自定义 webpart 并添加到 webpart 库中。我需要在我的页面中添加该 webpart 控件。 请帮助我如何在 powershell 脚本中实现这一点。

【问题讨论】:

    标签: sharepoint scripting powershell


    【解决方案1】:

    使用GetLimitedWebPartsManager() 获取对页面管理器的引用,然后调用其AddWebPart() 方法:

    $mgr = $web.GetLimitedWebPartManager($yourPageUrl,
        [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
    $mgr.AddWebPart($yourWebPart, "YourZone", 0);
    

    有关详细示例,请参阅http://blogs.flexnetconsult.co.uk/colinbyrne/2007/02/10/SharePointPowerShell8TheOneWithTheContactWebPart.aspx

    【讨论】:

    • 在本例中如何设置$yourWebPart
    • 好吧,您将实例化您的 Web 部件类。例如,上面链接中的示例发出$webpart=new-object Microsoft.SharePoint.Portal.WebControls.ContactFieldControl
    • 如何引用自定义 Web 部件类(Microsoft 未内置的东西)?如果它在 GAC 中,是否可以只做类似 {AssemblyName}.{ClassName} 的事情?
    • 我从来没有遇到过 GACed 程序集的完全限定名称的任何问题,而且Microsoft.Sharepoint.* 实际上并没有内置在 Microsoft 系统中(它是一个附加组件)。以这种方式引用 Web 部件类是否有问题?
    • 是的,我遇到了一些麻烦。你可以在这里看到更详细的解释:sharepoint.stackexchange.com/questions/25267/…
    【解决方案2】:

    这是使用 powershell 将 webpart 添加到页面的简单脚本,该脚本经过测试并且工作正常

    http://soreddymanjunath.blogspot.in/2014/07/add-webpart-to-page-using-powershell.html

    请注意,将 webpart 添加到页面导出 webpart 到您的本地驱动器和 .webpart/.dwp 文件将采用 xml 格式

    cls
    
    asnp "*sh*"
    
    $web=Get-SPweb -Identity "http://SP2013dev.com/sites/addwebpart/"
    
    [xml]$webpartxml= Get-Content -Path "C:\Manju\WPRequest.xml"
    
    $SR = New-Object System.IO.StringReader($webpartxml.OuterXml)
    
    $XTR = New-Object System.Xml.XmlTextReader($SR)
    
    $err=$null
    
    $WebPartZoneID = "Topzone"
    
    $WebPartZoneIndex = 0
    
     try
       {
    
      $page=$web.GetFile("Pages/default.aspx");
    
      $bool=$page.CheckedOutBy
    
            if($bool)
            {
                Write-Host "Page is already Checkout to " $page.CheckedOutBy.UserLogin
    
                $page.UndoCheckOut()
    
                Write-Host "Page is Over ridded by " $web.CurrentUser.DisplayName + " to Add Webpart"
            }
    
        $page.CheckOut();
    
        $wmgr=$web.GetLimitedWebPartManager($page,    [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
    
        $webpart=$wmgr.ImportWebPart($XTR,[ref]$err);
    
        $wmgr.AddWebPart($webpart,$WebPartZoneID,$WebPartZoneIndex);
    
        $page.CheckIn('dude');
    
        $page.Publish('Adding request Site Webpart') 
    
        "Request Site WebPart SucessfullAdded" + (Get-Date -DisplayHint Date) | Out-File -Append "C:\OutPutLog.txt" 
    
         $SR.Close();
         $XTR.Close();
         $web.Dispose()
    
        }
        catch
        {
            $ErrorMessage = $_.Exception.Message 
    
            "Request Site WebPart Failure" + $ErrorMessage  +  (Get-Date -DisplayHint Date) | Out-File -Append "C:\ErrorLog.txt"
        }
    

    【讨论】:

      【解决方案3】:

      这取决于我们的要求,但我们可以使用 JavaScript 或其他客户端对象模型库将 Web 部件添加到 SharePoint 页面。我的建议是,即使您的要求指导您编写 PowerShell 脚本,也尽量使用客户端对象模型而不是服务器对象模型。

      如果你想直接从 XML 变量中添加,请注意这一行:

      导入 Web 部件

      $wp = $webpartManager.ImportWebPart($WebPartXml.OuterXml)

      您可以在此处查看更多信息:http://josharepoint.com/2016/02/16/using-powershell-to-add-webpart-to-sharepoint-page-via-csom-in-office-365/

      【讨论】:

        猜你喜欢
        • 2010-12-25
        • 2011-06-13
        • 1970-01-01
        • 2021-10-08
        • 2019-07-13
        • 2021-11-16
        • 2021-04-29
        • 2010-09-19
        • 2012-03-31
        相关资源
        最近更新 更多