Slight modifications to be able to export recursively

Sep 9, 2011 at 8:29 PM

I updated the code (inside CommandExportWiq.cs in the tfs2010 project) to allow me to export a list of queries.

 This allows you to specify the following:

/n@<FileName> - this takes the input from a file.  You can create the file by using the list command and redirect it to a file :-).

/f@<path> - this allows you to specify the root directory to where all the files are written.  It converts the path that is included in the query name, and creates the directories as needed.

I thought these might be useful to someone else, so I thought I'd post them here.

One more thing I noticed while using the tool is that if the query name as special characters (such as the '/'), the code does not properly export those, I did not spend any time figuring out why (I only had one because the query name included a date like 10/1/2010, so I just renamed the query). 

 John

---

 


        /// <inheritdoc />
        public void Execute(IDictionary<string, string> parameters)
        {
          WorkItemStore workItemStore = TfsUtility.GetWorkItemStore(parameters["/collection"], parameters.ContainsKey("/noprompt"));

          if (parameters["/n"].StartsWith("@"))
          {
            string line;

            // Read the file and process it line by line
            using (var file = new System.IO.StreamReader(parameters["/n"].Substring(1)))
            {
              while ((line = file.ReadLine()) != null)
              {
                if (!string.IsNullOrEmpty(line))
                {
                  System.Console.WriteLine("Processing " + line);
                  try
                  {
                    ExportQuery(workItemStore, line, parameters);
                  }
                  catch(Exception ex)
                  {
                    Console.WriteLine("Not Processed " + ex.ToString());
                  }
                }
              }

              file.Close();
            }
          }
          else
          {
            // export just the single query
            ExportQuery(workItemStore, parameters["/n"], parameters);
          }
  
        }

        private void ExportQuery(WorkItemStore workItemStore, string strQuery, IDictionary<string, string> parameters)
        {
          // get query
          var query = workItemStore.Projects[parameters["/p"]]
              .QueryHierarchy
              .Find(strQuery);

          if (query == null)
          {
            throw new Exception(string.Format(ResourceStrings.ErrorUnknownWorkItemQuery, strQuery));
          }

          if (!(query is QueryDefinition))
          {
            throw new Exception(string.Format(ResourceStrings.ErrorNotAQuery, parameters["/n"]));
          }

          // create xml
          var xml = new XDocument(
              new XDeclaration("1.0", parameters.ContainsKey("/e") ? parameters["/e"] : "utf-8", null),
              new XElement(
                  "WorkItemQuery",
                  new XAttribute("Version", "1"),
                  new XElement("Wiql", ((QueryDefinition)query).QueryText)
                  )
              );

          // write to output
          if (parameters.ContainsKey("/f"))
          {
            string strTargetFile = "";
            if (parameters["/f"].StartsWith("@"))
            {
              strTargetFile = parameters["/f"].Substring(1);
              strTargetFile = Path.Combine(strTargetFile, strQuery.Replace('/', '\\'));
              strTargetFile = Path.ChangeExtension(strTargetFile, ".wiq");
            }
            else
            {
              strTargetFile = parameters["/f"];
            }

            // create the directory if needed
            Directory.CreateDirectory(Path.GetDirectoryName(strTargetFile));
            // write the file
            xml.Save(strTargetFile);

            Console.WriteLine(ResourceStrings.OperationCompleted);
          }
          else
          {
            Console.WriteLine(xml.Declaration);
            Console.WriteLine(xml);
          }
        }
        #endregion
   

 

Coordinator
Sep 26, 2011 at 8:57 AM

Hi,
I'll look at this as soon as possible.

Thanks for the feedback

Coordinator
Jul 5, 2012 at 1:16 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Sep 30, 2013 at 8:07 PM
I can't figure out how to get the recursive export to work. Can someone please post an example?
Sep 30, 2013 at 8:30 PM
Edited Sep 30, 2013 at 9:17 PM
OK, I figured it out. I'll post my findings in case some else is having trouble with it.

First create a text file by capturing the output of the list command like this. I saved my output as "WiqList.txt".
wiqadmin list /collection:collectionurl /p:project /recursive > ./WiqList.txt

Then with that list of queries in the same directory, reference the list like this.
wiqadmin export /collection:collectionurl /p:project /n:@WiqList.txt /d:./
Nov 7, 2013 at 11:37 AM
Great!

What about a list import? :-)
Coordinator
Nov 19, 2013 at 8:06 PM
Hi BenGraf,

I'm currently working on rewriting to tool as a PowerShell module. The cmdlets will support pipeline so that you can pass lists to them when appropriate. I don't have a ETA on the PowerShell cmdlet though.

I'll try to add the list import to the command line if i find some time.
Coordinator
Nov 19, 2013 at 8:13 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.