1

I am trying to create about 1000 publishing pages using powershell. The powershell script reads data from a xml file. I have to update Modified By field for all newly created pages. I have used

$item["Editor"] = $user
$item[[Microsoft.SharePoint.SPBuiltInFieldId]::Modified_x0020_By] = $user

and tested all following options for updating the item.

$item.Update()
$item.SystemUpdate($true)
$item.SystemUpdate($false)
$item.UpdateOverwriteVersion()

but When I go to the pages list and, all pages is updated by user who executed the powershell. Updated: added Code

$myWeb =  Get-SPWeb $WebUrl
$myPublishWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($myWeb)
$pagesListName = $myPublishWeb.PagesListName

$newFile = $myweb.GetFile("$pagesListName/temp.aspx")
if (-not($PersonaltidningspolicyFile.Exists)) 
{
  $myPage = $myPublishWeb.AddPublishingPage("temp.aspx", $myPageLayout)
  $myPage.Title = "Title Value"
  $myItem = $myPage.ListItem 


  $modifiedBy = $myWeb.EnsureUser("domain\testuser")
  $modifiedByUserFieldValue = New-Object Microsoft.SharePoint.SPFieldUserValue($myWeb, $modifiedBy.ID, $modifiedBy.Name)
  $pagesList = $myPublishWeb.PagesList

  $myItem["Title"] = "Title Value"

  $pagesList.EnableMinorVersions = $false
  $pagesList.Update()
  $myItem[[Microsoft.SharePoint.SPBuiltInFieldId]::Modified_x0020_By] = $modifiedByUserFieldValue
  $myItem[[Microsoft.SharePoint.SPBuiltInFieldId]::Editor] = $modifiedByUserFieldValue
  $myItem[[Microsoft.SharePoint.SPBuiltInFieldId]::Modified] = $oldDate
  $myItem["Modified"] = $oldDate

  $myItem.UpdateOverwriteVersion()          
  $pagesList.EnableMinorVersions = $true
  $pagesList.DraftVersionVisibility = [Microsoft.SharePoint.DraftVisibilityType]::Reader
  $myPublishWeb.Update()

  if($newFile.CheckOutStatus -ne [Microsoft.SharePoint.SPFile+SPCheckOutStatus]::None)
  {
        $newFile.CheckIn("From Powershell")
        $newFile.Publish("frrom Powershell")
  }
}

I have used UpdateOverwriteVersion and EnableMinorVersions to make it possible change the last modified date. I have even removed it and tested creating new page without that code and using systemUpdate(false) without success.

Everything works greate if I remove last part of the code (CheckIn and Publish) but I need to publish the newly created page.

  • 2
    You should create an SPFieldUserValue object and use it instead of the actual user object! The same approach, but a different field (works the same): sharepoint.stackexchange.com/questions/84232/… – Robert Lindgren Dec 3 '13 at 18:03
  • I used it same problem. – Medes Dec 4 '13 at 8:33
  • Then I think we need to see more of your code, because something is done wrong! – Robert Lindgren Dec 4 '13 at 8:37
  • I have added the code. It works when I remove CheckIn and Publish method, but the page is not published. – Medes Dec 4 '13 at 9:24
  • 1
    Thank you Robert I fixed it by moving CheckIn and Publish to the begining directly after creating the publishing page. – Medes Dec 4 '13 at 9:52
1

Since the problem seems to be related to the Publish of the file, try this approach:

 SPFile fileDest = libDest.RootFolder.Files.Add(
                 urlDestFile, 
                 streamFile, 
                 hashSourceProp, 
                 userCreatedBy, 
                 userModifiedBy, 
                 dateCreatedOn, 
                 dateModifiedOn, 
                 strVerComment, 
                 true);
     if (bolMajorVer)
/*Here we're checking if this is a major version and calling the publish method, passing in the check-in comments.  Oddly when the publish method is called the passed created and modified dates are displayed in the SharePoint UI properly without further adjustment.*/
          fileDest.Publish(strVerComment);
     else
     {
/*Setting the created and modified dates in the SPListItem which corrects the display in the SharePoint UI version history for the draft versions.*/
          SPListItem itmNewVersion = fileDest.Item;
          itmNewVersion["Created"] = dateCreatedOn;
          itmNewVersion["Modified"] = dateModifiedOn;
          itmNewVersion.UpdateOverwriteVersion();
     }

Source of this code: http://sharepointvenividivici.typepad.com/sharepoint-customization/2011/06/maintain-file-version-history-when-movingcopying-files-between-sharepoint-sites.html

  • I solved the problem by moving CheckIn and Publish to the begining of the code so I checkin first then I update the Item. thanks all of you. – Medes Dec 4 '13 at 9:51
2

To create a SPUserField in PowerShell you can use

$user=New-Object Microsoft.SharePoint.SPFieldUserValue(web, $userId, $userName) 
$item[[Microsoft.SharePoint.SPBuiltInFieldId]::Editor] = $user
$item[[Microsoft.SharePoint.SPBuiltInFieldId]::Modified_x0020_By] = $user
$item.SystemUpdate($false);
  • I used it but stil modified by for all pages is same and the person executes the script. I tested Author(created by) it changed but still editor is same. – Medes Dec 4 '13 at 8: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.