Issues Implementing Advanced Features with Microsoft Graph SDK 2.0 for SharePoint
@shemogumbe đang làm issue này rồi.
Từ ngày 20/2/2025.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
Hello everyone,
I am working on a project that uses version 2.0 of the Microsoft Graph SDK library to interact with SharePoint. I have successfully set up the following permissions in Azure AD:
Files.ReadWrite.All
BrowserSiteLists.ReadWrite.All
Sites.FullControl.All
And I have developed a class, GraphTest, to handle authentication and some basic operations. My current code works for obtaining a SharePoint site ID via URL (certainly not the best method), but I am encountering difficulties in implementing more advanced features, specifically:
- Use the "Get a site resource" method with hostname and relative-path (GET /sites/{hostname}:/{server-relative-path}).
- Create a new folder in SharePoint by specifying a destination URL and the folder name (POST /sites/{site-id}/drive/items/{parent-item-id}/children).
- Upload a new small file to SharePoint by providing a destination URL and the file (PUT /sites/{site-id}/drive/items/{parent-id}:/{filename}:/content).
- Upload a large file to SharePoint by creating an upload session (POST /sites/{siteId}/drive/items/{itemId}/createUploadSession).
I am a bit confused by the documentation and the examples provided, and would be very grateful if someone could provide me with some code examples or advice on how to implement these features. I am sure that other members of the community could also benefit from this discussion.
Thank you in advance for your time and help.
Best regards,
Ivan
Here is my code:
// Define the SharePoint site URL
$url = 'https://contoso.sharepoint.com/sites/examplesite';
// Define authentication settings including client ID, client secret, and tenant ID
$settings = array(
'clientId' => 'xxxxxxxxxxxxxxxxxxxx',
'clientSecret' => 'xxxxxxxxxxxxxxxxxxxx',
'tenantId' => 'xxxxxxxxxxxxxxxxxxxx'
);
// Create an instance of the GraphTest class and initialize it with the authentication settings
$graphServiceClient = new GraphTest($settings);
// Call the getSiteId method of the GraphTest instance to obtain the SharePoint site ID based on the URL
$resultID = $graphServiceClient->getSiteId($url);
class GraphTest
{
private $graphServiceClient;
private $tenantId;
private $clientId;
private $clientSecret;
/**
* Constructor to initialize class properties.
*
* @param array $settings Configuration settings including tenantId, clientId, and clientSecret.
*/
public function __construct(array $settings)
{
$this->tenantId = $settings['tenantId'];
$this->clientId = $settings['clientId'];
$this->clientSecret = $settings['clientSecret'];
$this->authenticate();
}
/**
* Authenticates and sets up the Graph Service Client.
*/
private function authenticate()
{
$tokenRequestContext = new ClientCredentialContext($this->tenantId, $this->clientId, $this->clientSecret);
$this->graphServiceClient = new GraphServiceClient($tokenRequestContext);
}
/**
* Retrieves the site ID based on a given SharePoint path.
*
* @param string $sharePointPath The SharePoint URL.
* @return string|null The site ID or null if not found.
*/
public function getSiteId($sharePointPath)
{
try {
$siteName = basename(parse_url($sharePointPath, PHP_URL_PATH));
$request = $this->graphServiceClient->sites()->get()->wait();
foreach ($request->getValue() as $site) {
if ($site->getName() === $siteName) {
return $site->getId();
}
}
return null;
} catch (ApiException $ex) {
// Log the error and rethrow or handle it as needed
error_log("API Error: " . $ex->getMessage());
throw $ex;
} catch (\Exception $e) {
// Log general exceptions and rethrow or handle them as needed
error_log("General Error: " . $e->getMessage());
throw $e;
}
}
}
- Ngôn ngữ chính
- PHP
- Star
- 669
- Fork
- 150
- Merge trung bình
- 15 giờ 21 phút
- Pull request đã merge (30 ngày)
- 3
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoftgraph/msgraph-sdk-php
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
microsoftgraph/msgraph-sdk-php#1794 · 2 bình luận ·
-
bug: metadata type:bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
microsoftgraph/msgraph-sdk-php#1739 · 6 bình luận ·
-
status:waiting-for-triage type:bug
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 68/100
microsoftgraph/msgraph-sdk-php#1643 · 8 bình luận · 4 reaction ·
-
status:waiting-for-triage
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
microsoftgraph/msgraph-sdk-php#1800 · 1 reaction ·
-
Now Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 1/100
microsoftgraph/msgraph-sdk-php#1799 ·
Tất cả issue của microsoftgraph/msgraph-sdk-php
Issue tương tự
-
sync-en
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 85/100
-
sync-en
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 85/100
-
Перевод устарел
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 78/100
-
[6.x]: "Cannot use object of type stdClass as array" loading Users index (regression of #19182) Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100