locked
AddIn: How to get currently opened (focused) workitem in Visual Studio Team Explorer? RRS feed

  • Question

  •  

    Hello everybody,

    I'm about to write a Visual Studio (2010) AddIn. With this AddIn, I need to get the TFS workitem in Visual Studio (in the Team Explorer) which is currently opened / focused by the user. How can I do that programmatically in C#? 

    So far, I've tried it with the applicationObject, e.g.

     

    Document doc = _applicationObject.ActiveDocument;
    

     

    but I don't know if this is correct. (Otherwise, if this is should be the right way to do it: How can I convert the "Document"-type to "WorkItem"?

     

     

    Any suggestions?

    Thanks in advance!

     



    • Edited by Lilith X Friday, September 9, 2011 8:40 AM
    Friday, September 9, 2011 8:22 AM

Answers

  • Hello Lilith,

     

    Based on the http://social.msdn.microsoft.com/Forums/en-US/tfsworkitemtracking/thread/974df18b-2331-49b0-a46a-0a7cd9b9271b, we know that you have get an idea how to obtain a workitem from a ID or name,  the current question is how to get the name or id from a current opened workitem.

     

    As we know, a opened workitem is represented as  a document object. Therefore, you can get the current active Document via DTE.ActiveDocument.  If you want get sorts of opened Documents you can go though DTE.Documents object to check every opened documents one by one, which as mentioned by Vicky in previous thread.

     

    Now, the question changes to how identify whether  a Document is a  workitem. As far as I know, a document object use kind property  to indicate the type of the object. The kind itself is a GUID. The GUID for workitem is “{40A91D9D-8076-4D28-87C5-5AF9F0ACFE0F}”. Once we get a workitem as document, we can get the ID or name from the document.name property. E.g.

     

       var item = typ.ProjectItem;
                        DTE2 dte = _applicationObject;
                        Guid WI = new Guid("{40A91D9D-8076-4D28-87C5-5AF9F0ACFE0F}");
     
                        
                        // for single active document;
                        Document adoc = dte.ActiveDocument;
     
                        if (adoc.Kind == WI.ToString())
                        {
                            
                            ... ...
                            
                            WorkItem wi = workItemStore.GetWorkItem(adoc.name);
     
                         //to obtain the workitem and do your own logic 
                        }
     
     
                        // for all opened documents;
                        Documents docs = dte.Documents;
                        foreach (Document doc in docs)
                        {
                            if (adoc.Kind == WI.ToString())
                            {
                            ... ...
                            
                            WorkItem wi = workItemStore.GetWorkItem(adoc.name);
         
                                                //to obtain the workitem and do your own logic 
                            }
                        
                        }
     

     

     

    Hope the information helps.

     

    Regards,

    Yi

     

     


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Yi Feng Li Monday, September 12, 2011 6:43 AM
    • Marked as answer by Lilith X Thursday, September 15, 2011 6:20 AM
    Monday, September 12, 2011 6:34 AM
  • Hi Lilith,

    You can convert a String to a Int by Convert.ToInt32().

    More information, see http://msdn.microsoft.com/en-us/library/bb397679.aspx

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Lilith X Thursday, September 15, 2011 6:20 AM
    Thursday, September 15, 2011 2:46 AM

All replies

  • Hello Lilith,

     

    Based on the http://social.msdn.microsoft.com/Forums/en-US/tfsworkitemtracking/thread/974df18b-2331-49b0-a46a-0a7cd9b9271b, we know that you have get an idea how to obtain a workitem from a ID or name,  the current question is how to get the name or id from a current opened workitem.

     

    As we know, a opened workitem is represented as  a document object. Therefore, you can get the current active Document via DTE.ActiveDocument.  If you want get sorts of opened Documents you can go though DTE.Documents object to check every opened documents one by one, which as mentioned by Vicky in previous thread.

     

    Now, the question changes to how identify whether  a Document is a  workitem. As far as I know, a document object use kind property  to indicate the type of the object. The kind itself is a GUID. The GUID for workitem is “{40A91D9D-8076-4D28-87C5-5AF9F0ACFE0F}”. Once we get a workitem as document, we can get the ID or name from the document.name property. E.g.

     

       var item = typ.ProjectItem;
                        DTE2 dte = _applicationObject;
                        Guid WI = new Guid("{40A91D9D-8076-4D28-87C5-5AF9F0ACFE0F}");
     
                        
                        // for single active document;
                        Document adoc = dte.ActiveDocument;
     
                        if (adoc.Kind == WI.ToString())
                        {
                            
                            ... ...
                            
                            WorkItem wi = workItemStore.GetWorkItem(adoc.name);
     
                         //to obtain the workitem and do your own logic 
                        }
     
     
                        // for all opened documents;
                        Documents docs = dte.Documents;
                        foreach (Document doc in docs)
                        {
                            if (adoc.Kind == WI.ToString())
                            {
                            ... ...
                            
                            WorkItem wi = workItemStore.GetWorkItem(adoc.name);
         
                                                //to obtain the workitem and do your own logic 
                            }
                        
                        }
     

     

     

    Hope the information helps.

     

    Regards,

    Yi

     

     


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Yi Feng Li Monday, September 12, 2011 6:43 AM
    • Marked as answer by Lilith X Thursday, September 15, 2011 6:20 AM
    Monday, September 12, 2011 6:34 AM
  • Hello Yi Feng Li,

    thanks for your reply. I still have one issue.

    This:

     

    WorkItem wi = workItemStore.GetWorkItem(adoc.name);
    

     

    is not working, because adoc.name returns a string. But the method GetWorkItem only takes an uri or an integer as a parameter.

    Any ideas how to solve this?


    • Edited by Lilith X Wednesday, September 14, 2011 2:55 PM
    Wednesday, September 14, 2011 2:54 PM
  • Hi Lilith,

    You can convert a String to a Int by Convert.ToInt32().

    More information, see http://msdn.microsoft.com/en-us/library/bb397679.aspx

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Lilith X Thursday, September 15, 2011 6:20 AM
    Thursday, September 15, 2011 2:46 AM
  • Hi Yi Feng Li,

    now I got it. My mistake was, that I tried to get a workitem when a workitem-query was opened, not a workitem alone. Thank you very much for your help! It works fine.

    Regards, Lilith

    Thursday, September 15, 2011 6:19 AM