Using SharePoint CSOM inside .Net Core application

被刻印的时光 ゝ 提交于 2020-01-02 07:13:16

问题


I want to get data from SharePoint list by CSOM library on .Net Core MVC application. There are absolutely no problem to achieve that on .Net Framework aplication because Microsoft.SharePoint.Client library contains ExecuteQuery method. Unfortunately .Net Core equivalent not.

I've made Async function

public async Task<ActionResult> GetRequests()
    {
        ClientContext context = new ClientContext("https://xxx/sites/xxx/");
        List certificatesList = context.Web.Lists.GetByTitle("Items");
        CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
        ListItemCollection items = certificatesList.GetItems(query);
        context.Load(items);
        Task spTask = context.ExecuteQueryAsync();
        await Task.WhenAll(spTask);
        var result = items;
        ...

    }

but it seems like it also won't work on .Net Core because it throws

Exception thrown: 'System.InvalidOperationException' in >System.Private.CoreLib.ni.dll

Additional information: Cannot find platform service library. For Windows Store application, please include Microsoft.SharePoint.Client.Runtime.WindowsStore.dll in the application package. For Windows Phone application, please include Microsoft.SharePoint.Client.Runtime.WindowsPhone.dll in the application package. For Windows application, please install Microsoft.SharePoint.Client.Runtime.Windows.dll in the GAC (Global Assembly Cache) or make it available for the Windows application.

Does someone use CSOM in .Net Core or should I use REST Api instead?

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  }

回答1:


I had this issue. For some reason when you reference the Microsoft.SharePointOnline.CSOM nuget package it used the .net framework dlls rather than the netcore45 dlls. Here's what I did to work around this:

  1. Download the nuget package https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM/
  2. Extract it to a subfolder in your project eg; Dependencies
  3. Reference the correct assemblies

    netcore45\Microsoft.SharePoint.Client.Portable.dll netcore45\Microsoft.SharePoint.Client.Runtime.Portable.dll net45\Microsoft.SharePoint.Client.Runtime.Windows.dll netcore45\Microsoft.SharePoint.Client.Runtime.WindowsStore.dll

Oh and if you already have that nuget package do an uninstall-package on it.



来源:https://stackoverflow.com/questions/42186888/using-sharepoint-csom-inside-net-core-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!