如何将文件附加到工作在TFS项目没有物理文件路径? [英] How to attach a file to work item in TFS without physical file path?

查看:441
本文介绍了如何将文件附加到工作在TFS项目没有物理文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类型的对象 Microsoft.Office.Interop.Outlook.Attachment ,我想将其附加到TFS工作项(微软.TeamFoundation.WorkItemTracking.Client.Microsoft.TeamFoundation.WorkItemTracking.Client

I have an object of type Microsoft.Office.Interop.Outlook.Attachment and I want to attach it to a TFS work item (Microsoft.TeamFoundation.WorkItemTracking.Client.Microsoft.TeamFoundation.WorkItemTracking.Client)

不幸的是,我发现只有一个方法添加附件 Microsoft.TeamFoundation.WorkItemTracking.Client.Microsoft.TeamFoundation.WorkItemTracking.Client 需要文件的物理路径。但对我来说,我已经在内存中的文件(类型的 Microsoft.Office.Interop.Outlook.Attachment )。

Unfortunately I found just one way to add attachment to Microsoft.TeamFoundation.WorkItemTracking.Client.Microsoft.TeamFoundation.WorkItemTracking.Client that needs physical path of file. But in my case, I have a file in memory (of type Microsoft.Office.Interop.Outlook.Attachment).

我如何我的文件附加到TFS工作项

How I can attach my file to TFS work item?

注意:此代码是没有回答我的问题:

workItem.Attachments.Add(new Attachment("PATH OF MY ATTACHMENT", "COMMENT ABOUT ATTACHMENT"));

由于我没有任何路径。此外,我不希望我的文件保存到硬盘上,因为这种方式有很糟糕的表现。

because I haven't any path. Also I don't want to save my file into hard disk because this way has very bad performance.

推荐答案

您可以做到这一点使用 Microsoft.TeamFoundation.WorkItemTracking.Proxy.WorkItemServer 对象(见下面的代码)。它需要使用常规TFS WIT对象模型时比多一点点工作,但允许您使用的附件,而不是使用物理文件路径Stream对象。

You can do it using Microsoft.TeamFoundation.WorkItemTracking.Proxy.WorkItemServer object (see the code below). It requires a little bit more work than when using regular TFS WIT object model but allows you to use Stream object for attachments instead of using physical file path.

using Microsoft.TeamFoundation.WorkItemTracking.Proxy;

var tpc = new TfsTeamProjectCollection(new Uri("<collection URL>"));
var store = tpc.GetService<WorkItemStore>();
var teamProject = store.Projects["<project name>"];
var server = tpc.GetService<WorkItemServer>();

FileAttachment attachment = new FileAttachment();
attachment.LocalFile = stream; /*this is the stream object with attachment contents*/
attachment.AreaNodeUri = teamProject.AreaRootNodes[0].Uri.ToString();
attachment.FileNameGUID = Guid.NewGuid(); /*just random guid*/
attachment.ProjectUri = teamProject.Uri.ToString();

server.UploadFile(attachment); /*upload the file to TFS*/

/*Time to attach the TFS file to the work item. We need to use Update() method directly*/
const string c_UpdatePackage = 
                @"<Package AttachmentUrl=""{7}/WorkItemTracking/v1.0/AttachFileHandler.ashx"" xmlns="""">
                   <UpdateWorkItem ObjectType=""WorkItem"" ClientCapabilities=""0"" WorkItemID = ""{0}"" Revision=""{1}"">
                     <InsertFile FieldName=""System.AttachedFiles"" OriginalName=""{2}"" FileName=""{3}"" CreationDate=""{4}"" LastWriteDate=""{4}"" FileSize=""{5}"" />
                       <Columns>
                         <Column Column=""System.ChangedBy"" Type=""String"">
                           <Value>{6}</Value>
                         </Column>
                       </Columns>
                       <ComputedColumns>
                         <ComputedColumn Column=""System.PersonId"" />
                         <ComputedColumn Column=""System.RevisedDate"" />
                         <ComputedColumn Column=""System.ChangedDate"" />
                         <ComputedColumn Column=""System.AuthorizedDate"" />
                         <ComputedColumn Column=""System.Watermark"" />
                       </ComputedColumns>
                    </UpdateWorkItem>
                   </Package>";

XmlDocument updatePackage = new XmlDocument();
updatePackage.LoadXml(string.Format(c_UpdatePackage, 
                1 /*work item ID*/, 
                2 /*work item latest revision*/, 
                "<file name you want, it will show up in the work item attachment tab>", 
                attachment.FileNameGUID,
                DateTime.Now.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'"),
                fileContent.Length, 
                "<display name of the TFS user making the change, e.g. John Smith>",
                "<collection url, e.g. http://localhost:8080/tfs/defaultcollection>"));

            XmlElement outputPackage; /*this can be ignored*/
            string dbStamp; /*this can be ignored*/
            IMetadataRowSets metadata; /*this can be ignored*/
            server.Update(Guid.NewGuid().ToString(),
                updatePackage.DocumentElement,
                out outputPackage,
                null,
                out dbStamp,
                out metadata);

这篇关于如何将文件附加到工作在TFS项目没有物理文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