简体   繁体   中英

insert work item into tfs using tfs api

I am building a Web page to insert TFS Work Items into TFS using TFS API.

I am using my credentials to connect to TFS Server.

Each time someone creates a TFS Work item using the TFS Web page, it creates a Work Item under my name ,since I was the one connected to the TFS Server. Is there a way I can Create a Work Item with the User logged into my web application and created the work Item ?

I have access to the Users name but not to the users password

protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
        Uri url = new Uri("url");

            NetworkCredential nc = new NetworkCredential();

            TfsTeamProjectCollection coll = new TfsTeamProjectCollection(url, nc);

            coll.EnsureAuthenticated();

            WorkItemStore workItemStore = coll.GetService<WorkItemStore>();
            Project teamproject = workItemStore.Projects["ABC"];
            WorkItemType workItemType = teamproject.WorkItemTypes["Issue"];

            WorkItem wi = new WorkItem(workItemType);

            wi.Title = ((TextBox)FormView1.FindControl("txtTaskTitle")).Text;   

            wi.Save();

        }

Can you please tell me what I can do to have the name of the Person who logged into the application as the one who created the TFS Work Item ?

I also tried the following :

       Uri url = new Uri("url");

        var collection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(url);
        var workItemStore = new WorkItemStore(collection, WorkItemStoreFlags.BypassRules);

        Project teamproject = workItemStore.Projects["ABC"];
        WorkItemType workItemType = teamproject.WorkItemTypes["Issue"];

        WorkItem wi = new WorkItem(workItemType);

        string s = "Name";
        wi.Fields["System.CreatedBy"].Value = s;

        wi.Title = "Test Item";
        wi.Save();

You could try setting the Created By field:

wi.Fields["System.CreatedBy"].Value = "TfsService";

Per the answer on this thread , you may need to be a member of the Project Collection Service Accounts.

Also, see this thread . It looks like you may only be able to modify the Created By field on the first revision... Or, you can try creating the WorkItemStore (instead of using the service) and using this flag: WorkItemStoreFlags.BypassRules

Try this:

TfsTeamProjectCollection coll = new TfsTeamProjectCollection(url,CredentialCache.DefaultCredentials);

Get rid of the whole Network credential thing.

You need to do two things. First follow Etienne's suggestion and either remove the credentials or pass the default.

Second you need to enable "impersonation" in your web application:http://msdn.microsoft.com/en-us/library/134ec8tc(v=vs.100).aspx

That works fine if your run your web application on your TFS server. However if you want to run it elsewhere you will also need a kerberos token to allow you to pass through the credentials to the TFS server. This is often called double-hop authentication. To get it working you have to do some giggery-pokery with Active Directory and setup Service Principal Names (SPN) for the account that you run your website under.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM