locked
Accessing webparts of a page by powershell doesn't retrieve any properties RRS feed

  • Question

  • Hi,

    I'm currently trying to list all the webparts of a page which are closed or in an "error state".
    For this, I use a powershell script that does actually work since I'm able to populate a variable with all the webparts collection.

    The issue is that I can't retrieve the Title or any other properties except the ID. See below the output :

     

    ErrorMessage                       : This page has encountered a critical error. Contact your system administrator if this problem persists
                                         .
    ErrorDetails                       :
    RepresentedWebPartType             :
    FatalError                         : True
    ErrorType                          : Unsafe
    Title                              :
    FrameType                          : Default
    SuppressWebPartChrome              : False
    Description                        :
    IsIncluded                         : True
    ZoneID                             : Header
    PartOrder                          : 2
    FrameState                         : Normal
    Height                             :
    Width                              :
    AllowRemove                        : True
    AllowZoneChange                    : True
    AllowMinimize                      : True
    AllowConnect                       : True
    AllowEdit                          : True
    AllowHide                          : True
    IsVisible                          : True
    Hidden                             : False
    CatalogIconImageUrl                :
    TitleIconImageUrl                  :
    TitleUrl                           :
    DetailLink                         :
    HelpUrl                            :
    HelpLink                           :
    HelpMode                           : Modeless
    Dir                                : Default
    PartImageSmall                     :
    MissingAssembly                    : Cannot import this Web Part.
    PartImageLarge                     :
    IsIncludedFilter                   :
    AuthorizationFilter                :
    ExportControlledProperties         : True
    ConnectionID                       : 00000000-0000-0000-0000-000000000000
    ID                                 : g_4672c1d9_9246_438a_b1d6_99d80e697291
    StorageKey                         : 4672c1d9-9246-438a-b1d6-99d80e697291
    Connections                        :
    UnknownXmlElements                 : {}
    SerializedAttachedPropertiesShared :
    SerializedAttachedPropertiesUser   :
    UseDefaultStyles                   : True
    Qualifier                          :
    ClientName                         : varPart
    Caption                            :
    Subtitle                           :
    Permissions                        : AllProperties
    BrowserDesignMode                  : False
    EffectiveTitle                     : Untitled
    EffectiveStorage                   : Shared
    EffectiveFrameType                 : Standard
    AllowClose                         : True
    ChromeState                        : Normal
    ChromeType                         : Default
    ConnectErrorMessage                :
    Direction                          : NotSet
    DisplayTitle                       : Untitled
    ExportMode                         : All
    HasUserData                        : False
    HasSharedData                      : False
    IsClosed                           : False
    ImportErrorMessage                 : Cannot import this Web Part.
    IsShared                           : True
    IsStandalone                       : False
    IsStatic                           : False
    Verbs                              : {}
    WebBrowsableObject                 : Microsoft.SharePoint.WebPartPages.ErrorWebPart
    Zone                               :
    ZoneIndex                          : 2
    Controls                           : {}
    BackImageUrl                       :
    DefaultButton                      :
    GroupingText                       :
    HorizontalAlign                    : NotSet
    ScrollBars                         : None
    Wrap                               : True
    AccessKey                          :
    Attributes                         : System.Web.UI.AttributeCollection
    BackColor                          : Color [Empty]
    BorderColor                        : Color [Empty]
    BorderWidth                        :
    BorderStyle                        : NotSet
    ControlStyle                       : System.Web.UI.WebControls.PanelStyle
    ControlStyleCreated                : True
    CssClass                           :
    Style                              : System.Web.UI.CssStyleCollection
    Enabled                            : True
    EnableTheming                      : True
    Font                               :
    ForeColor                          : Color [Empty]
    HasAttributes                      : False
    SkinID                             :
    TabIndex                           : 0
    ToolTip                            :
    ClientID                           : g_4672c1d9_9246_438a_b1d6_99d80e697291
    EnableViewState                    : True
    NamingContainer                    :
    BindingContainer                   :
    Page                               :
    TemplateControl                    :
    Parent                             :
    TemplateSourceDirectory            :
    AppRelativeTemplateSourceDirectory :
    Site                               :
    Visible                            : True
    UniqueID                           : g_4672c1d9_9246_438a_b1d6_99d80e697291

     

    As you can see, the error seems to be : "This page has encountered a critical error. Contact your system administrator if this problem persists".

    I tried in Full Trust level, farm account, with GetWebPartManager function without success.


    Thanks in advance for any help !

    Wesley.

     

    The Powershell script is this one : (taken from this site : http://briantjackett.com/archive/2010/11/11/powershell-script-to-find-all-closed-web-parts-on-sharepoint.aspx )

     

    function Search-SPClosedWebParts
    {
    <#
     .Synopsis
      Finds instances of closed web parts on SharePoint site and sub-sites.
     .Description
      Finds instances of closed web parts on SharePoint site and sub-sites.
     .Parameter
      SiteUrl
      SiteUrl to search for closed web parts
     .Example
      Search-SPClosedWebParts
      Finds instances of closed web parts on SharePoint site and sub-sites.
    #>
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, valueFromPipeline=$true)]
        [String]
        $SiteUrl
    )#end param
        Process
        {
            $site = new-Object Microsoft.SharePoint.SPSite($SiteUrl)

            $site.AllWebs | ForEach-Object {
                $currentWeb = $_
                $pages = $currentWeb.Files | Where-Object {$_.Name -match ".aspx"}

                $pages | ForEach-Object {
                    $currentPage = $_
        
                    $webPartManager = $currentWeb.GetLimitedWebPartManager($currentPage.ServerRelativeUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

                    $webPartManager.WebParts | ForEach-Object {
                        if($_.IsClosed)
                        {
                            Write-Host "'$($_.Title)' on $($currentPage.ServerRelativeUrl) is closed"
                        }
                    }
                }
               
                $currentWeb.Dispose()
            }
           
            $site.Dispose()
        }
    } #end Search-SPClosedWebParts

    $siteUrlToSearch = Read-Host -Prompt "Enter site URL to check for closed web parts"

    Search-SPClosedWebParts -SiteUrl $siteUrlToSearch

    Wednesday, June 1, 2011 11:34 PM

