I have a requirement where I need to find if there is a table in the pages on my website. I thought of writing a PowerShell script for getting all the webparts in the page and reading the content inside the webpart and then find if there is a table in the content.

My script goes like this:

function GetClientContext([string]$Url, [string]$UserName, [securestring]$Password) {
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url);
    $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password);
    return $context;

function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
    $list = $Context.Web.Lists.GetByTitle($listTitle)
    $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    $items = $list.GetItems($qry)
    return $items 

#get all pages from Pages List
        $subcontext = GetClientContext $childWeb.url $username $password
        $allPages = Get-ListItems -Context $subcontext -ListTitle "Pages" 

        foreach ($page in $allPages) {
            Write-Host "PageTitle:" $page["Title"];
            Write-Host "PageRef:" $page["FileRef"];

            $file = $subcontext.Web.GetFileByServerRelativeUrl($page["FileRef"]);

            $webpartManager = $file.GetLimitedWebPartManager([Microsoft.Sharepoint.Client.WebParts.PersonalizationScope]::Shared);

            $webparts = $webpartManager.Webparts;

            if ($webparts.Count -gt 0) {  
                Write-Host "Looping through all webparts";
                foreach ($webpart in $webparts) {


                    if (($webpart.Content.InnerText) -ne $null) {
                        if ($webpart.Content.InnerText.Contains("<table")) {
                            Write-Host "Contains Table here";

But here, I'm unable to access the $webpart.Content.InnerText. I could make this script work in 2013 but not now in Office 365 SharePoint Online.

Please let me know if accessing the content is possible or any other way of doing this.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.