1

在 MVA 课程 Building Blocks and Services of the SharePoint Platform 的模块 2 中,标题为“深入了解用于数据存储的 SharePoint 列表”(大约 45 分钟)Ted Pattison 演示了使用控制台应用程序在 SharePoint 上创建列表在线网站。该课程位于http://www.microsoftvirtualacademy.com/training-courses/deep-dive-building-blocks-and-services-of-sharepoint

我正在尝试在我的环境中做同样的事情,但我遇到了麻烦。

在演示中,他前往 _layouts/15/AppRegNew.aspx 在应用注册表中注册一个新应用。在演示中,页面顶部有一个“应用程序类型”单选按钮列表,其中包含“在 Web 服务器上运行的应用程序”和“在客户端计算机上运行的应用程序”选项。当我在我的网站上访问此页面时,没有这样的单选按钮列表。同样在演示中,Ted 将重定向 URL 留空。在我的网站上,它是必需的: 在此处输入图像描述 因此,为了解决这个问题,我输入了我网站的 URL(https://mydomain.sharepoint.com/sites/test)。应用 ID 创建成功: 在此处输入图像描述

然后我去 _layouts/15/AppInv.aspx 为应用程序提供安全性。我粘贴了 CAML 以授予应用程序对 Web 的读取访问权限:

  <AppPermissionRequests>
    <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="Read" />
  </AppPermissionRequests>

在此处输入图像描述 然后通过单击 Trust It 信任该应用程序:

在此处输入图像描述

然后我将应用注册中的值复制到我的 app.config 中:

 <add key="targetSiteUrl" value="https://xxxxx.sharepoint.com/sites/test"/>
    <add key="clientId" value="bf4c37ef-9202-41ba-8430-3983cba26285"/>
    <add key="clientSecret" value="nKGefHSvT69Ls2rwq1AIVyyHkIwlBzT9UkpbJMUcIbw="/>
    <add key="deleteOnly" value="false"/>

然后根据演示中的内容创建代码以获取网络标题:

static void Main(string[] args)
        {
            string siteUrl = ConfigurationManager.AppSettings["targetSiteUrl"];
            bool deleteOnly = ConfigurationManager.AppSettings["deleteOnly"].Equals("true");


                Uri siteUri = new Uri(siteUrl);
            string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
            var accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal,
                siteUri.Authority, realm).AccessToken;
            using (var clientContext = TokenHelper.GetClientContextWithAccessToken(siteUrl, accessToken)) {

                var web = clientContext.Web;
                clientContext.Load(web);
                clientContext.ExecuteQuery();

                Console.WriteLine(web.Title);

            }



        }

上面的代码获取领域和访问令牌并成功创建了 clientContext,但是当我运行 executeQuery 时,我总是收到错误 Microsoft.SharePoint.Client.ServerUnauthorizedAccessException。我尝试让应用 ID 完全控制 Web、网站集和租户,但我仍然遇到同样的错误。

如何让我的控制台应用程序拥有对我的站点的更新访问权限?

4

1 回答 1

1

在 appinv.aspx 中添加权限时需要设置 AllowAppOnlyPolicy

<AppPermissionRequests AllowAppOnlyPolicy="true" >
于 2015-02-26T13:53:26.310 回答