1

I have the following code inside my Event Receiver, to copy a file from a document library named "Templates" to a sub-folder inside our "Shared documents" library

    SPDocumentLibrary template = (SPDocumentLibrary)properties.Web.GetList(properties.Web.ServerRelativeUrl + "/OrderTemplates/");
    SPDocumentLibrary projectid = (SPDocumentLibrary)properties.Web.GetList(properties.Web.ServerRelativeUrl + "/Shared Documents/"+properties.ListItemId+"/");
    SPListItem softemplete = null;
    foreach (SPListItem i in template.Items)
        {
            if (i.Name.ToLower().Contains("pof draft"))
                {
                    softemplete = i;

                }
        }
    byte[] fileBytes = softemplete.File.OpenBinary();
    string destUrl = properties.Web.ServerRelativeUrl+ "/"+ projectid.RootFolder.Url + "/" + properties.ListItemId + "/Order Final";
    SPFile destFile = projectid.RootFolder.Files.Add(destUrl, fileBytes, true);

now the above code will work well, till the last line:-

SPFile destFile = projectid.RootFolder.Files.Add(destUrl, fileBytes, true);

which will raise an exception as follow:-

The URL 'Shared Documents/113/Order Final' 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.

although the URL Shared Documents/113/Order Final should be valid, as i have a folder named 113 and under it there is a sub folder named Order Final as follow, where the below url is valid:-

http://servername/PM/Shared Documents/113/Order Final

so not sure why i am getting the exception, that the url is not valid??

2
  • 1
    The destUrl variable is missing the file name. Feb 9, 2018 at 23:22
  • @LukášNešpor you are correct seems i need to add the name and the file extension as well string destUrl = properties.Web.ServerRelativeUrl+ "/"+ projectid.RootFolder.Url + "/" + properties.ListItemId + "/Order Final/POF Draft.xlsx";
    – John John
    Feb 10, 2018 at 0:05

2 Answers 2

0

Try the absolute url instead of relative. The exception message might just be poorly formatted, but I would expect the full url of the "URL" it's trying to reach be included as the parameter.

Your destUrl parameter should be absolute (or at least try it and see).

If that's not it, perhaps url encoding is needed to replace spaces with "%20" and so on.

EDIT

On second pass, I noticed the url part should likely just be the "file name". Though because you want it to be in a subfolder it would be "subfolder/file name". Right now it looks like you have "library/subfolder/file name". Am I reading that right?

0

It seems I need to add the name and the file extension as well.

string destUrl = properties.Web.ServerRelativeUrl+ "/"+ projectid.RootFolder.Url + "/" + properties.ListItemId + "/Order Final/POF Draft.xlsx";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.