简体   繁体   中英

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. All the examples I have found use 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() .

This is the error message I am getting (I am using 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.

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. And I'm not 100% sure it's working correctly as it returns 200 sites in the initial request, which seems a little suspect.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM