从Console App获取SharePoint站点的标题

use*_*554 10 c# sharepoint sharepoint-online

我有一个用C#编写的控制台应用程序.我需要从SharePoint站点获取一些信息.此SharePoint实例是Office 365(即SharePoint Online)的一部分.

我的挑战是,我不能使用帮助库.我必须使用基于REST的API,因为我使用的是.NET Core.

首先,我使用Azure Active Directory注册了我的控制台应用程序.此控制台应用程序是在我的Office 365环境所属的同一Azure Active Directory中创建的.我还将"Office 365 SharePoint Online"API的"读取所有网站集中的项目"权限授予了我的控制台应用程序.

在我的情况下,我有一个控制台应用程序坐在服务器上.我在SharePoint租户上设置了一个用户名/密码的测试用户.我还拥有我在Azure Active Directory中注册的控制台应用程序的客户端ID和重定向URL.

这时,我有一些看起来像这样的代码:

var accessToken = await GetToken(); // retrieves a token from Active Directory
using(var client = new HttpClient()) {
    client
        .DefaultRequestHeaders
        .Clear();

    client
        .DefaultRequestHeaders
        .Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

    client
        .DefaultRequestHeaders
        .Accept
        .Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var apiUrl = "https://[mySite].sharepoint.com/_api/web";

    // how do I get the title of the site at the apiUrl variable?
}
Run Code Online (Sandbox Code Playgroud)

因为我正在获取访问令牌,所以我觉得我很接近.我似乎无法弄清楚如何获得该网站的标题.如何从C#代码中获取SharePoint站点的标题?

Mar*_*eur 2

SharePoint REST API 的web资源包括一个Title表示网站标题的属性。

呼叫:

GET http://<site url>/_api/web/title
Run Code Online (Sandbox Code Playgroud)

返回:

GET http://<site url>/_api/web/title
Run Code Online (Sandbox Code Playgroud)

或者,假设您已将Accept标头设置为application/json

<d:Title xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
    xmlns:georss="http://www.georss.org/georss" 
    xmlns:gml="http://www.opengis.net/gml">Site Title Goes Here</d:Title>
Run Code Online (Sandbox Code Playgroud)