首页 > 解决方案 > 将日志写入列表时服务器代码错误 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

标签: c#sharepoint-2013

解决方案


首先尝试从 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;
            }
        }

推荐阅读