none
Fing an image path on the Page using powershell RRS feed

  • Question


  • I am trying to find an Image on a SharePoint wiki page, where there is no web part added on the page and directly html is placed in the pace holder.

    I have extracted the image from the content editor webparts but i also need the image if it is directly put on the page in html, below is my code and i want to add this condition too

    Please help!

    Thanks

     cls
    #Add SharePoint PowerShell SnapIn if not already added
    $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} 
    
    if ($snapin -eq $null) 
    
    {    
    
        Write-Host "Loading SharePoint Powershell Snapin"    
    
        Add-PSSnapin "Microsoft.SharePoint.Powershell"  -EA SilentlyContinue
    
    }   
    
    try
    {
    
        cls
    $StartTime = $(get-date -f F)
    $timeStamp = Get-Date -format "MM_dd_yy_hh_mm"
    #Get Current folder file path
    $invocation = (Get-Variable MyInvocation).Value
    $currentPath = Split-Path $invocation.MyCommand.Path 
    $currentPath = $currentPath + "\"
    
    #Config File Path
    $configPath = $currentPath + "Config.xml"
    #fetching details from config.xml
    [xml]$configXML = Get-Content $configPath
    
    
    $inputFileName = [string]$configXML.Config.Constants.InputFileName
    $errorFileName = [string]$configXML.Config.Constants.ErrorFileName
    $outFilePath = [string]$configXML.Config.Constants.OutputFileName
    $extn =[string]$configXML.Config.Constants.Extension;
    
    #Source File path containing list of WebApplications in a farm. 
    $siteCOllFilePath = $currentPath + $inputFileName
    
    #Output File path of the exported User Permission. 
    $outfile = $currentPath + $outFilePath
    
    #File path of the file which will capture all the errors while running the script. 
    $errorPath = $currentPath + $errorFileName + $timeStamp + ".csv"
    
    # Creating object to write logging into the error and output file
    $LogFileName = New-Object System.IO.StreamWriter  $outfile
    $ErrorLogFileName = New-Object System.IO.StreamWriter $errorPath
    
    $LogFileName.WriteLine("No." +"`t"+"SiteCollUrl" +"`t"+"WebUrl" +"`t"+"Page URL"+"`t" +"Image Path");
    $ErrorLogFileName.WriteLine("SiteCollUrl" +"`t"+"ExcepLevel" +"`t"+"WebUrl" +"`t"+"ExceptionMsg");
    
    # Fetching SharePoint WebApplications list from a CSV file
    $CSVData = Import-CSV -path $siteCOllFilePath 
    
        $count = 0;
       #Iterates though all the site collections in Web appliation
           try
        {
    
          foreach ($row in $CSVData)  
         {
         if($row -ne $Null -and $row -ne '')
            {            
                     $spSiteCollection =  $row.SiteCollURL
    
                     $MySiteCollection=Get-SPsite $spSiteCollection
                     #$web=$MySiteCollection.OpenWeb()
    
      [Microsoft.Sharepoint.SPSecurity]::RunWithElevatedPrivileges({
           try
           {
               #$siteUrl = $_.Url
                 foreach ($siteColl in $MySiteCollection) 
                   {
                        foreach($web in $siteColl.AllWebs)
                        {
                            if($web -ne $null)
                            {
    
                       Write-Host "Checking web : "$web.Url -ForegroundColor Magenta
    
    $path = "/_layouts/images/"
    
    $AllPages = @($web.Files | Where-Object {$_.Name -match ".aspx"}) 
    #Search All Folders for Pages
    foreach ($folder in $web.Folders)
        {
        #Write-Host "folder : "$folder
        if($folder.Name -ne "Forms") #Leave "Forms" Folder
            {
             #Add the pages to $AllPages Array
             $AllPages += @($folder.Files | Where-Object {$_.Name -match ".aspx"})
    
         } #if
    
        } #foreach
    
     #Iterate through all pages
    
     foreach($Page in $AllPages)
      {
        Write-Host "Page : "$Page
        $pageUrl = $web.Url + "/"+ $Page
        Write-Host "pageUrl : "$pageUrl
    
    
         #Web Part Manager to get all web parts from the file
    
         $WebPartManager = $web.GetLimitedWebPartManager( $Page.ServerRelativeUrl,  [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
         #Iterate through each web part
    
         foreach($webPart in $WebPartManager.WebParts)  
          {
    
            if( ($webPart.Content.InnerText -like '*'+$path +'*' ) -and ($webPart.GetType() -eq [Microsoft.SharePoint.WebPartPages.ContentEditorWebPart]) ) 
            {
    
             $count += 1;
    
               $WebpartContent =  $webPart.Content
               $WebpartContentXml = $WebpartContent.InnerText
    
               #$ImagePath = $WebpartContentXml.SubString($WebpartContentXml.IndexOf("_") - 1, $WebpartContentXml.LastIndexOf(".") - $WebpartContentXml.IndexOf("_") + 5)
    
               $ImagePath = [regex]::Matches($WebpartContentXml, '(_layouts[^"]+)') | ForEach-Object {"/"+ $_.Groups[1].value +","}       
               Write-Host "Image Path :  " $ImagePath;
               Write-Host "Webpart Content :  "$WebpartContentXml;
               Write-Host "Page URL :  "$Page.Url;
               #Write-Host "WebPart Type :  "$webPart.GetType();
               $LogFileName.WriteLine($count.ToString()+"`t"+$siteColl+"`t"+$web+"`t"+$pageUrl +"`t"+$ImagePath);
    
             }#if
    
    
    
    ##############################################
    
    
    
    ##############################################
    
          }#foreach
    
        } #foreach
    
    
                  } #if
    
            } #foreach2
        } #foreach1
      } #try3   
                   catch
                   {
                       Write-Host "Exception at web Level" $_.Exception.Message -ForegroundColor Red
                       $ErrorLogFileName.WriteLine($siteColl+"`t"+"WebLevel"+"`t"+$web.Url+"`t"+$_.Exception.Message);
                   }
    
                 }) 
              } #foreach  
           } #try2
          } 
           catch
           {
               Write-Host "Exception at web Level" $_.Exception.Message -ForegroundColor Red
               $ErrorLogFileName.WriteLine($siteColl+"`t"+"SCLevel"+"`t"+$web.Url+"`t"+$_.Exception.Message);
           }
    
    
        }
    catch
    {
        write-host "Exception OcurCed in current script" $_.Exception.Message
    }
    finally
    {
        $LogFileName.Close()
        $ErrorLogFileName.Close()
        Write-Host "Start Time: " $StartTime -ForegroundColor Green
        $EndTime = $(get-date -f F)
        Write-Host "End Time: " $EndTime -ForegroundColor Green
    
        $TimeSpan = NEW-TIMESPAN –Start $StartTime –End $EndTime
        Write-Host "Duration: " $TimeSpan -ForegroundColor Green
    } `


    Paru

    Tuesday, May 23, 2017 10:43 AM

Answers

  • Hi Paru,

    We can get the .aspx page file content as string using SPWeb.GetFileAsString method to achieve it.

    SPWeb.GetFileAsString method: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getfileasstring

    And then find the <img/> tag using regular expression.

    Here is an example using C# code for your reference:

    public static string[] GetHtmlImageUrlList(string sHtmlText)
    {
    			   
    	Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
     
    	MatchCollection matches = regImg.Matches(sHtmlText);
    	int i = 0;
    	string[] sUrlList = new string[matches.Count];            
    	foreach (Match match in matches)
    	sUrlList[i++] = match.Value;
    	return sUrlList;
    }
    
    static void Main(string[] args)
    {
       
    	using (SPSite site = new SPSite("http://sp2010/sites/team"))
    	{
    		using (SPWeb web = site.OpenWeb())
    		{                 
    			var fileString = web.GetFileAsString("http://sp2013/sites/team/SitePages/Test524.aspx");
    			var images = GetHtmlImageUrlList(fileString);                 
    		}
    	}
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    Wednesday, May 24, 2017 12:39 PM
    Moderator

All replies

  • Hi Paru,

    We can get the .aspx page file content as string using SPWeb.GetFileAsString method to achieve it.

    SPWeb.GetFileAsString method: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getfileasstring

    And then find the <img/> tag using regular expression.

    Here is an example using C# code for your reference:

    public static string[] GetHtmlImageUrlList(string sHtmlText)
    {
    			   
    	Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
     
    	MatchCollection matches = regImg.Matches(sHtmlText);
    	int i = 0;
    	string[] sUrlList = new string[matches.Count];            
    	foreach (Match match in matches)
    	sUrlList[i++] = match.Value;
    	return sUrlList;
    }
    
    static void Main(string[] args)
    {
       
    	using (SPSite site = new SPSite("http://sp2010/sites/team"))
    	{
    		using (SPWeb web = site.OpenWeb())
    		{                 
    			var fileString = web.GetFileAsString("http://sp2013/sites/team/SitePages/Test524.aspx");
    			var images = GetHtmlImageUrlList(fileString);                 
    		}
    	}
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    Wednesday, May 24, 2017 12:39 PM
    Moderator
  • Thanks so much! this works, 

    now i just have to get the pages in the SharePoint site which do not have web-part an directly html is placed,

    i am getting the HTML of pages which has web parts as of now with this code.

    Thanks


    Paru

    • Marked as answer by Paru Upreti Friday, May 26, 2017 4:12 AM
    • Unmarked as answer by Paru Upreti Friday, May 26, 2017 4:12 AM
    Thursday, May 25, 2017 2:01 PM
  • Hi Paru,

    If my reply helps you, please mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, May 26, 2017 1:21 AM
    Moderator