0

I write the powershell script below to check if a webpart exists on some page on all mysites. I got an exception on the root site collection of the mysite webapplication because the page does not exists.

How to avoid this exception? I would like to check for example if the page exists but it looks like it dont work. I check already if the variable $page is not null.

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA silentlycontinue

$webAppUrl = "http://MyWebApplication.com"
$spWebApp = Get-SPWebApplication $webAppUrl

foreach($SpSite in $spWebApp.Sites)         
{  
        $resultsUrl = $SpSite.Url +"/Social/Sites.aspx"
        $page = $SpSite.RootWeb.GetFile($resultsUrl);

        if($page -ne $null)
        {
            # Get the web part manager for the page
            $webPartManager = $SpSite.RootWeb.GetLimitedWebPartManager($resultsUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

            # And pull the web part we want to work on
            $webpart = $webpartmanager.WebParts | ? { $_.GetType().Name -eq 'MyCustomWebpart' }
            if($webpart -ne $null)
            {
                "Found webpart '" + $webpart.Title + "' " + "on page $resultsUrl"
            }
        }

    $SpSite.RootWeb.Dispose();
    $SpSite.Dispose();
}
1

It's very simple, you have to do it like this:

$page = $SpSite.RootWeb.GetFile($resultsUrl);
if ($page.FileExists)
{
}

Please refer to this site. It only works if you used "GetFile" before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.