private void tvLists_AfterSelect(object sender, TreeViewEventArgs e) { ImportDestination importdestination = tvLists.SelectedNode.Tag as ImportDestination; textBoxDestinationFolderURL.Text = importdestination.DestinationFolderUrl; textBoxDestinationServerURL.Text = importdestination.DestinationServerUrl; textBoxDestinationWebURL.Text = importdestination.DestinationWebUrl; textBoxDestinationLibraryName.Text = importdestination.DestinationLibraryName; }
// Process an individual web and add it and it's children into the passed tree view node private static void PopulateTreeView(Web web, TreeNode currentnode, ClientContext clientcontext, BackgroundWorker worker) { worker.ReportProgress(0, MethodBase.GetCurrentMethod().Name.ToString() + ":" + web.Title); WebCollection webs = web.GetSubwebsForCurrentUser(null); clientcontext.Load(webs); clientcontext.ExecuteQuery(); // Go through every sub web of the passed web foreach (Web subweb in webs) { clientcontext.Load(subweb); clientcontext.ExecuteQuery(); // Create a node for the current web TreeNode subnode = new TreeNode(); // Set the node title (That displays to the user) subnode.Text = subweb.Title.ToString(); // Iterate any further sub webs and lists WebCollection furthersubwebs = subweb.Webs; ListCollection furtherlists = subweb.Lists; clientcontext.Load(furthersubwebs); clientcontext.Load(furtherlists); clientcontext.ExecuteQuery(); // If there are any further subwebs then add them foreach (Web furthersubweb in furthersubwebs) { PopulateTreeView(furthersubweb, subnode, clientcontext, worker); } // If there are any lists then add then foreach (List list in furtherlists) { clientcontext.Load(list); clientcontext.ExecuteQuery(); if (list.BaseType == BaseType.DocumentLibrary) { // At this point we are adding a list which is a document library and it is a valid destination // we will therefore supply a bit more detail about the item in a TAG TreeNode librarynode = new TreeNode(); librarynode.Text = list.Title; Folder folder = list.RootFolder; clientcontext.Load(folder); clientcontext.ExecuteQuery(); ImportDestination importdestination = new ImportDestination(); importdestination.DestinationFolderUrl = folder.ServerRelativeUrl; importdestination.DestinationWebUrl = subweb.ServerRelativeUrl; importdestination.DestinationServerUrl = Get_ServerURL_From_URL(clientcontext.Url); importdestination.DestinationLibraryName = list.Title; librarynode.Tag = importdestination; subnode.Nodes.Add(librarynode); } } clientcontext.ExecuteQuery(); // Add the completed node to the tree view control currentnode.Nodes.Add(subnode); } // Go through every list of the passed web ListCollection lists = web.Lists; clientcontext.Load(lists); clientcontext.ExecuteQuery(); foreach (List list in web.Lists) { clientcontext.Load(list); clientcontext.ExecuteQuery(); // Now check to see if the list is a document library if (list.BaseType == BaseType.DocumentLibrary) { // At this point we are adding a list which is a document library and it is a valid destination // we will therefore supply a bit more detail about the item in a TAG TreeNode librarynode = new TreeNode(); librarynode.Text = list.Title; Folder folder = list.RootFolder; clientcontext.Load(folder); clientcontext.ExecuteQuery(); ImportDestination importdestination = new ImportDestination(); importdestination.DestinationFolderUrl = folder.ServerRelativeUrl; importdestination.DestinationWebUrl = web.ServerRelativeUrl; importdestination.DestinationServerUrl = Get_ServerURL_From_URL(clientcontext.Url); importdestination.DestinationLibraryName = list.Title; librarynode.Tag = importdestination; currentnode.Nodes.Add(librarynode); } } // Now if after all this we have a web which has no document libraries or sub webs then we don't need it if (currentnode.Nodes.Count == 0) { currentnode.Remove(); } }