如何通过GraphAPI在SharePoint List中插入数据?



我有 SharePoint 列表,其中内容为参考编号。它的网址看起来像这样: https://xyz.sharepoint.com/sites/site_name/Lists/List_name/AllItems.aspx

此列表内容参考编号。我正在尝试将此数据插入列表中。

{
"Optimum_x0020_Case_x0020_Reference": "000777"
} 

这是我发布数据的网址。 https://graph.microsoft.com/v1.0/sites/xyz.sharepoint.com:/sites/site_name:/lists/List_names/items

但是我收到此错误:

error": {
"code": "accessDenied",
"message": "The caller does not have permission to perform the action.",

如何解决这个问题?使用访问权限,我可以为其他文档创建文件夹,子文件夹和更新元数据。

你这样做的背景是什么? 是您正在使用的应用程序吗? 您是在现有列表项还是新项上插入数据?

这是我必须用于 UWP 应用的代码。 我不确定这是否会对你有所帮助,但我希望它应该给你一点指导。 创建字典和弄清楚 XML 结构是我必须拼凑在一起以使我的代码正常工作的关键。

我在 App.xaml 中声明了我的范围.cs

public static string[] scopes = new string[] { "user.ReadWrite", "Sites.ReadWrite.All", "Files.ReadWrite.All" };

我有一个在主页上使用的提交按钮

private async void SubmitButton_Click(object sender, RoutedEventArgs e)
{
var (authResult, message) = await Authentication.AquireTokenAsync();
if (authResult != null)
{
await SubmitDataWithTokenAsync(submiturl, authResult.AccessToken);
}
}

这调用了我在类文件中拥有的AquireToken:

public static async Task<(AuthenticationResult authResult, string message)> AquireTokenAsync()
{
string message = String.Empty;
string[] scopes = App.scopes;
AuthenticationResult authResult = null;
message = string.Empty;
//TokenInfoText.Text = string.Empty;
IEnumerable<IAccount> accounts = await App.PublicClientApp.GetAccountsAsync();
IAccount firstAccount = accounts.FirstOrDefault();
try
{
authResult = await App.PublicClientApp.AcquireTokenSilentAsync(scopes, firstAccount);
}
catch (MsalUiRequiredException ex)
{
// A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token
System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
try
{
authResult = await App.PublicClientApp.AcquireTokenAsync(scopes);
}
catch (MsalException msalex)
{
message = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}";
}
}
catch (Exception ex)
{
message = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}";
}
return (authResult,message);
}

我为我的 SharePoint List 创建了另一个类

公共类 SharePointListItems { 公共类查找 { 公共字符串序列号 { get; set; } 公共字符串 ID { get; set; } 公共覆盖字符串 ToString(( { 返回序列号; } }

public class Value
{
public Lookup fields { get; set; }
}
public class Fields
{
[JsonProperty("@odata.etag")]
public string ODataETag { get; set; }
public string ParameterA { get; set; }
public string ParameterB { get; set; }
public string ParameterC { get; set; }
}
public class RootObject
{
[JsonProperty("@odata.context")]
public string ODataContext { get; set; }
[JsonProperty("@odata.etag")]
public string ODataETag { get; set; }
[JsonProperty("fields@odata.context")]
public string FieldsODataContext { get; set; }
public Fields Fields { get; set; }
}    
}

我使用此类创建了一个字典,用于将数据提交到 SharePoint。

public async Task<string> SubmitDataWithTokenAsync(string url, string token)
{
var httpClient = new HttpClient();
HttpResponseMessage response;
try
{
var root = new
{
fields = new Dictionary<string, string>
{
// The second string are public static strings that I
// I declared in my App.xaml.cs because of the way my app
// is set up.
{ "ParameterA", App.ParameterA },
{ "ParameterB", App.ParameterB },
{ "ParameterC", App.ParameterC },
}
};
var s = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
var content = JsonConvert.SerializeObject(root, s);
var request = new HttpRequestMessage(HttpMethod.Post, url);
//Add the token in Authorization header
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
request.Content = new StringContent(content, Encoding.UTF8, "application/json");
response = await httpClient.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();                
return responseString;
}
catch (Exception ex)
{
return ex.ToString();
}
}

我的提交网址被定义:

public static string rooturl = "https://graph.microsoft.com/v1.0/sites/xxxxxx.sharepoint.com,495435b4-60c3-49b7-8f6e-1d262a120ae5,0fad9f67-35a8-4c0b-892e-113084058c0a/";
string submiturl = rooturl + "lists/18a725ac-83ef-48fb-a5cb-950ca2378fd0/items";

你也可以在这里查看我发布的关于类似主题的问题。

最新更新