{"id":23213793,"url":"https://github.com/nitin27may/sharepoint-graph-api","last_synced_at":"2025-06-26T17:34:01.299Z","repository":{"id":268759884,"uuid":"901466913","full_name":"nitin27may/sharepoint-graph-api","owner":"nitin27may","description":"A .NET 9 reference implementation demonstrating how to integrate with Microsoft Graph to perform seamless file operations on SharePoint Online.","archived":false,"fork":false,"pushed_at":"2025-05-21T23:11:42.000Z","size":22,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-22T00:23:33.801Z","etag":null,"topics":["dotnet9","graphapi","microsoftgraphapi","net9","sharepoint-online","spo"],"latest_commit_sha":null,"homepage":"https://nitinksingh.com/implementing-sharepoint-file-crud-operations-using-microsoft-graph-api/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nitin27may.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-12-10T17:57:24.000Z","updated_at":"2025-05-21T23:11:45.000Z","dependencies_parsed_at":"2024-12-18T19:19:23.123Z","dependency_job_id":"e3f6bae0-ea13-47fb-8daf-d6808b3d251f","html_url":"https://github.com/nitin27may/sharepoint-graph-api","commit_stats":null,"previous_names":["nitin27may/sharepoint-graph-api"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nitin27may/sharepoint-graph-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nitin27may%2Fsharepoint-graph-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nitin27may%2Fsharepoint-graph-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nitin27may%2Fsharepoint-graph-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nitin27may%2Fsharepoint-graph-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nitin27may","download_url":"https://codeload.github.com/nitin27may/sharepoint-graph-api/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nitin27may%2Fsharepoint-graph-api/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262113734,"owners_count":23261092,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dotnet9","graphapi","microsoftgraphapi","net9","sharepoint-online","spo"],"created_at":"2024-12-18T19:19:20.237Z","updated_at":"2025-06-26T17:34:01.290Z","avatar_url":"https://github.com/nitin27may.png","language":"C#","readme":"# SharePoint Online File Operations using Microsoft Graph API\n\nThis .NET 9 project demonstrates how to perform file operations in SharePoint Online using the Microsoft Graph API with OAuth 2.0 authentication and delegated access. The application facilitates seamless file interactions, leveraging modern authentication techniques and the capabilities of the Graph API.\n\n## What is Microsoft Graph API?\n\nMicrosoft Graph API is a unified API endpoint that provides access to Microsoft 365 data and intelligence. It serves as a single endpoint (`https://graph.microsoft.com`) to access data across various Microsoft services including SharePoint, OneDrive, Outlook, Teams, and more. Key advantages include:\n\n- **Unified Access** - A single API to access data across multiple Microsoft services\n- **Consistent Data Model** - Standardized entities and relationships\n- **Rich SDK Support** - Official SDKs for .NET, JavaScript, Java, Python, and more\n- **Modern Authentication** - OAuth 2.0 and OpenID Connect support\n- **Intelligence \u0026 Insights** - AI-powered features and analytics\n\nIn this project, we leverage Microsoft Graph API to interact with SharePoint Online document libraries and files.\n\n## Features\n- OAuth 2.0 authentication with delegated access\n- File operations on SharePoint Online, such as:\n  - Uploading files\n  - Downloading files\n  - Deleting files\n  - Listing files\n  - Updating file metadata\n- Easy-to-use interface for managing files in SharePoint document libraries\n- Distributed caching for improved performance\n\n## Prerequisites\n\nBefore running the application, ensure you have the following:\n\n- A Microsoft 365 tenant with SharePoint Online enabled\n- Azure Active Directory (AAD) App Registration with:\n  - Client ID\n  - Tenant ID\n  - Client Secret\n  - Redirect URI configured for OAuth 2.0 authentication\n- API Permissions granted in Azure AD App Registration:\n  - `Sites.ReadWrite.All` - Required for accessing and modifying SharePoint sites\n  - `Files.ReadWrite.All` - Required for file operations\n- Development environment:\n  - Visual Studio 2022 or later\n  - .NET 9 SDK installed\n  - (Optional) Bruno API client for testing\n\n### Setting Up Azure AD Application\n\n1. Go to [Azure Portal](https://portal.azure.com/) and navigate to **Azure Active Directory**\n2. Select **App registrations** and click **New registration**\n3. Enter a name for your application and select the appropriate account type\n4. Set the Redirect URI to your application's callback URL\n5. Click **Register**\n6. Navigate to **API permissions** and add the following permissions:\n   - Microsoft Graph API \u003e Application permissions \u003e Sites.ReadWrite.All\n   - Microsoft Graph API \u003e Application permissions \u003e Files.ReadWrite.All\n7. Click **Grant admin consent** for these permissions\n8. Navigate to **Certificates \u0026 secrets** and create a new client secret\n9. Note down the Client ID, Tenant ID, and Client Secret for configuration\n\n## Getting Started\n\n### 1. Clone the Repository\n```bash\ngit clone https://github.com/nitin27may/sharepoint-graph-api\ncd sharepoint-graph-api\n```\n\n### 2. Configure the Application\n1. Open the `appsettings.json` file and update the placeholders with your Azure AD App Registration details:\n    ```json\n    {\n    \"GraphApiSettings\": {\n        \"ClientId\": \"\u003cYOUR-CLIENT-ID\u003e\",\n        \"TenantId\": \"\u003cYOUR-TENANT-ID\u003e\",\n        \"ClientSecret\": \"\u003cYOUR-CLIENT-SECRET\u003e\",\n        \"Scope\": \"https://graph.microsoft.com/.default \",\n        \"BaseGraphUri\": \"https://graph.microsoft.com/v1.0\",\n        \"BaseSpoSiteUri\": \"\u003cYOUR-BASE-SHAREPOINT-SITE-Name\u003e.sharepoint.com\"\n        }\n    }\n    ```\n\n### 3. Build and Run the Application\n1. Open the project in Visual Studio.\n2. Restore NuGet packages:\n   ```bash\n   dotnet restore\n   ```\n3. Build the solution:\n   ```bash\n   dotnet build\n   ```\n4. Run the application:\n   ```bash\n   dotnet run\n   ```\nNote: We are using [Bruno API client](https://www.usebruno.com/downloads) for the testing Rest API, the files are added in Api Collection Folder\n\n### 4. Authenticate\n- This project has `GraphApiCientFactory` which is handling the authentication.\n- Upon successful authentication, the application will obtain an access token to interact with SharePoint Online via the Graph API.\n\n## Usage\n\nThis section provides detailed instructions on how to use the API endpoints for SharePoint file operations.\n\n### Understanding SharePoint Hierarchy\n\nIn SharePoint Online, the hierarchy is:\n- **Site**: A collection of document libraries and other content\n- **Drive**: A document library within a site (e.g., \"Documents\")\n- **Path**: The location within a drive where files are stored\n\n### Important Notes\n\n- Use `root` as the `siteName` parameter if you are using the base SharePoint Online site.\n- The default document library name in SharePoint is usually \"Documents\".\n- File paths are relative to the drive root and should not include a leading slash.\n\n### Available Operations\n\n#### 1. List Files\n\nRetrieves a list of files in a specified path within a document library.\n\n```http\nGET /api/graph/files?siteName={siteName}\u0026driveName={driveName}\u0026path={path}\n```\n\n**Parameters**:\n- `siteName`: Name of the SharePoint site (use \"root\" for base site)\n- `driveName`: Name of the document library (e.g., \"Documents\")\n- `path`: Path within the library (e.g., \"Folder1/SubFolder\")\n\n#### 2. Upload a File\n\nUploads a file to a specified path within a document library.\n\n```http\nPOST /api/graph/files?siteName={siteName}\u0026driveName={driveName}\u0026path={path}\n```\n\n**Parameters**:\n- `siteName`: Name of the SharePoint site\n- `driveName`: Name of the document library\n- `path`: Target path for the file\n- Request body: Form data containing the file\n\n#### 3. Download/Read a File\n\nRetrieves file metadata or downloads the file.\n\n```http\nGET /api/graph/files/{fileName}?siteName={siteName}\u0026driveName={driveName}\u0026path={path}\n```\n\n**Parameters**:\n- `fileName`: Name of the file to download\n- `siteName`: Name of the SharePoint site\n- `driveName`: Name of the document library\n- `path`: Path to the file\n\n#### 4. Delete a File\n\nDeletes a file from a specified path.\n\n```http\nDELETE /api/graph/files/{fileName}?siteName={siteName}\u0026driveName={driveName}\u0026path={path}\n```\n\n**Parameters**:\n- `fileName`: Name of the file to delete\n- `siteName`: Name of the SharePoint site\n- `driveName`: Name of the document library\n- `path`: Path to the file\n\n#### 5. Update File Metadata\n\nUpdates metadata of an existing file.\n\n```http\nPATCH /api/graph/files/{fileName}/metadata?siteName={siteName}\u0026driveName={driveName}\u0026path={path}\n```\n\n**Parameters**:\n- `fileName`: Name of the file to update\n- `siteName`: Name of the SharePoint site\n- `driveName`: Name of the document library\n- `path`: Path to the file\n- Request body: JSON object with metadata key-value pairs\n\n## Architecture and Code Flow\n\nThe application follows a layered architecture pattern with clear separation of concerns. Below is an architectural diagram of the codebase:\n\n```mermaid\nflowchart TB\n    Client[Client Application]\n    SpoWebApi[Spo.WebApi]\n    SpoGraphApi[Spo.GraphApi Library]\n    GraphApiClient[GraphApiClient]\n    GraphApiFactory[GraphApiClientFactory]\n    AuthHandler[GraphApiAuthenticationHandler]\n    GraphAPI[Microsoft Graph API]\n    SharePoint[SharePoint Online]\n    \n    Client --\u003e|HTTP Requests| SpoWebApi\n    SpoWebApi --\u003e|Uses| SpoGraphApi\n    SpoGraphApi --\u003e|Creates| GraphApiFactory\n    GraphApiFactory --\u003e|Creates| GraphApiClient\n    GraphApiClient --\u003e|Authenticated Requests| GraphAPI\n    GraphApiClient --\u003e|Uses| AuthHandler\n    AuthHandler --\u003e|OAuth 2.0| GraphAPI\n    GraphAPI --\u003e|Interacts with| SharePoint\n    \n    subgraph \"Client Layer\"\n        Client\n    end\n    \n    subgraph \"API Layer\"\n        SpoWebApi\n    end\n    \n    subgraph \"Core Library\"\n        SpoGraphApi\n        GraphApiFactory\n        GraphApiClient\n        AuthHandler\n    end\n    \n    subgraph \"External Services\"\n        GraphAPI\n        SharePoint\n    end\n```\n\n### Component Descriptions\n\n1. **Client Application**: External applications that consume the API endpoints.\n\n2. **Spo.WebApi**: ASP.NET Core Web API that exposes endpoints for SharePoint file operations. It acts as a façade over the core library.\n\n3. **Spo.GraphApi Library**: Core library containing all the logic for interacting with Microsoft Graph API.\n\n4. **GraphApiClientFactory**: Factory class responsible for creating instances of GraphApiClient with proper authentication.\n\n5. **GraphApiClient**: Main class that handles all file operations by making authenticated requests to Microsoft Graph API.\n\n6. **GraphApiAuthenticationHandler**: Handles OAuth 2.0 authentication with Azure AD and token management.\n\n7. **Microsoft Graph API**: Microsoft's unified API endpoint that provides access to SharePoint data.\n\n8. **SharePoint Online**: The destination service where files are stored and managed.\n\n### Request Flow\n\n1. The client sends an HTTP request to one of the endpoints in Spo.WebApi.\n2. The controller in Spo.WebApi processes the request and calls the appropriate method in the Spo.GraphApi library.\n3. The GraphApiClientFactory creates an authenticated instance of GraphApiClient.\n4. GraphApiClient sends authenticated requests to Microsoft Graph API.\n5. Microsoft Graph API interacts with SharePoint Online to perform the requested operation.\n6. The response follows the same path back to the client.\n\n## Technologies Used\n- **.NET 9** - The latest version of the .NET framework\n- **Microsoft Graph API** - Unified REST API for Microsoft 365 services\n- **OAuth 2.0 Authentication** - Industry-standard protocol for authorization\n- **Azure Active Directory** - Microsoft's cloud-based identity and access management service\n- **SharePoint Online** - Cloud-based content management and collaboration platform\n- **Distributed Caching** - For improved performance when accessing repeated resources\n- **Bruno API Client** - For testing REST API endpoints\n\n## Codebase Overview\n\nThis section provides insights into the key components of the codebase to help developers understand and extend the functionality.\n\n### Project Structure\n\n- **Spo.GraphApi**: Core library containing interfaces, models, and implementations for Graph API operations\n  - `GraphApiClient.cs`: Main implementation of Graph API operations\n  - `GraphApiClientFactory.cs`: Factory for creating authenticated clients\n  - `GraphApiAuthenticationHandler.cs`: Handles OAuth 2.0 token acquisition\n  - `Models/`: Contains data models used throughout the application\n\n- **Spo.WebApi**: ASP.NET Core Web API exposing endpoints\n  - `Controllers/GraphApiController.cs`: Exposes REST endpoints\n  - `Program.cs`: Application startup and configuration\n  - `appsettings.json`: Application settings including Graph API configuration\n\n- **API Collection**: Bruno API client collections for testing\n  - Contains pre-configured requests for all API operations\n\n### Key Implementation Details\n\n1. **Authentication Flow**\n   - The application uses the Client Credentials OAuth 2.0 flow\n   - `GraphApiClientFactory` handles authentication token acquisition and renewal\n   - Tokens are cached to improve performance\n\n2. **Performance Optimizations**\n   - Site and drive information is cached using distributed caching\n   - This reduces redundant calls to Microsoft Graph API\n\n3. **Error Handling**\n   - Custom exception types for Graph API-specific errors\n   - Consistent error response format across all endpoints\n\n4. **Extension Points**\n   - The modular design allows for easy extension of functionality\n   - Implement additional Graph API features by extending the `IGraphApiClient` interface\n\n## Testing with Bruno API Client\n\nThis project includes a collection of API requests for testing with Bruno API client.\n\n1. Install [Bruno API client](https://www.usebruno.com/downloads)\n2. Open the `API Collection` folder in Bruno\n3. Configure the environment variables in `environments/Local.bru`\n4. Execute requests to test different endpoints\n\n## Troubleshooting\n\n### Common Issues\n\n1. **Authentication Errors**\n   - Verify Client ID, Tenant ID, and Client Secret in `appsettings.json`\n   - Ensure proper permissions are granted in Azure AD\n\n2. **File Operation Errors**\n   - Check if the site, drive, and path parameters are correct\n   - Verify that the SharePoint site is accessible\n\n3. **Rate Limiting**\n   - Microsoft Graph API has rate limits that may be encountered during heavy usage\n   - Implement retry logic or backoff strategies for production use\n\n## Contributing\n\nContributions are welcome! If you find a bug or have a feature request, feel free to open an issue or submit a pull request.\n\n1. Fork the repository\n2. Create your feature branch (`git checkout -b feature/amazing-feature`)\n3. Commit your changes (`git commit -m 'Add some amazing feature'`)\n4. Push to the branch (`git push origin feature/amazing-feature`)\n5. Open a Pull Request\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n\n## Contact\n\nFor any inquiries or support, please contact [nitin27may@gmail.com](mailto:nitin27may@gmail.com).\n\n---\n\nBuilt with ❤️ using .NET 9 and Microsoft Graph API.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnitin27may%2Fsharepoint-graph-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnitin27may%2Fsharepoint-graph-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnitin27may%2Fsharepoint-graph-api/lists"}