[英]MS Graph SDK: User's SharePoint FollowedSites
I am trying to get a list of the SharePoint sites a user follows using the MSGraph SDK V4.54.0.我正在尝试使用 MSGraph SDK V4.54.0 获取用户关注的 SharePoint 站点的列表。 All the examples I have found use await graphClient.Me.FollowedSites.Request().GetAsync()
.我发现的所有示例都使用await graphClient.Me.FollowedSites.Request().GetAsync()
。 I am not using Me
, but a specific user so I am accessing it as such await graphClient.Users[userEmailAddress].FollowedSites.Request().GetAsync()
.我没有使用Me
,而是使用特定用户,所以我正在访问它await graphClient.Users[userEmailAddress].FollowedSites.Request().GetAsync()
。
This is the error message I am getting (I am using MSTest):这是我收到的错误消息(我正在使用 MSTest):
Microsoft.Graph.ServiceException: Code: generalException
Message: General exception while processing
Inner error:
AdditionalData:
date: 2023-05-31T14:19:42
request-id: ***
client-request-id: ***
ClientRequestId: ***
Stack Trace:
<SendAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
<SendRequestAsync>d__40.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
<SendAsync>d__34`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
<GetAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
TaskAwaiter`1.GetResult()
<SharePointSite_TestFollowedSites>d__88.MoveNext() line 2364
--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
ThreadOperations.ExecuteWithAbortSafety(Action action)
It strikes me that although the SDK exposes it, there may be no endpoint in the API for Users[x].FollowedSites.让我印象深刻的是,尽管 SDK 公开了它,但 API 中可能没有针对 Users[x].FollowedSites 的端点。
I have written the following code, but as you can see, it iterates all possible sites, looks for the "User Information List", and then finally in the items for the user.我写了下面的代码,但是如您所见,它遍历所有可能的站点,查找“用户信息列表”,最后在用户的项目中查找。 Needless to say, this is extremely inefficient and takes ages.不用说,这是非常低效的并且需要很长时间。 Approx, 20 minutes.大约 20 分钟。 And I'm not 100% sure it's working correctly as it returns 200 sites in the initial request, which seems a little suspect.而且我不能 100% 确定它是否正常工作,因为它在初始请求中返回 200 个站点,这似乎有点可疑。
MSGraphClient graph = GetMSGraphClient_WithClientSecret();
SharePointClient spClient = graph.SharePoint;
var defaultField = default(KeyValuePair<string, object>);
var _sites = new List<Site>();
var sites = await graph.Client
.Sites
.Request()
.GetAsync();
foreach (var site in sites)
{
var list = (await graph.Client
.Sites[site.Id]
.Lists
.Request()
.Filter("displayName eq 'User Information List'")
.GetAsync()).FirstOrDefault();
if (list != null)
{
var items = await graph.Client
.Sites[site.Id]
.Lists[list.Id]
.Items
.Request()
.GetAsync();
foreach (var item in items)
{
var fields = await graph.Client
.Sites[site.Id]
.Lists[list.Id]
.Items[item.Id]
.Fields
.Request()
.GetAsync();
var foundField = fields.AdditionalData.FirstOrDefault((_f) => _f.Key == "UserName" && _f.Value.ToString().ToLower() == graph.AuthenticationOptions.UserName.ToLower());
if (!foundField.Equals(defaultField))
{
_sites.Add(site);
break;
}
}
}
}
bool actual = _sites.Count > 0;
Assert.IsTrue(actual);
Is there a better/more efficient way of getting the sites a user follows?有没有更好/更有效的方法来获取用户关注的网站?
Check you have permissions to view the users followed sites.检查您是否有权查看用户关注的网站。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.