I downloaded and customized this project asp mvc todo sample. this is working fine without any issue with onedrive excel files/items. but i have to do this using office 365 sharepoint online only. i am able to get file id using service point as follows:
var serviceEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items";
But I am not sure how should I define workbook end point URL with Sharepoint to update/manipulate same excel file data. Current workbook end point URL as follows (which is not working):
var workbookEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items/" + fileId + "/workbook";
My complete method to update cell value in excel file on Sharepoint as follows:
public static async Task UpdateExcelCellValue_Sharepoint(string accessToken, string cellAddress, string val)
{
string worksheetName = "Parameters";
var serviceEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var filesResponse = await client.GetAsync(serviceEndpoint + "?$select=name,id");
if (filesResponse.IsSuccessStatusCode)
{
var filesContent = await filesResponse.Content.ReadAsStringAsync();
JObject parsedResult = JObject.Parse(filesContent);
foreach (JObject file in parsedResult["value"])
{
var name = (string)file["id"];
if (name.Contains("173"))
{
fileId = (string)file["id"];
break;
}
}
}
else
{
//Handle failed response
}
//Set up workbook and worksheet endpoints
var workbookEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items/" + fileId + "/workbook";
var worksheetsEndpoint = workbookEndpoint + "/worksheets";
var patchMethod = new HttpMethod("PATCH");
var summaryTableRowJson = "{" +
"'values': '" + val + ".csv'" +
"}";
var colNamePatchBody = new StringContent(summaryTableRowJson);
colNamePatchBody.Headers.Clear();
colNamePatchBody.Headers.Add("Content-Type", "application/json");
var colNameRequestMessage = new HttpRequestMessage(patchMethod, worksheetsEndpoint +
"('" + worksheetName + "')/range(address='Parameters!B2')")
{ Content = colNamePatchBody };
var colNameResponseMessage = await client.SendAsync(colNameRequestMessage);
}