C# – insert work item into tfs using tfs api

asp.netctfstfs-sdkvisual studio

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();

Best Answer

Try this:

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

Get rid of the whole Network credential thing.

Related Topic