2
我下載並定製了此項目asp mvc todo sample。這工作正常,沒有任何與onedrive excel文件/項目的問題。但我必須僅使用Office 365 Sharepoint在線來完成此操作。 我能夠使用服務點如下得到文件ID:使用Microsoft Graph API使用Sharepoint文檔庫處理Excel文件
var serviceEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items";
但我不知道我應該如何定義工作簿終點URL與SharePoint更新/操作同一個Excel文件中的數據。如下當前工作簿終點URL(沒有工作):
var workbookEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items/" + fileId + "/workbook";
我的完整方法在SharePoint上的Excel文件更新單元格的值如下:
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);
}