0

First off let me start by saying this is the first time I have posted a question here, I often visit this website and other StackExchange/Overflow sites for assistance on various projects and assignments. Hopefully I have given enough detail below for someone to help me out however please let me know if you need anything else.

I am trying to update a large number (about 6500) of publishing pages across 20-30 webs within an application that we have. I have copied existing code that I have used many times before which successfully edits pages, the only difference is that this time round I am getting the content from the file system rather than getting it from SharePoint, modifying it directly within PowerShell and putting it back in.

The changes to the PublishingPageContent field are not getting updated and revert back to it's previous value before getting checked back in and published.

ForEach ($Item in $ListItems)
{
    $ItemPath = $PageDirectory + $SubWebUrl2 + "/" + $Item.Url
    $ItemPath = $ItemPath -replace "/","\"

    $ItemFile = $Item.File

    if($ItemFile.CheckOutStatus -ne "None")
    {
        Write-Host "First Checkin.. " -NoNewLine
    $ItemFile.CheckIn("Automatic CheckIn. (Administrator)")
    }

    Write-Host "Checkout.. " -NoNewLine
    $ItemFile.CheckOut()

    Write-Host "Updating page.. " -NoNewLine
    $ItemFile.Properties.PublishingPageContent = Get-Content $ItemPath | Out-String
    $ItemFile.Update()

    Write-Host "Second Checkin.. " -NoNewLine
    $ItemFile.CheckIn("Automatic CheckIn. (Administrator)")
}

And this is the output of a single item

Checkout.. Updating page.. Update : Exception calling "Update" with "0" argument(s): "Only String, INT, and DateTime datatypes can be used as the value in Properties."
At C:\Workspace\Step4ImportPages.ps1:48 char:19
  +         $ItemFile.Update <<<< ()
  + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  + FullyQualifiedErrorId : DotNetMethodException
Second Checkin.. Publishing.. Approving.. PS C:\Workspace>

Unfortunately I am unable to provide a complete example of the content being imported, however I can say that it is just plain HTML being stored in ASPX files purely for convenience so that the file system structure replicates what is in SharePoint. The Get-Content $ItemPath command is successfully getting the HTML, here is a snippet

<H3>&nbsp;</H3><A name=LPTOC1></A>
<H3 class=Level2>
<DIV style="TEXT-ALIGN: right"><A name=JD_MSI247-ATTACHMENT1></A><SPAN style="FONT-WEIGHT: bold">ATTACHMENT 1</SPAN></DIV></H3>
<P>&nbsp;</P>
1

Ok I found the solution.

My issue was that the Get-Content cmdlet was creating an array of strings for each line. Advice I found on the internet suggested that piping the output of Get-Content to Out-String would consolidate this into a single string. Out-String does not actually perform this behaviour and as such the datatype of String[] was not being accepted even though I assumed (based on the advice) that it would have been consolidated to just String.

As I am using SharePoint 2010 which requires the use of PowerShell 2.0, I was also unable to utilise the Get-Content parameter -Raw which was introduced in PowerShell 3.0. To get around this I ended up having to strongly typecast the property to get it to work.

Broken Line:

$ItemFile.Properties.PublishingPageContent = Get-Content $ItemPath | Out-String

Solution (strongly typecasted):

[string]$ItemFile.Properties.PublishingPageContent = Get-Content $ItemPath
0

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.aspx

as per this above link, you can set any property on SPListItem and data types are mentioned which are supported. Try it yourself and post it here if you still get any issue, below is an example you can try (suppose your SPListItem object is oItem) both ways

oItem["ProductAmount"] =1200;

OR

oItem["ProductAmount"] ="1200";

When we work on publishing pages, we have to take care of update commands in power shell.

if($Item.Exists -ne $false) { #Checkout the page $Item.CheckOut() #Update Item Property $Item["Title"] = "Title Updated"; #Check in and Publish the page $Item.CheckIn("Property Updated") $Item.Publish("Property Updated") } $spWeb.Update(); #Dispose SPWeb object $spWeb.Dispose();

  • Thanks for your response, unfortunately this does not even come close to resolving my issue. I know how to update ListItem properties including using the Update method, as I mentioned I have successfully done so in the past. The difference here is that I am getting HTML from flat files and trying to overwrite the content of their respective SharePoint pages with this HTML. I have read that Get-Content creates an array of strings (ie. one index position per line) therefore piping the array to Out-String should create a single string. I am not sure if this is actually happening or not. – Owarya Mar 2 '15 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.