C# Accessing a sharepoint site using the hostname and relative site path using Graph API

S.Raghu Nathan 1 Reputation point
2022-05-24T13:48:01.537+00:00

205088-image.png

My requirement is, with proper permissions in SharePoint, I should be able to get the Site details using the Graph API by specifying the SharePoint domain (host) name and site path.

  • User [testuser@harsh.com .com] is already added as Owner to the Private Group [sites/MyPrivateGroup]
  • I am able to log in to the SharePoint site from the browser using the testuser@harsh.com .com ID without any errors.
  • Able to upload files, created document libraries, and edit files using the same id - When trying to access the site using the Graph API, getting the following error "error": {
            "code": "InternalServerError",
            "message": "Unable to find target address",
            "innerError": {
                "date": "2022-05-24T13:36:46",
                "request-id": "b7b083a7-5345-4380-9f68-416ace4e0a63",
                "client-request-id": "f5f6f16b-5011-7c0f-0b41-26e24daad709"
            }

I tried the following Graph API Endpoint via Microsoft Graph Explorer
https://graph.microsoft.com/v1.0/sites/{host-name}:/{server-relative-path}

{host-name} - "myofficedomain.sharepoint.com"
{server-relative-path} - "sites/MyPrivateGroup"

Any help would be really appreciated.

Thank you
Raghunathan S

Microsoft 365 and Office SharePoint Development
Microsoft Security Microsoft Graph
Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AtulThorat-MSFT 391 Reputation points
    2022-05-24T19:33:05.153+00:00

    Hi @S.Raghu Nathan ,

    Hope you are doing well.

    With C# code and graph SDK, you can try below code to get a sharepoint site using the hostname and relative site path.

    GraphServiceClient graphClient = new GraphServiceClient( authProvider );

    var site = await graphClient.Sites["{hostname}:"].Sites["{server-relative-path}"].Request().GetAsync();

    Hope this helps.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

  2. Tong Zhang_MSFT 9,251 Reputation points
    2022-05-31T05:14:59.883+00:00

    Hi @S.Raghu Nathan ,

    Have you tried AtulThorat-MSFT' answer, feel free to reply if you still have any questions.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.