none
Updating CEWB content using Client powershell RRS feed

  • Question

  • Hi

    I am trying to update the contents for CEWB using the client powershell but the method save changes is not available in this, what can be an alternate solution this?

    to save the changes or anything else wrong with my script?

    # Begin the process
    $loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
    $loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
    $loadInfo3 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")
    
    
    #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
    }
    
    CLS
    
    # Function to add Site Content link in thes where it does not exists
    
    function addContentCEWB( $data,$imagePath,$CSVData)
    {
      try
         {    
         foreach ($row in $CSVData)  
         {
            $pageURL = $webURL + $row.page
            $file= $spoCtx.Web.GetFileByUrl($pageURL)
            $spoctx.Load($file);
            $spoctx.ExecuteQuery();
    
            if($file.Exists)
            {
                #Web Part Manager to get all web parts from the file
                $limitedWebPartManager = $file.GetLimitedWebPartManager([Microsoft.Sharepoint.Client.WebParts.PersonalizationScope]::Shared);
                $spoctx.Load($limitedWebPartManager.WebParts);          
                $spoctx.ExecuteQuery();
                write-host $limitedWebPartManager.WebParts.AreItemsAvailable;
                write-host $limitedWebPartManager.WebParts.Count;
    
                #Iterate through each web part
                foreach($webPart in $limitedWebPartManager.WebParts) 
                  {
                    $spoctx.Load($webPart.WebPart.Properties) 
                    $spoctx.ExecuteQuery()  
                    $propValues = $webpart.WebPart.Properties.FieldValues  
     
                    # Get the Content Editor web part with specific Title
                    if( ($propValues.Title -eq "addContent") )#-and ($webPart.GetType() -eq [Microsoft.SharePoint.WebPartPages.ContentEditorWebPart]) )
                        {
                        #Content to be Placed inside CEWP
                        $HtmlContent= $data
                        $XmlDoc = New-Object System.Xml.XmlDocument
                        $contentXml=$xmlDoc.CreateElement("content") 
                        $contentXml.InnerText= $HtmlContent
                        #Set content and Save
                        $webPart.WebPart.properties["Content"]  = $contentXml    
                        $limitedWebPartManager.SaveChanges($webPart)
                        #$spoctx.Load($webPart);
                        #$spoctx.ExecuteQuery();
    
                        }
                    }
                  }        
         }
                   
         }
                catch
                {
                    Write-Host("Exception at Site Collection Url :" + $pageURL + $_.Exception.Message)                    
                    $logfile.WriteLine($webURL+"`t"+ $pageURL +"`t"+$_.Exception.Message)              
                }   
        }

    Thanks


    Paru

    Tuesday, March 20, 2018 12:28 PM

All replies

  • Hi,

    I may not directly answer your question, however if you intend to update the CEWP content - you can use different strategy. For example by using Content Link in CEWP - where you can specify external HTML file for content source.


    Best regards,

    Riwut Libinuko
    Principal Architect, Singapore
    Office Servers and Services MVP | Singapore
    Blog : http://blog.libinuko.com

    Tuesday, March 20, 2018 1:12 PM
  • I have to do it on many pages, hence trying to automate it

    Paru

    Tuesday, March 20, 2018 2:03 PM