All replies

  • Hi Wesley,

     

    I have encountered this issue before and maybe you can solve this problem by following these steps:

    1.       Do an IIS reset and check.

    2.       Check all the webpart’s safe control entries.

    3.       Replace the web.config file with the backed up web.congif file.

     

    If the problem persist after you did all the things above, you can go through these two threads to see if your problem is similar with any of them:

    http://social.technet.microsoft.com/Forums/en-US/sharepointgeneral/thread/82470708-b324-42ed-988f-07fdd094decd/;

    http://blog.dennus.net/2009/06/29/webpartpageuserexception-this-page-has-encountered-a-critical-error/.

     

    Thanks & Regards,

    Peng Lei

    • Marked as answer by David HM Wednesday, June 8, 2011 9:02 AM
    • Unmarked as answer by Wesley De Arburn Wednesday, June 8, 2011 11:52 AM
    Friday, June 3, 2011 5:11 AM
  • Hi Peng Lei,

     

    I appreciate the reply but unfortunately, none of the proposed steps fixed the issue.

    I also find out that the Gary Lapointe's STSADM commands has the same behavior. I f I run stsadm -o gl-enumpagewebparts, the output doesn't shows the title of the webparts.

    As our farm is heavily customised, I guess something went wrong with a solution that created this behavior but I'm unable to find out why.
    This is part of the clean up process following the preupgradecheck report in which we have thousans of "webparts referenced by the content but not installed on the server".
    Another issue we have about this (but this should be another post I suppose )is the fact that after running the powershell script to delete all closed/error webparts through out the sites, we still have many references of missing webparts in pages such as "EditForm.aspx". The weird thing is that if I go in the webpart maintenance page (?contents=1), I don't see any webparts listed here thought the preupgradecheck report tells me the contrary.

    Any thoughts ?

    Regards,

    Wesley

    Wednesday, June 8, 2011 12:03 PM
  • Hi Wesley

    You might want to try this modification which provides an HTTP context to the webpart manager:

    function Search-SPClosedWebParts
    {
    <#
     .Synopsis
      Finds instances of closed web parts on SharePoint site and sub-sites.
     .Description
      Finds instances of closed web parts on SharePoint site and sub-sites.
     .Parameter
      SiteUrl
      SiteUrl to search for closed web parts
     .Example
      Search-SPClosedWebParts
      Finds instances of closed web parts on SharePoint site and sub-sites.
    #>
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, valueFromPipeline=$true)]
        [String]
        $SiteUrl
    )#end param
        Process
        {
            #Variables to provide HTTP context
            [Boolean]$isContextNull = $false;
            [System.Web.HttpContext]$htc = $null;
            [System.IO.StringWriter]$sw = $null;
            [System.Web.HttpResponse]$resp = $null;
            [System.Web.HttpRequest]$req = $null;
    
            $site = new-Object Microsoft.SharePoint.SPSite($SiteUrl)
    
            $site.AllWebs | ForEach-Object {
                $currentWeb = $_
    
                # set the current httpcontext, needed for contentquerywebparts webparts that use the
                # xslitemlink, xslmainlink or xslheaderlink property
                # else makeserverrelative url will fail, because there no context
                if ($null -eq [System.Web.HttpContext]::Current){
                    Write-Debug "Setting HttpContext";
                    $isContextNull = $true;
                    $sw = New-Object System.IO.StringWriter;
                    $resp = New-Object System.Web.HttpResponse $sw;
                    $req = New-Object System.Web.HttpRequest "", $currentWeb.Url, "";
                    $htc = New-Object System.Web.HttpContext $req, $resp;
                    #explicitly cast $web to spweb object else sharepoint will
                    #see it as a PSObject
                    $htc.Items["HttpHandlerSPWeb"] = $currentWeb -as [Microsoft.SharePoint.SPweb];
                    [System.Web.HttpContext]::Current = $htc;
                    if ($sw -ne $null){
                        $sw.Dispose()
                    }
                } else {
                    Write-Debug "HttpContext already set";
                }
                
                $pages = $currentWeb.Files | Where-Object {$_.Name -match ".aspx"}
    
                $pages | ForEach-Object {
                    $currentPage = $_
        
                    $webPartManager = $currentWeb.GetLimitedWebPartManager($currentPage.ServerRelativeUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
    
                    $webPartManager.WebParts | ForEach-Object {
                        if($_.IsClosed)
                        {
                            Write-Host "'$($_.Title)' on $($currentPage.ServerRelativeUrl) is closed"
                        }
                    }
                }
    
                if($isContextNull){
                    Write-Debug "Resetting HttpContext";
                    [System.Web.HttpContext]::Current = $null;
                }
               
                $currentWeb.Dispose()
            }
           
            $site.Dispose()
        }
    } #end Search-SPClosedWebParts

    I think I found this on Jeroen Derde's Blog.

    Regards,

    Mathias

    Monday, June 11, 2012 2:27 PM