Search code examples
c#tfstfs-sdk

WorkItemHttpClient - TFS work item not found or not permission to read it


Please find the below Wiql query. (Get all work items from a date - to date)

 @"SELECT *  FROM WorkItems  WHERE [System.TeamProject] = '" + _teamProjectName + "'  and( [System.ChangedDate] >= '" + fromDate + "' and [System.ChangedDate] <= '" + toDate + "')  ORDER BY [System.AssignedTo]"

I am getting all the work items from this query as per below code. (Please let me know if the query is correct) . But whenever I try to call this function GetWorkItemsAsync I get an error -

TFS work item 'xyz' does not exist or you don't have permission to read it

Essentially the fromDate is the start of the week, toDate is the end of the week.

 using (WorkItemTrackingHttpClient workItemTrackingHttpClient = connection.GetClient<WorkItemTrackingHttpClient>())
            {
                //execute the query to get the list of work items in the results
                WorkItemQueryResult workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;

                IEnumerable<WorkItemReference> workItemRefs = workItemQueryResult.WorkItems;


                List<DateTime> weekDatesToGetWorkItems = weekDates();


                foreach (var item in weekDatesToGetWorkItems)
                {
                    try
                    {

                        int skip = 0;
                        const int batchSize = 100;

                        do
                        {
                            workItemRefs = workItemQueryResult.WorkItems.Skip(skip).Take(batchSize);
                            if (workItemRefs.Any())
                            {
                                //Exception here
                                List<WorkItem> workItems = workItemTrackingHttpClient.GetWorkItemsAsync(workItemRefs.Select(wir => wir.Id), this.GetFieldNames(), item).Result;
                                }
                        }
                        while (workItemRefs.Count() == batchSize);

                    }
                    catch (Exception ex)
                    {
//// Always gives an exception for one of the workItem "TFS work item xyz does not exist or you don't have permission to read it"
                    }

                }
}

Question 1: Any idea how the above exception can be debugged or resolved?

Question 2: I already tried getting workitem data with GetWorkItemAsync by passing single workitemId. This works but the data which I get is not synced with the latest data, it always gives me data till prior week. Is there a proper way to get the latest one?

Any help over this issue is much appreciated.


Solution

  • I got an answer to my Second question. Seems that i was doing a silly mistake with the time part.

    I added the timestamp value and now I was able to get the latest data.

       List<WorkItem> workItems = workItemTrackingHttpClient.GetWorkItemsAsync(workItemRefs.Select(wir => wir.Id), this.GetFieldNames(),
     item.Add(new TimeStamp(23,59,59))).Result;
     //// Adding timestamp worked here.
    

    So next time if you are stuck with something which plays with dates never ignore the Time part. (Silly but always goes unnoticed).

    Happy coding!! cheers!