0

what i want is i want to copy a document (or item) exist inside my document library into a folder exist also inside the same document library. any help please:

i tried that but no way: i want make copy

  public void MoveItemInsideFolder(SPListItem item, string path)
    {
        //Path like 'Folder1/Folder2/'
        switch (item.FileSystemObjectType)
        {
            case SPFileSystemObjectType.Folder:
                var newFolder = string.Format("{0}/{1}{2}", 
    item.ParentList.RootFolder.Url, path, item.Folder.Name);
                item.Folder.MoveTo(newFolder);
                break;
            case SPFileSystemObjectType.File:
                var file = item.Web.GetFile(item.Url);
                var newFilePath = string.Format("{0}/{1}{2}_.000", 
  item.ParentList.RootFolder.Url, path, item.ID);
                file.MoveTo(newFilePath);
                break;
        }
    }
1
  • Are you getting any error.
    – Aanchal
    Commented May 29, 2014 at 9:38

2 Answers 2

1

Comparing this with some of my own code I believe there are two issues here. Firstly your not doing a file (or folder) update after the move e.g. file.Update(). Secondly I think the path has to be Server Relative, the one your using is web relative.

So taking your code I've done a version for document libraries:

public void MoveItemInsideFolder(SPListItem item, string path)
{
    //Path like 'Folder1/Folder2/'
    switch (item.FileSystemObjectType)
    {
        case SPFileSystemObjectType.Folder:
            SPFolder folder = item.Folder;
            var newFolder = string.Format("{0}/{1}{2}", item.ParentList.RootFolder.ServerRelativeUrl, path, folder.Name);
            folder.MoveTo(newFolder);
            folder.Update()
            break;
        case SPFileSystemObjectType.File:
            SPFile file = item.File;
            var newFilePath = string.Format("{0}/{1}{2}", item.ParentList.RootFolder.ServerRelativeUrl, path, item.Name);
            file.MoveTo(newFilePath);
            file.Update();
            break;
    }
}
0

Modify your code as :

 case SPFileSystemObjectType.File:
                    var file = item.Web.GetFile(item.Url);
                    var newFilePath = string.Format("{0}/{1}/{2}_.000",
                    item.ParentList.RootFolder.Url, path, item.ID);
                    file.MoveTo(newFilePath);

I am not able to understand your folder moving login. Can you please explain that.

Also you can use file.CopyTo(newFilePath); to copy the files.

Your Answer

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

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