3

I wan to change the page name with Power shell script. When I had run the script I was given an spexception error:

Exception calling "CheckOut" with "0" argument(s): "The URL 'en/articles/kbarticles/Pages/R2-1_articles_Testing.aspx' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web." At line:1 char:1 + $spFile.CheckOut() + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SPException

The below script was used for update the name of the page:

$spWeb = Get-SPWeb("here i am given the web url")
$spFile = $spWeb.GetFile("full url with page name")
$spFile.CheckOut("Online",$null)    ////here i am getting the error(spexception)
$spFile.Properties["Name"] = "Pages/R2-1_articles_Testing_New"
$spFile.Update()
$spFile.CheckIn("Update page layout via PowerShell",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
$spWeb.Dispose()
Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
user21709
  • 43
  • 1
  • 5
  • Thanks for your replay, but i am run the script same as your updates.Still i am getting the same error(Spexception) – user21709 Jan 03 '14 at 10:36
  • Exception calling "CheckOut" with "0" argument(s): "The URL 'en/articles/kbarticles/Pages/R2-1_articles_Testing.aspx' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web." At line:1 char:1
    • $spFile.CheckOut()
    •   + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : SPException
      
    – user21709 Jan 03 '14 at 12:19
  • the above exception was i am facing – user21709 Jan 03 '14 at 12:20
  • when i had run the script by power shell $spFile.CheckOut() – user21709 Jan 03 '14 at 12:21
  • Please see my updated answer below – Robert Lindgren Jan 03 '14 at 12:24

1 Answers1

3

Get-SPWeb does not need to have "(" and ")" around the parameters. Try:

$spWeb = Get-SPWeb "web url"

also, use $spFile.CheckOut() without any parameters.

Full script:

$spWeb = Get-SPWeb "here i am given the web url"
$spFile = $spWeb.GetFile("full url with page name")
$spFile.CheckOut()
$spFile.Properties["Name"] = "Pages/R2-1_articles_Testing_New"
$spFile.Update()
$spFile.CheckIn("Update page layout via PowerShell",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
$spWeb.Dispose()

Also, take a look at this similar question, specifically the part about checking if the page is in a state that allows it to be checked out (Pseudo code cited from a comment on that question):

$fooWeb = Get-SPWeb("FooWebURL");
$fooFile = $fooWeb.GetFile("FooFile");

if($fooFile.CheckOutType -eq "None" -And $fooFile.LockType -eq "None") { $fooFile.CheckOut() Write-Host $fooFile.Name Checked out 6. } else { Write-Host $fooFile.Name already Checked out or locked }

$fooWeb.Dispose()

Update after Error message was posted

To use the $spWeb.GetFile() you must specify a FULL, existing URL to the file, which you are not providing right now!

It should look something like http://siteadress/possiblysubweb/Pages/R2-1_articles_Testing.aspx, so you are missing part of the url.

Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79