Tuesday 22 September 2015

Find the url or text on the publishing pages with in a SharePoint site or site collection

.bat file to run the below powershell script

cd /d %~dp0
powershell -noexit -file    ".\Textfind_publishingpages.ps1" "%CD%"
pause

.ps1

# Add SharePoint cmdlets reference
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

function enumerateWebParts($Url) {
    $site = new-object Microsoft.SharePoint.SPSite $Url

    foreach($web in $site.AllWebs) {
       
        if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) {
            $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
            $pages = $pWeb.PagesList

            #Write-Host "Processing Web:" $pWeb.Url "..." -ForegroundColor Magenta
$webUrl = $pWeb.Url;

            foreach ($item in $pages.Items) {

                $fileUrl = $webUrl + "/" + $item.File.Url
                #Write-Host "   " $fileUrl -ForegroundColor Green
        try
        {               
            $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
        }
        catch [Exception]
        {
            $fileUrl = $webUrl + "/" + $item.File.Url
            #write-host "error occured processing the page     $fileUrl"       
            continue;
        }   
                $wps = $manager.webparts
        $weburll = $pWeb.Url;


        foreach ($webpart in $wps){
              $type = $webpart.GetType().Name;
            if ($type -eq ‘SummaryLinkWebPart’){
                #Write-host "$fileUrl" -ForegroundColor Green;;
                foreach ($summarylink in $webpart.SummaryLinkValue.SummaryLinks){
                    $sumurl = $summarylink.LinkUrl;
                    #write-host "$sumurl"  -ForegroundColor Red;
$teststr = "http://test.com/"
                    if($sumurl.ToLower() -eq $UrltoSearch){
                    #$resul = $sumurl.Contains($UrltoSearch);
                    #if($resul -eq "True"){
                        write-host "Summary Links Webpart - $fileUrl" -ForegroundColor Green;
                    $row | select-object @{Expression={$fileUrl};Label="Page URL"}
                    }
                }
            }
       



#end of checking summary links webpart
#checking content editor webpart
if ( $webpart.WebBrowsableObject.ToString() -eq "Microsoft.SharePoint.WebPartPages.ContentEditorWebPart" )
            {

if  ($webpart.contentlink.length -gt 0 )
                {
                  #Write-Host "         content: HAS <SCRIPT>"  -foregroundcolor red

# write-host $webpart.content."#cdata-section" -foregroundcolor green

$xmlDoc = New-Object -TypeName xml
$xmlElement = $xmlDoc.CreateElement("MyElement");

# THIS IS WHERE THE REPLACE IS DONE.
$textcompare = $webpart.Content.InnerText;

$res = $textcompare -match $UrltoSearch
if($res -eq "True")
{
#write-host "$weburll" -ForegroundColor Magenta;
write-host "Content Editor - $fileUrl" -ForegroundColor Green;
$row | select-object @{Expression={$fileUrl};Label="Page URL"}
}



                }

}
}
#end of checking content editor
             

            }
        }
   

#check for hyperlinks fields in the web


$result = @() #Edited
# Iterate lists
foreach ($list in $web.Lists) {
    # First find all HyperLink fields in list
    $fields = @()
    foreach ($field in $list.Fields) {
        if ($field.TypeAsString -eq "URL") {
            $fields = $fields + $field.Title       
        }
    }

    # Skip list because there are no Hyperlink fields
    if($fields.Count -eq 0) { continue }
    # Iterate list items NOTE: this can be slow
    foreach ($item in $list.Items) {
        # Iterate HyperLink fields
        foreach ($field in $fields) {
            # Get field value
            $fldValue = $item[$field]
            # Skip if null
            if($fldValue -ne $null) {
                # Check for http: https: or file:
                if($fldValue.Contains($UrltoSearch)) {
                    write-host "List Name : $list";
            write-host "Site url - $webUrl";
            $row | select-object @{Expression={$fileUrl};Label="Page URL"}
                }
            }
        }
    }
}
#$result | Out-File file_links.txt #Edited

#end of code for hyperlink fields
    $web.Dispose()
    }

 }
$webAppUrl = Read-Host "Enter the web application URL for ex:weew.wewe.com"
$UrltoSearch = Read-Host "Enter the URL to search"
$outputfile = "c:\matchingrecords.csv"

$row = enumerateWebParts $webAppUrl $UrltoSearch

$row | Out-File $outputfile;

No comments:

Post a Comment