1

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)
    $Context.Load($items)
    $Context.ExecuteQuery()
    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"]);
            $subcontext.Load($file)
            $subcontext.ExecuteQuery()

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

            $webparts = $webpartManager.Webparts;
            $subcontext.Load($webparts);
            $subcontext.ExecuteQuery();

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

                    $subcontext.Load($webpart);
                    $subcontext.ExecuteQuery();

                    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, privacy policy and cookie policy

Browse other questions tagged or ask your own question.