简体   繁体   中英

Delete a wiki page with powershell

I need help with my code. I first tried to create a new wiki page in SharePoint 2013 and that worked perfectly. Now I'm trying to delete a wiki page and that doesn't work very well.

My code:

Add-Type –Path "C:\Users\Benutzername\AppData\Local\Apps\OfficeDevPnP.PowerShell.V15.Commands\Modules\OfficeDevPnP.PowerShell.V15.Commands\Microsoft.SharePoint.Client.dll"  
Add-Type –Path "C:\Users\Benutzername\AppData\Local\Apps\OfficeDevPnP.PowerShell.V15.Commands\Modules\OfficeDevPnP.PowerShell.V15.Commands\Microsoft.SharePoint.Client.Runtime.dll" 

Function Delete-WikiPage ([Microsoft.SharePoint.Client.ClientContext]$Context, [string]$WikiLibraryTitle,[string]$PageName)
{
    $wikiLibrary = $context.Web.Lists.GetByTitle($wikiLibraryTitle)
    $context.Load($wikiLibrary.RootFolder.Files)
    $context.ExecuteQuery()

    $wikiPage = $wikiLibrary.RootFolder.Files | Where {$_.Name -eq $pageName}
    $context.Load($wikiPage)
    $context.DeleteObject()
}
$Url = "hhtps://sharepoint.com"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$pageName = "Testlauf.aspx"
Delete-WikiPage -Context $context -WikiLibraryTitle "Testwiki" -PageName $pageName

I got an error message about the DeleteObject() method but I didn't find anything how I can fix that:

Error when calling the method [Microsoft.SharePoint.Client.ClientContext] no method found with the name "DeleteObject".
In C:\Users\Benutzername\Desktop\Projektarbeit_Wiki\PowerShell\Delete_WikiPage.ps1:12 Zeichen:5
+     $context.DeleteObject()
+     ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

I can only work with methods from the Add-Types. Can anyone help me with this?

You use the CSOM in PowerShell. (you load the CSOM DLL ). So you should search information with SharPoint CSOM.

To answer you question, you use : DeleteObject() on you object : context . This methode doesn't exist on the Context Object. But the methode exist on the File.

Si you should try something like :

$wikiPage = $wikiLibrary.RootFolder.Files | Where {$_.Name -eq $pageName}
$context.Load($wikiPage)
$context.ExecuteQuery() //Get the item, but not sure this is needed
$wikiPage.DeleteObject()
$context.ExecuteQuery()

personal Note : You should use CAML query to retrieve your WikiPage, not this: $wikiPage = $wikiLibrary.RootFolder.Files | Where {$_.Name -eq $pageName} With this kind of query you have bad performance, and you can raise exception list threshold

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM