5

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.

3
  • I guess it wont work in .net Core
    – Gaurravs
    Commented Aug 6, 2019 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 Commented Aug 6, 2019 at 12:53
  • aexecutequeryasync() => wait() Commented May 17, 2020 at 7:33

2 Answers 2

3

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 = "[email protected]";
            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();
        }
    }
}
1
  • 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. Commented Aug 7, 2019 at 6:16
0

MS has released new version few days back with .net standard version. https://developer.microsoft.com/en-us/microsoft-365/blogs/net-standard-version-of-sharepoint-online-csom-apis/

Hope we can create React (which is Microsoft promoting these days with SPFx) easily. So all type of SharePoint applications can work under React roof.

This only supports SP online. No support for SP-onprem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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