0

We host a site (one of many, this one is causing unnecessary pain) on SharePoint 2013. I am using a powershell script to try and replace, soon to be invalid, part of the webpart content with an empty string.

I am not sure how to properly use the PowerShell replace with wildcards and between the multiple examples onl-line, have trouble to figure it out. Maybe just tired.

I am going to use one example to demonstrate what I have and the actual result.

Powershell Script:

    cls
Add-PSSnapin "Microsoft.SharePoint.PowerShell"


#Get All Webs (sites)
$webs = Get-SPWebApplication "mysite" | Get-SPSite -Limit All | Get-SPWeb -Limit All
  
 #Iterate through webs
 foreach ($web in $webs)
 {
  #Get All Pages from site's Root into $AllPages Array
  $AllPages = @($web.Files | Where-Object {$_.Name -match ".aspx"})
  
  #Search All Folders for Pages
  foreach ($folder in $web.Folders)
      {
          #Add the pages to $AllPages Array
          $AllPages += @($folder.Files | Where-Object {$_.Name -match ".aspx"})
      }
 
   #Iterate through all pages
   foreach($Page in $AllPages)
   {
        if ($Page.name -inotlike "home*" -and $Page.name -inotlike "dispform*" -and $Page.name -inotlike "editform*" -and $Page.name -inotlike "default*" -and $Page.name -inotlike "allitems*" -and $Page.name -inotlike "newform*" -and $Page.name -inotlike "newsfeed*" -and $Page.name -inotlike "*all *" -and $Page.name -inotlike "how to use*" -and $Page.name -inotlike "myitems*" -and $Page.name -inotlike "active*" -and $Page.name -inotlike "duetoday*" -and $Page.name -inotlike "MyGrTsks*" -and $Page.name -inotlike "GettingStarted*" -and $Page.name -like "Land-Use-Management-and-Sustainability*" )
        {
        write-host $Page.Name
        #Web Part Manager to get all web parts from the page
        $webPartManager = $web.GetLimitedWebPartManager($Page.ServerRelativeUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
 
           #Iterate through each web part
           foreach($webPart in $WebPartManager.WebParts)
           {
                #$webPart.ViewContentTypeId.ToString()
                $ViewContentTypeID = $webPart.Title
                $ViewContent = $webPart.Content.InnerText
                $ViewContentX = $ViewContent -replace '&FolderCTID=.*">','">HHH'
                $IconImagePath = $webPart.CatalogIconImageUrl
                write-host "  -Webpart: $ViewContentTypeID"
                write-host "  -WebpartContent: $ViewContent"
                write-host "  -WebpartContentX: $ViewContentX"
                #write-host "  -CatalogIconImageUrl: '$IconImagePath'"
                #write-host "Webpart: '$webPart.title'"
                #$OldTitle=$webPart.title
                ##Get the Content Editor web part with specific Title
                #if($webPart.title -like "*Crescent Inc*")
                # {
                #      #Replace the Old Title
                #      $webPart.title = $webPart.title.Replace("Crescent Inc.", "Lunar Inc.")
                # 
                #      #Same method goes to update any other custom properties.
                #      #E.g. To update Page viewer web part's link property:
                #      #$webPart.ContentLink = "https://www.sharepointdiary.com"
                #      #To set built-it properties, E.g. To set Set the Chrome type programmatically use:
                #      #$webPart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::TitleAndBorder
                #       
                #      #Save the changes
                #      $webPartManager.SaveChanges($webPart)
                #     
                #      write-host "Updated '$($OldTitle)' on $($web.URL)$($Page.ServerRelativeUrl)"
                # }
           }
      }
  }
}


#Read more: https://www.sharepointdiary.com/2013/08/update-web-part-properties-programmatically.html#ixzz7V2okwR00

What I have:

<p><span><strong class="ms-rteFontSize-3 ms-rteForeColor-1">Related Documentation:</strong><span class="ms-rteFontSize-3 ms-rteStyle-Emphasis"></span><img class="ms-rtePosition-2 ms-
rteStyle-Emphasis" alt="stick_figure_holding_folder_9748.png" src="/PublishingImages/Pages/SHEQ-Policy/stick_figure_holding_folder_9748.png" style="margin: 5px; width: 131px;"/></spa
n></p><p><span class="ms-rteFontSize-2 ms-rteStyle-Emphasis"><span class="ms-rteForeColor-1"><a href="/sites/PMW/Palabora%20SHEQ%20Management%20System/Forms/AllItems.aspx?RootFolder=
%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F02%20Standards%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20Land%20Use%20Management%20and%20Sustainability&amp;FolderCTID=0x01200
0CEE56914143D1444809437B68BE10DBE&amp;View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D">Standards</a></span></span></p><div><a href="/sites/PMW/Palabora%20SHEQ%20Management%20
System/04%20%20Palabora%20Operating%20Procedures/4.6%20SHE%20Standards/4.6.25%20Land%20Use%20Management%20and%20Sustainability"><span class="ms-rteFontSize-2"><em>Palabora Operating 
Procedures</em></span></a></div><div>&#160;</div><p><span class="ms-rteFontSize-2 ms-rteStyle-Emphasis"><span class="ms-rteForeColor-1"><a href="/sites/PMW/Palabora%20SHEQ%20Manageme
nt%20System/Forms/AllItems.aspx?RootFolder=%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F15%20Forms%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20Land%20Use%20Management%20and%
20Sustainability&amp;FolderCTID=0x012000CEE56914143D1444809437B68BE10DBE&amp;View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D">Forms</a></span></span></p><p><span class="ms-rt
eStyle-Emphasis"><span class="ms-rteFontSize-2 ms-rteStyle-Emphasis"><span class="ms-rteForeColor-1"><a href="/sites/PMW/Palabora%20SHEQ%20Management%20System/Forms/AllItems.aspx?Roo
tFolder=%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F20%20Management%20Plan%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20Land%20Use%20Management%20and%20Sustainability&amp;Fo
lderCTID=0x012000CEE56914143D1444809437B68BE10DBE&amp;View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D">Management Plan</a></span></span></span></p><a href="/sites/PMW/Palabor
a%20SHEQ%20Management%20System/Forms/AllItems.aspx?RootFolder=%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F16%20Guidance%20Notes%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20
Land%20Use%20Management%20and%20Sustainability&amp;FolderCTID=0x012000CEE56914143D1444809437B68BE10DBE&amp;View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D"><span class="ms-rt
eFontSize-2"><em>Guidance Notes</em></span></a>

Actual current output:

<p><span><strong class="ms-rteFontSize-3 ms-rteForeColor-1">Related Documentation:</strong><span class="ms-rteFontSize-3 ms-rteStyle-Emphasis"></span><img class="ms-rtePosition-2 ms-
rteStyle-Emphasis" alt="stick_figure_holding_folder_9748.png" src="/PublishingImages/Pages/SHEQ-Policy/stick_figure_holding_folder_9748.png" style="margin: 5px; width: 131px;"/></spa
n></p><p><span class="ms-rteFontSize-2 ms-rteStyle-Emphasis"><span class="ms-rteForeColor-1"><a href="/sites/PMW/Palabora%20SHEQ%20Management%20System/Forms/AllItems.aspx?RootFolder=
%2Fsites%2FPMW%2FPalabora%20SHEQ%20Management%20System%2F02%20Standards%2F4%2E6%20SHE%20Standards%2F4%2E6%2E25%20Land%20Use%20Management%20and%20Sustainability">HHH<span class="ms-rt
eFontSize-2"><em>Guidance Notes</em></span></a>

The replace should actually just replace every occurrence of "&FolderCTID=0x01200 0CEE56914143D1444809437B68BE10DBE&View=%7B53CE6768%2DB28C%2D4261%2D9E86%2D92A5904DE9CD%7D" with "". Currently it seems like it starts at the first occurrence and replace everything between it and the last occurrence.

0

1 Answer 1

1

Strangely it seems to be working ok for me!

But the issue may be due to regex and how it does lazy/greedy matching.

I cant test it, but try changing the '&amp;FolderCTID=.*">' to '&amp;FolderCTID=.*?">'

This will then match your pattern until the next occurence, not to the last occurence.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.