【问题标题】:Issue Executing Code Sharepoint C# Console App to get Sharepoint information using APIs问题执行代码 Sharepoint C# 控制台应用程序以使用 API 获取 Sharepoint 信息
【发布时间】:2020-04-28 02:17:11
【问题描述】:

我正在使用以下代码获取有关 Sharepoint 文档的信息:

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    class Class1
    {    
        static void Main(string[] args)    
        {    
          //make changes based on your site url
          HttpWebRequest endpointRequest =(HttpWebRequest)HttpWebRequest.Create("https://company.sharepoint.com/_api/search/query?querytext='docName");
          endpointRequest.Method = "GET";
          endpointRequest.Accept = "application/json;odata=verbose";
          endpointRequest.Headers.Add("Authorization","Bearer " + accessToken);
          HttpWebResponse endpointResponse =(HttpWebResponse)endpointRequest.GetResponse();     
        }
    }
}

但我遇到以下错误:

严重性代码描述项目文件行抑制状态 错误 CS0234 类型或命名空间名称“SharePoint”不存在于 命名空间“Microsoft”(您是否缺少程序集 参考?) ConsoleAppSP C:\Program.cs 1 Active

如何将 Microsoft Sharepoint 库添加到 Visual studio 2019 Preview
(我是 C#、Visual Studio 和 SharePoint 的新手)。

【问题讨论】:

  • 您是否尝试过检查.NetFramework 是否设置正确?
  • 另外,您使用的是哪个版本的 Sharepoint?
  • @AlessandraAmosso 我使用了 Micrsoft Sharepoint 15.04 的 nupkg。现在,sharepoint 问题已解决,但请告诉我如何使用用户名密码或域名填充 accessToken 以用于 sharepoint。
  • 您是否已经检查过this 文档?

标签: c# visual-studio sharepoint dll


【解决方案1】:

以下步骤供您参考。

1.使用 Visual Studio 创建控制台应用程序。

2.使用下面的 Nuget 安装 SharePoint Online 客户端库。

Install-Package Microsoft.SharePointOnline.CSOM -Version 16.1.19515.12000

3.将以下代码复制到Program.cs。

using System;
using System.Security;
using Microsoft.SharePoint.Client;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string userName = "admin@tenant.onmicrosoft.com";
            string password = "xxx";

            string apiUrl = "https://tenant.sharepoint.com/_api/search/query?querytext='docName'";
            var securePassword = new SecureString();
            foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
            var credential = new SharePointOnlineCredentials(userName, securePassword);
            Uri uri = new Uri(apiUrl);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "GET";
            request.Credentials = credential;
            request.Headers[HttpRequestHeader.Cookie] = credential.GetAuthenticationCookie(new Uri(apiUrl), true);  // SPO requires cookie authentication
            request.Headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f";

            HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
            Stream webStream = webResponse.GetResponseStream();
            StreamReader responseReader = new StreamReader(webStream);
            string response = responseReader.ReadToEnd();

        }
    }
}

4.修改租户名称、用户名和密码使其生效。

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 2018-11-19
    • 2011-01-11
    • 2022-10-21
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多