c# - 将日志写入列表时服务器代码错误 403
问题描述
我需要在本地使用这个工作代码到在线
需要进行一些更改才能在线工作。主要是它对所有异常都显示相同的错误。
public static void writeLogsintoList(string Category_Name, string Method_Name, string Error_Message)
{
try
{
//UserCollection ouser = new UserCollection("Suyog.m@totalebizsolutions.com", "Wlcm$$2003");
//Microsoft.SharePoint.Client.ClientResult<PrincipalInfo>
//request.UseDefaultCredentials = true;
//request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
ClientContext context = new ClientContext(ApplicationSiteUrl);
List announcementsList = context.Web.Lists.GetByTitle("Logs");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newItem = announcementsList.AddItem(itemCreateInfo);
newItem["Timestamp"] = DateTime.Now;
newItem["Category_Name"] = Category_Name;
newItem["Method_Name"] = Method_Name;
newItem["Error_Message"] = Error_Message;
newItem.Update();
context.ExecuteQuery();
Console.WriteLine("added");
Console.ReadLine();
}
catch (Exception ex)
{
throw ex;
}
```[enter image description here][1]
[1]: https://i.stack.imgur.com/HX2Pg.png
actually its happening for all exceptions
解决方案
首先尝试从 Nuget 下载 SharePointOnline.CSOM:
然后使用 SharePointOnlineCredentials 类传递凭据,下面是代码片段供您参考:
static void Main(string[] args)
{
string password = "*******";
string account = "user@Tenant.onmicrosoft.com";
var secret = new System.Security.SecureString();
foreach (char c in password)
{
secret.AppendChar(c);
}
var credentials = new SharePointOnlineCredentials(account, secret);
using (ClientContext ctx = new ClientContext("https://Tenant.sharepoint.com/sites/sitename/"))
{
ctx.Credentials = new SharePointOnlineCredentials(account, secret);
ctx.Load(ctx.Web);
ctx.ExecuteQuery();
writeLogsintoList(ctx, "TestCategory", "TestMethod", "TestErrorMessage");
}
}
public static void writeLogsintoList(ClientContext context, string Category_Name, string Method_Name, string Error_Message)
{
try
{
List announcementsList = context.Web.Lists.GetByTitle("Logs");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newItem = announcementsList.AddItem(itemCreateInfo);
newItem["Timestamp"] = DateTime.Now;
newItem["Category_Name"] = Category_Name;
newItem["Method_Name"] = Method_Name;
newItem["Error_Message"] = Error_Message;
newItem.Update();
context.ExecuteQuery();
Console.WriteLine("added");
Console.ReadLine();
}
catch (Exception ex)
{
throw ex;
}
}
推荐阅读
- javascript - 为什么 Cordova Android 应用程序会抛出此警告:“[Intervention] Ignored attempt to cancel a touchstart event...”?
- java - 如何通过poi为word中的不同部分设置页码
- javascript - js 正则表达式未按预期工作。未检测到换行符
- java - 带有意外符号的 log4j2 异常输出
- javascript - 使用 mongoose 填充不会返回目标参考集合
- python - 为什么提供的程序的输出打印“1:1, 2:1, 3:2, -1:1” 而不是“0:2, 1:1, 2:1, 3:2, -1 :1”?
- php - PHP Google Cloud Vision API:注释立即淹没内存
- r - R:将数据压缩到数据矩阵中?
- c++ - 在 FBX 中,如何知道哪些顶点索引对应于哪些控制点索引?
- javascript - 为什么作为数组元素传递的对象键会在 javascript 中引发语法错误?