gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:25:34 24 4
gpt4 key购买 nike

我有一个控制台应用程序,我在其中通过 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com