【问题标题】:c# Update SharePoint User Property CSOM + Pnp Frameworkc# 更新 SharePoint 用户属性 CSOM + Pnp 框架
【发布时间】:2021-05-22 14:00:51
【问题描述】:

当我们将项目从 .net 4.7 迁移到 .Net core 3.1 时,我必须找到 SharePointOnlineCredentials 类的替代品。

为了生成我的 ClientContext,我现在使用 Pnp.Framework 包中的 AuthenticationManager。

但是,我无法再更新 Sharepoint 用户属性。 我得到了错误:

'访问被拒绝。您无权执行此操作或访问此资源。'

每当我使用 SharePoint 管理员用户(如以前)或具有 Uer.ReadWrite.All 权限的已注册 AAD 应用程序时。

我的测试 SharePoint 管理员用户帐户:

using(var am = PnP.Framework.AuthenticationManager.CreateWithCredentials(clientId, userName, password))
using (var clientContext = am.GetContext(adminSiteUrl))
{
    var peopleManager = new PeopleManager(clientContext);
    var personProperties = peopleManager.GetPropertiesFor(accountName);
    clientContext.Load(personProperties);
    clientContext.ExecuteQuery();

    peopleManager.SetSingleValueProfileProperty(personProperties.AccountName, "officeKey", "MTR");

    clientContext.ExecuteQuery(); //Throws the 'Access Denied' exception here
}

我对注册客户端应用的测试(使用证书)

using (var am = new PnP.Framework.AuthenticationManager(clientId, certificatePath, certificatePassword, tenant))
using (var clientContext = am.GetContext(adminSiteUrl))
{
    var peopleManager = new PeopleManager(clientContext);
    var personProperties = peopleManager.GetPropertiesFor(accountName);
    clientContext.Load(personProperties);
    clientContext.ExecuteQuery();

    peopleManager.SetSingleValueProfileProperty(personProperties.AccountName, "officeKey", "MTR");

    clientContext.ExecuteQuery(); //Throws the 'Access Denied' exception here
}

注意:我在 AAD 中注册的应用程序客户端拥有所有 SharePoint API 权限。

我的项目正在引用包:

  • Microsoft.SharePointOnline.CSOM (16.1.20912.12000)
  • Pnp.Framework (1.2.0)

【问题讨论】:

    标签: c# sharepoint csom


    【解决方案1】:

    我终于设法通过在 SharePoint(而不是 AAD)中注册一个应用程序来让它工作。 您首先需要通过创建 clientId 和 clientSecret 来注册应用程序:https://<tenant>-admin.sharepoint.com/_layouts/15/appregnew.aspx 然后你需要授予权限:https://<tenant>-admin.sharepoint.com/_layouts/15/appinv.aspx 您需要授予以下权限:

    <AppPermissionRequests AllowAppOnlyPolicy="true">
      <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
      <AppPermissionRequest Scope="http://sharepoint/social/tenant" Right="FullControl" />
    </AppPermissionRequests>
    

    然后您将能够获取用户属性:

    using (var clientContext = new PnP.Framework.AuthenticationManager().GetACSAppOnlyContext(siteUrl, appId, appSecret))
    {
        var peopleManager = new PeopleManager(clientContext);
        var personProperties = peopleManager.GetPropertiesFor(accountName);
        clientContext.Load(personProperties);
        clientContext.ExecuteQuery();
    
        peopleManager.SetSingleValueProfileProperty(personProperties.AccountName, "officeKey", "MTR");
    
        clientContext.ExecuteQuery(); //Throws the 'Access Denied' exception here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 2019-09-07
      相关资源
      最近更新 更多