2
votes

I am currently working on retrieving the siteId and listId from shared folders. So far I manage to do it on a Sharepoint folder where I have access to the Sharepoint root but when I use the URL of a shared Sharepoint location I am having the following error message: "Invalid hostname for the tenancy"

For a bit of context, my usecase is the following one:

  • A colleague of mine shares a folder with me from a Sharepoint (xxx.sharepoint.com).
  • I have access to his folder.
  • If I simply go to the root (xxx.sharepoint.com) I am having "access denied" which is fine (I believe).
  • I want to retrieve the sideId, listId (and eventually driveId) from the shared folder using the URL of this folder.

So far, as I said previously, I am able to retrieve those informations for a sharepoint folder where I do have access to the root. Unfortunatly, in my usecase previously describe I have an error and it is impossible to me to retrieve it even by retrieving all sites which I have access to or by getting the list from the folder URL.

The piece of code that I am using so far:

public async Task<List> UrlToList(string accessToken, string url)
{
    url = url.Replace("/Forms/AllItems.aspx", "");
    Uri uri = new Uri(url);
    var segments = string.Join("", uri.Segments[0..^1]).TrimEnd('/');

    var request = graphClient.Sites[uri.Host].SiteWithPath(segments).Lists.Request(Options(accessToken));
    var lists = await request.GetAsync();

    var list = lists.First(l => string.Compare(url, l.WebUrl, true) == 0);
    return list;
}

I only see two options so far... It could come from the configuration of xxx.sharepoint.com or a problem of permission.

Is there any possible way to retrieve those informations? If so, how? And is there also any code sample available somewhere?

1

1 Answers

1
votes

After experimenting different solution, the exact reason of this error due to a different tenancy between the user and the sharepoint destination.

To solve it you need first to do a HTTP Get request to the url xxx.sharepoint.com/site/subsite/Forms/AllItems.aspx with an empty bearer. From that you will find the tenantId of the sharepoint destination.

Once you have the tenantId you will need to request a new access token with this tenantId like the following sample.

public async Task<string> GetAccessToken(string clientId, string clientSecret, string scope, string refreshToken, string grantType = "refresh_token", string tenantId = "common")
{
    var request = new HttpRequestMessage(HttpMethod.Post, $"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token");
    request.Content = new FormUrlEncodedContent(new Dictionary<string, string>() {
        {"client_id", clientId },
        {"scope", scope },
        {"refresh_token", refreshToken },
        {"grant_type", grantType },
        {"client_secret", clientSecret }
    });

    HttpResponseMessage response = await client.SendAsync(request);

    response.EnsureSuccessStatusCode();

    var result = await response.Content.ReadAsStringAsync();
    return result;
}

With this access token if you request the sideId, listId,... of the given sharepoint destination you will be able to retrieve them.

Hope it will help you with this answer.