Reputation: 175
I have to update some Hostnames in Hyperlinks that are added to my WorkItems in TFS.
My thought was something like this:
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://mytfs"));
WorkItemStore wis = tfs.GetService<WorkItemStore>();
WorkItem wi = wis.GetWorkItem(12345);
foreach (Hyperlink link in wi.Links.OfType<Hyperlink>())
{
link.Location = link.Location.Replace("oldHostname", "newHostname");
}
wi.Save();
But unfortunately this doesn't work because the Location property is read-only.
Is there another way to update it?
Now i'm trying to remove the old and add the new hyperlink to the WorkItem but when the following foreach loop starts the second time i get an error.
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://mytfs"));
WorkItemStore wis = tfs.GetService<WorkItemStore>();
WorkItem wi = wis.GetWorkItem(14612);
foreach (Hyperlink hyperlink in wi.Links.OfType<Hyperlink>())
{
if (hyperlink.Location.Contains("oldHostname"))
{
Hyperlink newHyperlink = new Hyperlink(hyperlink.Location.Replace("oldHostname", "newHostname"));
wi.Links.Remove(hyperlink);
wi.Links.Add(newHyperlink);
}
}
if(wi.isDirty()) wi.Save();
The line containing foreach is marked and the error is:
An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.TeamFoundation.WorkItemTracking.Client.dll
Additional information: Operation is not valid due to the current state of the object.
How can i resolve this issue?
Upvotes: 2
Views: 1178
Reputation: 114571
.Replace
tends to return a new string, but not update the existing value in place. So you'll need to take the result:
link.Location = link.Location.Replace("oldHostname", "newHostname");
But since .Location
is read-only you'll have to remove the old link and create a new one to update the location
Upvotes: 4