Reading valid Project level data using REST API in Project Online
Hello Readers !
As part of one of the Project online integration with external web application solution, found that the data which we read using CSOM APIs for Project start date, finish date and %Complete were not matched. The data what we have got in the CSOM coding was different from what we see in the Project center page. I was able to achieve it with the ODATA Rest API call. Below is the sample code to read Project start date, Finish date and %Complete using ODATA REST API from C# code.
var projectDatesWebUri = new Uri(pwaUrl + "/_api/ProjectData/Tasks()?$select=TaskStartDate,TaskFinishDate,TaskPercentCompleted&$filter=(ProjectId eq guid'" + projectOnineGuidId + " ' and TaskIsProjectSummary eq true)"); var restResponse = await this.GetPjoRestResponseAsync(projectDatesWebUri, this.context.Credentials); if (restResponse.Count() > 0) { startDate = DateTime.Parse(Convert.ToString(restResponse.FirstOrDefault()[TaskStartDateString])).ToShortDateString(); endDate = DateTime.Parse(Convert.ToString(restResponse.FirstOrDefault()[TaskFinishDateString])).ToShortDateString(); projectPercentComplete = Convert.ToString(restResponse.FirstOrDefault()[TaskPercentCompletedString]); } //function to execute and read REST API request. public Task<JToken> GetPjoRestResponseAsync(Uri webUri, ICredentials credentials) { using (var handler = new HttpClientHandler() { Credentials = credentials }) { handler.CookieContainer.SetCookies(webUri, ((SharePointOnlineCredentials)credentials).GetAuthenticationCookie(webUri)); using (var client = new HttpClient(handler)) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync(webUri).GetAwaiter().GetResult(); response.EnsureSuccessStatusCode(); string jsonData = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); var t = JToken.Parse(jsonData); return t["value"]; } } }
Hope, this may help someone.
Thanks for reading. 🙂
Recent Comments