1

Is there any option to execute CSOM code from .net core application? I have a bot solution from this i need to store all the conversation details in SharePoint list. When executing the code, i am getting 400 bad request error. How to resolve this? This is SharePoint online. My code will be published in Azure environment.

My code,

   try
        {
            using (var ctx = new ClientContext(siteUrl))
            {
                ctx.Credentials = new SharePointOnlineCredentials(userName, password);
                Web web = ctx.Web;
                ctx.Load(web, w => w.Title);
                ctx.ExecuteQueryAsync();
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Able to access Your site. Title - " + ctx.Web.Title);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unable to access your site. Please find the error below.");
            Console.WriteLine(ex);
        }

Getting "Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException: The property or field 'Title' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested." error.

  • I guess it wont work in .net Core – Gaurravs Aug 6 at 12:53
  • storing bot conversations in a SharePoint list is going to give you scaling issues very quickly. I'd use a cosmos db or Azure table storage if I were you – Derek Gusoff Aug 6 at 12:53
0

We can install the package as below.

Install-Package TTCUE.NetCore.SharepointOnline.CSOM.16.1.8029.1200 -Version 16.1.8029.1200

More information is here: TTCUE.NetCore.SharepointOnline.CSOM.16.1.8029.1200

Or use the following solution from GitHub: NetCore.CSOM

Or follow the steps below.

1.Create a .NET Core console app.

2.Add the references: Microsoft.SharePoint.Client.Portable.dll, Microsoft.SharePoint.Client.Runtime.Portable.dll, and Microsoft.SharePoint.Client.Runtime.Windows.dll.

Note: If the project has references to Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll, please remove them.

These references can be accessed by installing CSOM library into another project, and then navigating to installed nuget packages in the file directory: c:\Users\user\.nuget\packages\microsoft.sharepointonline.csom\(version)\lib\netcore45

3.Add the code below to the .NET Core 2.0 console application:

namespace ConsoleApp1 {
    class Program {
        static void Main(string[] args) {
            string targetSiteURL = @"https://xxx.sharepoint.com/sites/xxx";

            var login = "xxx@xx.onmicrosoft.com";
            var password = "xxx";

            SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, password);

            ClientContext ctx = new ClientContext(targetSiteURL);

            ctx.Credentials = onlineCredentials;

            Web web = ctx.Web;
            ctx.Load(web, w => w.Title);
            ctx.ExecuteQueryAsync();        
            Console.WriteLine("Web title:" + web.Title);
            Console.ReadKey();
        }
    }
}
  • I have added it and now i am not receiving error when executing the query. But still i am getting error. Added code and error message in question. – Sriram Tamilarasan Aug 7 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.