我需要在本地将工作代码用于在线
需要进行一些更改才能在线上工作。主要是它对所有exceptin都显示相同的错误。
public static void writeLogsintoList(string Category_Name, string Method_Name, string Error_Message)
{
try
{
//UserCollection ouser = new UserCollection("[email protected]", "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 = "[email protected]";
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;
}
}