Move documents within site at folder level: Application Page


I have one requirement where user needs a friendly interface with the ability to move the document from one location (any folder under any document library) to another location.

To achieve this functionality, my first task is to create a Tree View of all the Document Libraries displaying complete folder structure (show all levels of folders).

 

Folder Structure

 

Code to create a Tree View for all the Document Libraries with recursive folders.

   1: /// <summary> 

   2: /// Creates a Tree View for all the Document Libraries with recursive folders. 

   3: /// </summary> 

   4: private void CreateTreeViewOfAllFolders()

   5: {

   6:     TreeNode Category1Node = new TreeNode("Category 1");

   7:     Category1Node.SelectAction = TreeNodeSelectAction.Expand;

   8:     TreeNode Category2Node = new TreeNode("OPI Material");

   9:     Category2Node.SelectAction = TreeNodeSelectAction.Expand;

  10:

  11:     try

  12:     {

  13:         using (SPSite site = new SPSite(baseURL))

  14:         {

  15:             using (SPWeb web = site.OpenWeb())

  16:             {

  17:                 baseURL = web.Url.ToString();

  18:                 treeFolderStructure.Nodes.Add(Category1Node);

  19:                 treeFolderStructure.Nodes.Add(Category2Node);

  20:

  21:                 TreeNode documentLibraryNode = null;

  22:                 SPListCollection docLibCol = web.GetListsOfType(SPBaseType.DocumentLibrary);

  23:

  24:                 foreach (SPList docLib in docLibCol)

  25:                 {

  26:                     //check if the current user have access to the libraries 

  27:                     if (docLib.DefaultView != null)

  28:                     {

  29:                         documentLibraryNode = new TreeNode();

  30:                         documentLibraryNode.Text = docLib.Title;

  31:                         documentLibraryNode.Value = docLib.DefaultViewUrl.ToString();

  32:                         documentLibraryNode.NavigateUrl = "javascript:void(0);";

  33:                         documentLibraryNode.SelectAction = TreeNodeSelectAction.Select;

  34:                         documentLibraryNode.Target = "_self";

  35:                         documentLibraryNode.ImageUrl = baseURL + "/_layouts/images/folder.gif";

  36:

  37:                         if (docLib.Folders.Count > 0)

  38:                         {

  39:                             foreach (SPListItem lstItem in docLib.Folders)

  40:                             {

  41:                                 if (lstItem.ContentType.Name == "Folder" && !folderStructureList.Contains(lstItem.Url.ToString()))

  42:                                 {

  43:                                     documentLibraryNode.ChildNodes.Add(CreateBranch(lstItem.Folder));

  44:                                     documentLibraryNode.CollapseAll();

  45:                                 }

  46:                             }

  47:                         }

  48:

  49:                         if (docLib.Title == "Document Library 1")

  50:                         {

  51:                             Category1Node.ChildNodes.Add(documentLibraryNode);

  52:                         }

  53:                         else if (docLib.Title == "Document Library 2" || docLib.Title == "Document Library 3" || docLib.Title == "Document Library 4")

  54:                         {

  55:                             Category2Node.ChildNodes.Add(documentLibraryNode);

  56:                         }

  57:                     }

  58:                 }

  59:             }

  60:         }

  61:     }

  62:     catch

  63:     {

  64:     }

  65: }

  66:

  67: /// <summary> 

  68: /// Create a node for the folder 

  69: /// </summary>

  70: /// <param name="folder"></param> 

  71: /// <returns></returns> 

  72: private TreeNode CreateFolderNode(SPFolder folder)

  73: {

  74:     TreeNode folderNode = new TreeNode();

  75:     try

  76:     {

  77:         folderNode.Text = folder.Name;

  78:         folderNode.Value = folder.Url.ToString();

  79:         folderNode.ImageUrl = baseURL + "/_layouts/images/folder.gif";

  80:         folderNode.NavigateUrl = "javascript:void(0);";

  81:         folderNode.SelectAction = TreeNodeSelectAction.Select;

  82:         folderNode.Target = "_self";

  83:         folderStructureList.Add(folder.Url.ToString());

  84:     }

  85:     catch

  86:     {

  87:     }

  88:     return folderNode;

  89: }

 

After Creating the folder structure, next task is to move the document or folder to the selected folder from the tree view.

   1: if (treeFolderStructure.SelectedNode != null)

   2: {

   3:     //get the selected folder from the tree view of the document libraries and folders

   4:     string folderSelected = treeFolderStructure.SelectedNode.Value;

   5:

   6:     using (SPSite site = new SPSite(baseURL))

   7:     {

   8:         using (SPWeb web = site.OpenWeb())

   9:         {

  10:             SPListItem docToMove = web.Lists[currentListId].GetItemById(docId);

  11:             Uri destUrl=null;

  12:             //Move the whole folder to the selected location 

  13:             if (docToMove.Folder != null)

  14:             {

  15:                 SPFolder folder = docToMove.Folder;

  16:                 destUrl = new Uri(baseURL + "/" + folderSelected + "/" + folder.Name);

  17:                 folder.MoveTo(destUrl.AbsoluteUri);

  18:             }

  19:             //Move the file to selected location 

  20:             else

  21:             {

  22:                 SPFile file = web.GetFile(docToMove.Url);

  23:                 destUrl = new Uri(baseURL + "/" + folderSelected + "/" + file.Name);

  24:                 file.MoveTo(destUrl.AbsoluteUri);

  25:             }

  26:         }

  27:     }

  28: }

 

Cheers,

Abhijit Sil

Advertisements

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: