c# - SharePoint Online - 通过 CSOM 应用程序更新用户配置文件属性

标签 c# sharepoint csom sharepoint-online sharepoint-apps

我有一个控制台应用程序,我在其中通过 CSOM 更新 SharePoint Online 用户配置文件属性。当我通过下面的代码(使用用户名和密码)执行此操作时,它工作正常。

private static void UpdateUserProfileProperties()
{
    var userAccountName = "i:0#.f|membership|myAccounName@mySite.com";
    var siteUrl = "https://mySite-admin.sharepoint.com";
    var myUserName = "myUserName";
    var myPassword = "myPassword";
    var secureStringPassword = new SecureString();
    myPassword.ToList().ForEach(c => secureStringPassword.AppendChar(c));

    using (var context = new ClientContext(siteUrl))
    {
        var credentials = new SharePointOnlineCredentials(myUserName, secureStringPassword);
        context.Credentials = credentials;

        var peopleManager = new PeopleManager(context);
        var personProperties = peopleManager.GetPropertiesFor(userAccountName);

        context.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties);
        context.ExecuteQuery();
    }
}

现在,我已经在 SharePoint 中创建了一个应用程序(通过 AppRegNew.aspx 页面)。我授予应用租户级权限(通过 AppInv.aspx 页面)和以下权限请求 XML:

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

现在,我使用 clientID/clientSecret 代替用户名和密码进行身份验证 - 使用此代码:

public static void UpdateUserProfileProperties2()
{
    var userAccountName = "i:0#.f|membership|myAccounName@mySite.com";
    var siteUrl = "https://mySite-admin.sharepoint.com";
    var siteUri = new Uri(siteUrl);
    var siteRealm = TokenHelper.GetRealmFromTargetUrl(siteUri);
    var siteToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, siteRealm).AccessToken;

    using (var context = TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), siteToken))
    {
        var peopleManager = new PeopleManager(context);
        var personProperties = peopleManager.GetPropertiesFor(userAccountName);

        context.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties);
        context.ExecuteQuery();
    }
}

我可以很好地获取 token 和上下文。当行 context.ExecuteQuery(); 执行时,我得到以下异常:

Microsoft.SharePoint.Client.ServerUnauthorizedAccessException: Access denied. You do not have permission to perform this action or access this resource.

应用程序 clientID/clientSecret 适用于各种其他基于站点的操作。是否可以通过 clientID/clientSecret 在管理站点中加载用户属性?如果是这样,你能举个例子吗?如何授予应用适当的权限?

最佳答案

如果您为加载项授予了正确的权限:

  1. http://sharepoint/social/core
  2. http://sharepoint/social/tenant

那么唯一可能出错的就是用户权限。 SharePoint 代码的执行权限是加载项权限和当前用户权限的交集。

关于c# - SharePoint Online - 通过 CSOM 应用程序更新用户配置文件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29952917/

相关文章:

csom - 使用 Project Online 和 CSOM 进行身份验证时出现错误 401

c# - 使用 DynamoDBContext.Query<T> 和 QueryOperator 与使用 QueryFilter 和 List<ScanCondition> 在 .NET 中查询 DynamoDB

node.js - Node.js 在 SharePoint 上有什么用途?

c# - 浮点运算以及x86和x64上下文

sharepoint - Sharepoint 中长时间运行的工作流,它们是否阻止 w3wp 进程

sharepoint - __RemoteAppManagementInfo__ SharePoint 2013 - 未配置远程托管服务

c# - 如何迭代文档库中的文件

c# - Sharepoint 字段尚未在 C# 中初始化

c# - 知道 Iqueryable 的元素数量

c# - 关于 AverageTimer32 PerformanceCounter 的困惑