I have a client who has setup a certificate allowing me access to his Sharepoint site. He has provided me with the PDX file, ClientID, and TenantID. I believe that I’m able to authenticate with the code provided below, but I have not been successful with using the GraphServiceClient to upload a file to the site.
He has done some testing on his end with a Powershell script and he is able to upload a file through Powershell. I am not fluent in Powershell and would prefer to write my solution in C#.
I have searched high and low for examples of code to do this work. None of the examples are working. I don’t need an asynchronous solution. This process will run periodically (likely daily) to read files from one system and copy them to another. Is this possible in C# ?
Here is my code. It won't compile due to problem last line - var resultDriveItem = await...
Thanks,
public static async void TestClientCert()
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientId = "blah";
var tenantId = "blah";
var clientCertificate = new X509Certificate2(@"c:\work\Test.pfx", "blah");
var options = new ClientCertificateCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
var clientCertCredential = new ClientCertificateCredential(
tenantId, clientId, clientCertificate, options);
var gc = new GraphServiceClient(clientCertCredential, scopes);
var fileName = @"c:\work\fileToUpload\upload.doc";
using (Stream fileStream = new FileStream(
fileName,
FileMode.Open,
FileAccess.Read))
{
var resultDriveItem = await gc.Sites["SiteID-Blah"]
.Drives[0].Root.ItemWithPath(fileName).Content.Request().PutAsync<DriveItem>(fileStream);
}
}
All I am saying is:
And your file is uploaded using the PnP PowerShell module (microsoft certified).
If you really wanna go with C# I would consider trying the PnP Core SDK: