0

I want to upload the same file to multiple site collection with the same hierarchy in all the site collection using powershell and the with functionality auto checkin/checkout.

I have able to upload the file in SharePoint. Below is the code. :

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

# create the Variable Path and Pass the source folder path

$path = “D:\ABC\DEF\26Nov\”;

# create the Variable destination and pass the URL of the SharePoint List

$destination = "complete URL will be mentioned here";

# Store the current user default credentials in the Variable Credentials

$credentials = [System.Net.CredentialCache]::DefaultCredentials;

# Create the object of the Webclient

$webclient = New-Object System.Net.WebClient;

# Pass the user credentials

$webclient.Credentials = $credentials; Get-ChildItem
Get-ChildItem $path | ForEach-Object { $webclient.UploadFile($destination + “/” + $_.Name, “PUT”, $_.FullName)};

For Each loop will upload all of the files one by one onto the destination using the UploadFile method

By This code the file is upload but its checked, out . I want it to be checkin automatically and incase if file is there the auto-checkout and then checkin.

Please help me on this, as I have already spent 2 days in it. But nothing happens Thanks much for your support

Regards

Kishan

  • Whya re you using WebClient class to upload file? You can found countless number of scripts to perform upload kmukesh.wordpress.com/2012/07/07/… – Falak Mahmood Nov 27 '13 at 9:17
  • Thanks @Falak, I have checked out this code, but this is not working for me. The code you referred is for the style sheet. Please tell me if there is any code in which the Upload and checkin/checkout works simultaneously – Kishan Nov 27 '13 at 9:30
  • Do you need to use a $webclient for any specific reason? – BlueBird Nov 27 '13 at 16:46
2

Using $SPFile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout check the state of file and then checked in it.

function CheckInFile ($SPFile)
{
    $x=$SPFile.ServerRelativeUrl
    write-host "file level" $SPFile.Level -ForegroundColor Green
    if ($SPFile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
    {
        write-host " Checking-in file" $x

        $SPFile.CheckIn("Checkin", [Microsoft.SharePoint.SPCheckInType]::MajorCheckin)
    }
    else
    {
        write-host " No Check-in needed for the file" $x

    }
}
  • Thanks @Andrey, I have tried this code adding in the code I mentioned but it nothing works. Thanks for your support – Kishan Nov 27 '13 at 9:43
  • Hi @Andrey, Do you have any code which can upload the file and checkin checkout simultaneously. Much thanks for your support. Regards – Kishan Nov 27 '13 at 9:55
  • 1
    You can use this link – Andrey Ungur Nov 27 '13 at 11:32

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.