1

I'm trying to copy a content type template file from another site collection to the current one and place it in the hidden folder _cts/<contentType>. Let's say _cts/Email.

It's working fine up until I try to copy the file to the new URL. I keep getting into my Catch block with the error

Exception calling "CopyTo" with "2" argument(s): "Folder "cts" does not exist."

If I put the URL "/_cts/$($contentType.Name)" as I still get the error

"Exception calling "CopyTo" with "2" argument(s): "Value does not fall within the expected range."

if ($spweb.GetFolder($folderName).Exists)
            {
                if ($spweb.GetFile($docTemplateUrl).Exists)
                {
                    try
                    {
                        $file = $spweb.GetFile($docTemplateUrl);
                        $newFileUrl = "_/cts/$($contentType.Name)/$file"
                        $file.CopyTo($newFileUrl, $true);

                        $spweb.GetFolder("/_cts/$($contentType.Name)").Update();

                    }
                    Catch [System.Exception]
                    {
                        Write-Host "Cannot move the file to directory /cts/$($contentType.Name) or a file already exists at the location with the same name" -BackgroundColor DarkRed;
                        return;
                    }

                    return "/cts/$($contentType.Name)"
                }
            }

If I change URL to $newFileUrl = "/_cts/$($contentType.Name)/$fileName" then it comes up with the error

Exception calling "CopyTo" with "2" argument(s): "Folder "Email" does not exist."

Why does this keep happening? I look at the API and it seems right!

The folder _cts is showing in Sharepoint Manager as well, along with _cts/Email

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.