0

I have SPO site and trying to check in checked out files (also inside folders) under "Shared Document" library. This PS script should checked in all files under single document library. I have tried below power shell code but it throws error.

    Connect-SPOService –Url https://***-admin.sharepoint.com –Credential jinesh@coctees.onmicrosoft.com
Add-Type -Path "E:/sp/Microsoft.SharePoint.Client.dll"   
Add-Type -Path "E:/sp/Microsoft.SharePoint.Client.Runtime.dll"   




$spWeb = Get-SPWeb "https://***.sharepoint.com/sites/tesst"
$listName = "Shared Documents"
$list = $spWeb.Lists |? {$_.Title -eq $listName}
foreach ($item in $list.Items) 
{
    $itemFile = $item.File
    if( $itemFile.CheckOutStatus -ne "None" )
    { 
        $itemFile.CheckIn("Automatic CheckIn. (Administrator)")
    }
}
$spWeb.Dispose()

Error:

Get-SPWeb : The term 'Get-SPWeb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:10
 $spWeb = Get-SPWeb "https://coctees.sharepoint.com/sites/tesst"
         ~~~~~~~~~
     CategoryInfo          : ObjectNotFound: (Get-SPWeb:String) [], CommandNotFoundException
     FullyQualifiedErrorId : CommandNotFoundException

Thanks.

  • could you please add the detail about error, it will help to understand the issue – Waqas Sarwar MVP Apr 3 '18 at 14:17
1

I did some job using PowerShell + REST API. My Script bellow is to create a site inside a site collection. I have taken it from a website. This has shown me how to deal with SPO using powershell and REST API

Configuration

$TenantUrl = "https://yoursitel.sharepoint.com" $Username = "sharepoint@yoursite" $Password = 'password'

$SiteTitle = $args[0] $SiteUrl = "https://yoursitel.sharepoint.com""+ $args[1] # Note this URL must be available (check with "/_api/GroupSiteManager/GetValidSiteUrlFromAlias") $SiteTemplate = "" # "Topic" => leave empty (default), "Showcase" => "6142d2a0-63a5-4ba0-aede-d9fefca2c767" and "Blank" => "f6cc5403-0d63-442e-96c0-285923709ffc"

Communication site creation request

$RequestBody = '{"request":{"__metadata":{"type":"SP.Publishing.PublishingSiteCreationRequest"},"Title":"' + $SiteTitle + '","Url":"' + $SiteUrl + '","Description":"","Classification":"","SiteDesignId":"' + $SiteTemplate + '","AllowFileSharingForGuestUsers":false}}'

if ($SiteTemplate -eq "") { $RequestBody = '{"request":{"__metadata":{"type":"SP.Publishing.PublishingSiteCreationRequest"},"Title":"' + $SiteTitle + '","Url":"' + $SiteUrl + '","Description":"","Classification":"","AllowFileSharingForGuestUsers":false}}' }

Get a user based context for SharePoint (app credentials not supported for this approach)

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($TenantUrl) $Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $(ConvertTo-SecureString -AsPlainText $Password -Force)) $Context.ExecuteQuery()

Get url, cookie and forms digest for authentication

$RequestUrl = "$($TenantUrl)/_api/sitepages/publishingsite/create" $AuthenticationCookie = $Context.Credentials.GetAuthenticationCookie($TenantUrl, $true) $FormsDigest = $Context.GetFormDigestDirect()

$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession $WebSession.Credentials = $Context.Credentials $WebSession.Cookies.SetCookies($TenantUrl, $AuthenticationCookie)

$Headers = @{ 'X-RequestDigest' = $FormsDigest.DigestValue; 'accept' = 'application/json;odata=verbose'; 'content-type' = 'application/json;odata=verbose' }

Call REST API to create new site

$Result = Invoke-RestMethod -Method Post -WebSession $WebSession -Headers $Headers -ContentType "application/json;odata=verbose;charset=utf-8" -Body $RequestBody -Uri $RequestUrl -UseDefaultCredentials

$Context.Dispose()

Site has been created

Write-Output "New site created at: $($Result.d.Create.SiteUrl)"

  • I take this script from a site I can´t remember I´m trying to find it now – user70011 Apr 3 '18 at 17:26
1

You get the error because “Get-SPWeb” is for SharePoint On-premise environment.

In SharePoint Online, you can use the script below to find all checked out file and check in them:

function Get-SPOFolderFiles
{
param (
        [Parameter(Mandatory=$true,Position=1)]
        [string]$Username,
        [Parameter(Mandatory=$true,Position=2)]
        [string]$Url,
        [Parameter(Mandatory=$true,Position=3)]
        $password,
        [Parameter(Mandatory=$true,Position=4)]
        [string]$ListTitle
        )


  $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
  $ctx.Load($ctx.Web)
  $ctx.ExecuteQuery()
  $ll=$ctx.Web.Lists.GetByTitle($ListTitle)
  $ctx.Load($ll)
  $ctx.ExecuteQuery()
  $spqQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
 $spqQuery.ViewXml ="<View Scope='RecursiveAll' />";
   $itemki=$ll.GetItems($spqQuery)
   $ctx.Load($itemki)
  $ctx.ExecuteQuery()

  foreach($item in $itemki)
  {

  Write-Host $item["FileRef"] $item.ElementType

  $file =
        $ctx.Web.GetFileByServerRelativeUrl($item["FileRef"]);
        $ctx.Load($file)
        $ctx.Load($file.Versions)     

         $ctx.Load($file.ListItemAllFields)
        $Author=$file.Author
        $CheckedOutByUser=$file.CheckedOutByUser
        $LockedByUser=$file.LockedByUser
        $ModifiedBy=$file.ModifiedBy
        $ctx.Load($Author)
        $ctx.Load($CheckedOutByUser)
        $ctx.Load($LockedByUser)
        $ctx.Load($ModifiedBy)
        $ctx.Load($file.EffectiveInformationRightsManagementSettings)
        $ctx.Load($file.Properties)
        $ctx.Load($file.VersionEvents)
        try
        {
        $ctx.ExecuteQuery()
        }
        catch
        {}

       if($CheckedOutByUser.LoginName -ne $null){
       Write-Host $file.Name
       Write-Host $CheckedOutByUser.LoginName
       $file.CheckIn('Checked in automatically', 'MajorCheckIn')
  $ctx.Load($file)
  try
  {
  $ctx.ExecuteQuery()        
  Write-Host $file.Name " has been checked in"     -ForegroundColor DarkGreen 
  }
        catch [Net.WebException]
     { 
        Write-Host $_.Exception.ToString()
     }

       }



     }   
  }

#Paths to SDK
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

#Enter the data
$AdminPassword=Read-Host -Prompt "Enter password" -AsSecureString
$username="user@testlz.onmicrosoft.com"
$Url="https://domain.sharepoint.com/sites/sitename/"
$ListTitle="libraryname"

Get-sPOFolderFiles -Username $username -Url $Url -password $AdminPassword -ListTitle $ListTitle

More information and PowerShell script file download in the article below: https://gallery.technet.microsoft.com/office/Find-all-checked-out-files-56fd562d#content

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.