locked
How to update content of Microsoft.SharePoint.Publishing.Fields.HtmlField using SharePoint Powershell RRS feed

  • Question

  • Hi,

    I need to update content of Publishing HtmlField using PowerShell command. I tried to run the following PowerShell command:

    [Microsoft.SharePoint.Publishing.Fields.HtmlField]$fld =                                                 [Microsoft.SharePoint.Publishing.Fields.HtmlField]$lstItem.Fields.GetFieldByInternalName($colName)

            $val = $lstItem[$colName]
            $val = $val.Replace($oldValue,$newValue)
            $fld.ParseAndSetValue($lstItem,$val);

           $lstItem.SystemUpdate($false);

    After running this command content of this field is distorted and renders the content in text format instead of html format.

    Please suggest the way how to update this field value without affecting format.

    Thanks

    Tuesday, April 9, 2019 3:34 PM

All replies

  • Hi,

    Please provide more information about the Publishing HTML field for further research. Like, it is a OOTB column or a custom column.

    In my test, I update the content of the "Page Content"(internal name is "PublishingPageContent") field in Pages library using the PowerShell below and it working fine.

    $siteUrl="http://sp2013/sites/publish"
    $listName="Pages"
    $listItemId=1
    $colName="PublishingPageContent"
    $oldValue="Check for"
    $newValue="Check for Dennis"
    $web = Get-SPWeb -Identity $siteUrl
    $list = $web.Lists[$listName]
    $lstItem=$list.GetItemById($listItemId)
    [Microsoft.SharePoint.Publishing.Fields.HtmlField]$fld =[Microsoft.SharePoint.Publishing.Fields.HtmlField]$lstItem.Fields.GetFieldByInternalName($colName)
    $val = $lstItem[$colName]
    $val = $val.Replace($oldValue,$newValue)
    $fld.ParseAndSetValue($lstItem,$val);
    $lstItem.SystemUpdate($false);

    Best Regards,

    Dennis


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

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, April 10, 2019 3:30 AM
  • Hi Dennis,

    It is OOTB column not custom column and used the same powershell command which you mentioned to update content of HtmlField. But page is looking like this one after SystemUpdate($false):

    Thanks

    Wednesday, April 10, 2019 12:19 PM
  • Hi,

    Did you also update the "Page Content" field?

    Or provide the complete code in use if possible for further research.

    Best Regards,

    Dennis


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

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, April 11, 2019 7:29 AM
  • Hi Dennis,

    Yes, I am also updating "Page Content" field. I am using the following code:

    [Microsoft.SharePoint.Publishing.Fields.HtmlField]$fld = [Microsoft.SharePoint.Publishing.Fields.HtmlField]$lstItem.Fields.GetFieldByInternalName($colName)
    
            $val = $lstItem[$colName]
            $val = $val.Replace($oldUrlValue,$newUrlValue)
            $fld.ParseAndSetValue($lstItem,$val);
    
            $lstItem.SystemUpdate($false);

    Thanks

    Regards

    Vipin

    Friday, April 12, 2019 9:11 AM
  • Hi Vipin,

    Please create a new simple page, and replace a simple text of in "Page Content" field using the PowerShell above to check if it works.

    Best Regards,

    Dennis


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

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, April 15, 2019 7:59 AM