Using Microsoft Graph API to convert the format of your documents

Published May 01 2019 04:17 PM 6,304 Views
Microsoft

First published on TECHNET on Oct 26, 2018
This post is a contribution from Jing Wang, an engineer with the SharePoint Developer Support team

Many SharePoint Online customers want to convert their word documents or some other documents in SPO to pdf files programmatically.
Within SharePoint Online User Interface, you can convert them one at a time manually, but sometimes, users wish to have the ability to convert multiple documents automatically without having to open each document library, locate the documents and click through the options to do them one at a time.

With new Graph api endpoint listed below, above requirement can be automated with custom code, for example, C#, JavaScript…


GET /drive/items/{item-id}/content?format={format}
GET /drive/root:/ {path and filename} :/content?format={format}


Format options
The following values are valid for the format parameter:
Format valueDescriptionSupported source extensions

pdf Converts the item into PDF format. csv, doc, docx, odp, ods, odt, pot, potm, potx, pps, ppsx, ppsxm, ppt, pptm, pptx, rtf, xls, xlsx


See details of the endpoint here:
https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/driveitem_get_content_forma...

Sample - Complete solution in C#:

Step I, Create a native app in Azure portal and give permissions, for Graph API.







Step II. Create a Console Application, add two dlls and their references:
DLLs:. These can be added as Nuget packages.
Microsoft.IdentityModel.Clients.ActiveDirectory.dll (Nuget Package Microsoft.SharePointOnline.CSOM)
Newtonsoft.Json.dll (Nuget Package Newtonsoft.Json)

Add the below using statements


using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json;


Step III, Implement the code, with Graph Api to convert the document and download it locally and upload it back to SPO site:
Note: ADAL library is used for authentication.

Source code:
-------------------------------


using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Security.Claims;
using System.IO;
using Microsoft.SharePoint.Client;
using System.Security;


namespace ConsoleApp1
{
public static class StreamExtensions
{
public static byte[] ReadAllBytes(this Stream instream)
{
if (instream is MemoryStream)
return ((MemoryStream)instream).ToArray();

using (var memoryStream = new MemoryStream())
{
instream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
}
}
class Program
{
private static string TENANT_NAME = "mycompany.onmicrosoft.com";
private static string resource = "https://graph.microsoft.com";
private static string loginname = "user@mycompany.onmicrosoft.com";
private static string loginpassword = "*********";
private static string AzureTenantID = "********-f247-4d48-a45d-************";
private static string spositeUrl = "https://mycompany.sharepoint.com/*********";
private static string destinationDocumentLibrary = "dl1";

static void Main(string[] args)
{
//USER TOKEN - THIS WORKS!!!!!!!!!!!!
UserPasswordCredential userPasswordCredential = new UserPasswordCredential(loginname, loginpassword);
var graphauthority = "https://login.microsoftonline.com/" + AzureTenantID;
AuthenticationContext authContext = new AuthenticationContext(graphauthority);
var token = authContext.AcquireTokenAsync(resource, "94b1544c-35e8-4d45-a941-c3dbaab283dc", userPasswordCredential).Result.AccessToken;

// Create a new HttpWebRequest Object to the mentioned URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("https://graph.microsoft.com/v1.0/me/drive/root:/orange.docx:/content?format=pdf");
//HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("https://graph.microsoft.com/v1.0/drives/b!zMNDej1sNEG0SanRDltXfAVTYcdt1pdIggMBPYZYp9Wgdi3ir9sFQJXof6...");
myHttpWebRequest.AllowAutoRedirect = false;
myHttpWebRequest.Headers.Set("Authorization", ("Bearer " + token));
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
string downloadPath = myHttpWebResponse.GetResponseHeader("Location");
Console.WriteLine("Download PDF file from here:\n " + downloadPath);

//Get the file Stream with Location Url
HttpWebRequest HttpWebRequest_download = (HttpWebRequest)WebRequest.Create(downloadPath);
HttpWebRequest_download.Accept = "blob";

var response = (HttpWebResponse)HttpWebRequest_download.GetResponse();
Stream myStream = response.GetResponseStream();
FileStream targetFile = new FileStream("C:\\temp\\orange_converted_localcopy.pdf", FileMode.Create);
myStream.CopyTo(targetFile);
myStream.Close();
response.Close();

//You can continue to use Graph API to upload document back to OneDrive or other SPO site
//since we used loginname/password above already, we will use simple CSOM to upload file to another SPO site as quick demo
using (var clientContext = new ClientContext(spositeUrl))
{
SecureString passWord = new SecureString();
foreach (char c in loginpassword.ToCharArray()) passWord.AppendChar(c);

clientContext.Credentials = new SharePointOnlineCredentials(loginname, passWord);
var web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();

List dl = web.Lists.GetByTitle(destinationDocumentLibrary);
clientContext.Load(dl);
clientContext.ExecuteQuery();

//Upload the converted file to SPO site
targetFile.Position = 0;
var fci = new FileCreationInformation
{
Url = "orange_converted_spocopy.pdf",
ContentStream = targetFile,
Overwrite = true
};
Folder folder = dl.RootFolder;
FileCollection files = folder.Files;
Microsoft.SharePoint.Client.File file = files.Add(fci);
clientContext.Load(files);
clientContext.Load(file);
clientContext.ExecuteQuery();

targetFile.Close();
response.Close();

Console.WriteLine("Converted file is uploaded to SPO site - orange_converted_spocopy.pdf");
Console.ReadKey();
}
}

}
}


Here is the converted file downloaded locally.



The converted pdf is also uploaded to this SPO site:



In the process to generate the url for the Graph API endpoint, I found it kind of tricky to identify the drive ID for specific SPO site, so listing the approach to get the same.

First, Use following url format in Graph Explorer to retrieve the drive id:
https://graph.microsoft.com/v1.0/sites/[spositehostname]:/[sites/pub]:/drive

For example, if the SPO site url is:
https://mycompany.sharepoint.com/sites/testsite

Url to put in Graph Explorer is:
https://graph.microsoft.com/v1.0/sites/mycompany.sharepoint.com:/sites/testsite:/drive



Output has the drive id:
--


{
"@odata.context": "https://graph.microsoft.com/beta/$metadata#drives/$entity",
"createdDateTime": "2017-12-04T19:48:25Z",
"description": "This system library was created by the Publishing feature to store documents that are used on pages in this site.",
"id": "b!zMNDej1sNEG0SanRDltXfAVTYcdt1pdIggMBPYZYp9Wgdi3ir9sFQJXof6j8GNUD",
"lastModifiedDateTime": "2018-10-11T04:08:37Z",
"name": "Documents",
"webUrl": "https://mycompany.sharepoint.com/sites/****/Documents",
"driveType": "documentLibrary",
"createdBy": {
"user": {
"displayName": "System Account"
}
}
}


I have a file named “Repro.docx” in the root of above drive:



So the file’s conversion endpoint is:
https://graph.microsoft.com/v1.0/drives/b!zMNDej1sNEG0SanRDltXfAVTYcdt1pdIggMBPYZYp9Wgdi3ir9sFQJXof6...

1 Comment
Occasional Visitor

Hello, is this feature down? It seems to not be working any more. I am getting an internal. error from office services statying that HttpCodeNotfound for https://excelcs.officeapps.live.com/document/export/pdf

%3CLINGO-SUB%20id%3D%22lingo-sub-510110%22%20slang%3D%22en-US%22%3EUsing%20Microsoft%20Graph%20API%20to%20convert%20the%20format%20of%20your%20documents%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-510110%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EFirst%20published%20on%20TECHNET%20on%20Oct%2026%2C%202018%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3EThis%20post%20is%20a%20contribution%20from%20Jing%20Wang%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team%20%3CBR%20%2F%3E%3CBR%20%2F%3EMany%20SharePoint%20Online%20customers%20want%20to%20convert%20their%20word%20documents%20or%20some%20other%20documents%20in%20SPO%20to%20pdf%20files%20programmatically.%20%3CBR%20%2F%3EWithin%20SharePoint%20Online%20User%20Interface%2C%20you%20can%20convert%20them%20one%20at%20a%20time%20manually%2C%20but%20sometimes%2C%20users%20wish%20to%20have%20the%20ability%20to%20convert%20multiple%20documents%20automatically%20without%20having%20to%20open%20each%20document%20library%2C%20locate%20the%20documents%20and%20click%20through%20the%20options%20to%20do%20them%20one%20at%20a%20time.%20%3CBR%20%2F%3E%3CBR%20%2F%3EWith%20new%20Graph%20api%20endpoint%20listed%20below%2C%20above%20requirement%20can%20be%20automated%20with%20custom%20code%2C%20for%20example%2C%20C%23%2C%20JavaScript%E2%80%A6%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3EGET%20%2Fdrive%2Fitems%2F%7Bitem-id%7D%2Fcontent%3Fformat%3D%7Bformat%7D%20%3CBR%20%2F%3EGET%20%2Fdrive%2Froot%3A%2F%20%3CSTRONG%3E%20%3CEM%3E%20%7Bpath%20and%20filename%7D%20%3C%2FEM%3E%20%3C%2FSTRONG%3E%20%3A%2Fcontent%3Fformat%3D%7Bformat%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3EFormat%20options%20%3CBR%20%2F%3EThe%20following%20values%20are%20valid%20for%20the%20format%20parameter%3A%20%3CBR%20%2F%3EFormat%20valueDescriptionSupported%20source%20extensions%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3Epdf%3C%2FTD%3E%0A%3CTD%3EConverts%20the%20item%20into%20PDF%20format.%3C%2FTD%3E%0A%3CTD%3Ecsv%2C%20doc%2C%20docx%2C%20odp%2C%20ods%2C%20odt%2C%20pot%2C%20potm%2C%20potx%2C%20pps%2C%20ppsx%2C%20ppsxm%2C%20ppt%2C%20pptm%2C%20pptx%2C%20rtf%2C%20xls%2C%20xlsx%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%3CBR%20%2F%3ESee%20details%20of%20the%20endpoint%20here%3A%20%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fdeveloper.microsoft.com%2Fen-us%2Fgraph%2Fdocs%2Fapi-reference%2Fv1.0%2Fapi%2Fdriveitem_get_content_format%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20https%3A%2F%2Fdeveloper.microsoft.com%2Fen-us%2Fgraph%2Fdocs%2Fapi-reference%2Fv1.0%2Fapi%2Fdriveitem_get_content_format%20%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3ESample%20-%20Complete%20solution%20in%20C%23%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CEM%3E%20Step%20I%2C%20Create%20a%20native%20app%20in%20Azure%20portal%20and%20give%20permissions%2C%20for%20Graph%20API.%20%3C%2FEM%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111663i9C74210CC5DBCC89%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111665iDF0576ADF70CB460%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CEM%3E%20Step%20II.%20Create%20a%20Console%20Application%2C%20add%20two%20dlls%20and%20their%20references%3A%20%3C%2FEM%3E%20%3CBR%20%2F%3EDLLs%3A.%20These%20can%20be%20added%20as%20Nuget%20packages.%20%3CBR%20%2F%3EMicrosoft.IdentityModel.Clients.ActiveDirectory.dll%20(Nuget%20Package%20Microsoft.SharePointOnline.CSOM)%20%3CBR%20%2F%3ENewtonsoft.Json.dll%20(Nuget%20Package%20Newtonsoft.Json)%20%3CBR%20%2F%3E%3CBR%20%2F%3EAdd%20the%20below%20using%20statements%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3Eusing%20Microsoft.IdentityModel.Clients.ActiveDirectory%3B%20%3CBR%20%2F%3Eusing%20Newtonsoft.Json%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3EStep%20III%2C%20Implement%20the%20code%2C%20with%20Graph%20Api%20to%20convert%20the%20document%20and%20download%20it%20locally%20and%20upload%20it%20back%20to%20SPO%20site%3A%20%3CBR%20%2F%3ENote%3A%20ADAL%20library%20is%20used%20for%20authentication.%20%3CBR%20%2F%3E%3CBR%20%2F%3ESource%20code%3A%20%3CBR%20%2F%3E-------------------------------%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3Eusing%20Microsoft.IdentityModel.Clients.ActiveDirectory%3B%20%3CBR%20%2F%3Eusing%20Newtonsoft.Json%3B%20%3CBR%20%2F%3Eusing%20System%3B%20%3CBR%20%2F%3Eusing%20System.Collections.Generic%3B%20%3CBR%20%2F%3Eusing%20System.Linq%3B%20%3CBR%20%2F%3Eusing%20System.Net.Http%3B%20%3CBR%20%2F%3Eusing%20System.Net.Http.Headers%3B%20%3CBR%20%2F%3Eusing%20System.Security.Cryptography.X509Certificates%3B%20%3CBR%20%2F%3Eusing%20System.Text%3B%20%3CBR%20%2F%3Eusing%20System.Threading.Tasks%3B%20%3CBR%20%2F%3Eusing%20System.Net%3B%20%3CBR%20%2F%3Eusing%20System.Security.Claims%3B%20%3CBR%20%2F%3Eusing%20System.IO%3B%20%3CBR%20%2F%3Eusing%20Microsoft.SharePoint.Client%3B%20%3CBR%20%2F%3Eusing%20System.Security%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3Enamespace%20ConsoleApp1%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Epublic%20static%20class%20StreamExtensions%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Epublic%20static%20byte%5B%5D%20ReadAllBytes(this%20Stream%20instream)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Eif%20(instream%20is%20MemoryStream)%20%3CBR%20%2F%3Ereturn%20((MemoryStream)instream).ToArray()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Eusing%20(var%20memoryStream%20%3D%20new%20MemoryStream())%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Einstream.CopyTo(memoryStream)%3B%20%3CBR%20%2F%3Ereturn%20memoryStream.ToArray()%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3Eclass%20Program%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3Eprivate%20static%20string%20TENANT_NAME%20%3D%20%22mycompany.onmicrosoft.com%22%3B%20%3CBR%20%2F%3Eprivate%20static%20string%20resource%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%3C%2FA%3E%22%3B%20%3CBR%20%2F%3Eprivate%20static%20string%20loginname%20%3D%20%22user%40mycompany.onmicrosoft.com%22%3B%20%3CBR%20%2F%3Eprivate%20static%20string%20loginpassword%20%3D%20%22*********%22%3B%20%3CBR%20%2F%3Eprivate%20static%20string%20AzureTenantID%20%3D%20%22********-f247-4d48-a45d-************%22%3B%20%3CBR%20%2F%3Eprivate%20static%20string%20spositeUrl%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fmycompany.sharepoint.com%2F*********%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fmycompany.sharepoint.com%2F*********%3C%2FA%3E%22%3B%20%3CBR%20%2F%3Eprivate%20static%20string%20destinationDocumentLibrary%20%3D%20%22dl1%22%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Estatic%20void%20Main(string%5B%5D%20args)%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%2F%2FUSER%20TOKEN%20-%20THIS%20WORKS!!!!!!!!!!!!%20%3CBR%20%2F%3EUserPasswordCredential%20userPasswordCredential%20%3D%20new%20UserPasswordCredential(loginname%2C%20loginpassword)%3B%20%3CBR%20%2F%3Evar%20graphauthority%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F%3C%2FA%3E%22%20%2B%20AzureTenantID%3B%20%3CBR%20%2F%3EAuthenticationContext%20authContext%20%3D%20new%20AuthenticationContext(graphauthority)%3B%20%3CBR%20%2F%3Evar%20token%20%3D%20authContext.AcquireTokenAsync(resource%2C%20%2294b1544c-35e8-4d45-a941-c3dbaab283dc%22%2C%20userPasswordCredential).Result.AccessToken%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2F%20Create%20a%20new%20HttpWebRequest%20Object%20to%20the%20mentioned%20URL.%20%3CBR%20%2F%3EHttpWebRequest%20myHttpWebRequest%20%3D%20(HttpWebRequest)WebRequest.Create(%22%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2Fdrive%2Froot%3A%2Forange.docx%3A%2Fcontent%3Fformat%3Dpdf%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fme%2Fdrive%2Froot%3A%2Forange.docx%3A%2Fcontent%3Fformat%3Dpdf%3C%2FA%3E%22)%3B%20%3CBR%20%2F%3E%2F%2FHttpWebRequest%20myHttpWebRequest%20%3D%20(HttpWebRequest)WebRequest.Create(%22%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fdrives%2Fb!zMNDej1sNEG0SanRDltXfAVTYcdt1pdIggMBPYZYp9Wgdi3ir9sFQJXof6j8GNUD%2Froot%3A%2FRepro.docx%3A%2Fcontent%3Fformat%3Dpdf%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fdrives%2Fb!zMNDej1sNEG0SanRDltXfAVTYcdt1pdIggMBPYZYp9Wgdi3ir9sFQJXof6j8GNUD%2Froot%3A%2FRepro.docx%3A%2Fcontent%3Fformat%3Dpdf%3C%2FA%3E%22)%3B%20%3CBR%20%2F%3EmyHttpWebRequest.AllowAutoRedirect%20%3D%20false%3B%20%3CBR%20%2F%3EmyHttpWebRequest.Headers.Set(%22Authorization%22%2C%20(%22Bearer%20%22%20%2B%20token))%3B%20%3CBR%20%2F%3EHttpWebResponse%20myHttpWebResponse%20%3D%20(HttpWebResponse)myHttpWebRequest.GetResponse()%3B%20%3CBR%20%2F%3Estring%20downloadPath%20%3D%20myHttpWebResponse.GetResponseHeader(%22Location%22)%3B%20%3CBR%20%2F%3EConsole.WriteLine(%22Download%20PDF%20file%20from%20here%3A%5Cn%20%22%20%2B%20downloadPath)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2FGet%20the%20file%20Stream%20with%20Location%20Url%20%3CBR%20%2F%3EHttpWebRequest%20HttpWebRequest_download%20%3D%20(HttpWebRequest)WebRequest.Create(downloadPath)%3B%20%3CBR%20%2F%3EHttpWebRequest_download.Accept%20%3D%20%22blob%22%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3Evar%20response%20%3D%20(HttpWebResponse)HttpWebRequest_download.GetResponse()%3B%20%3CBR%20%2F%3EStream%20myStream%20%3D%20response.GetResponseStream()%3B%20%3CBR%20%2F%3EFileStream%20targetFile%20%3D%20new%20FileStream(%22C%3A%5C%5Ctemp%5C%5Corange_converted_localcopy.pdf%22%2C%20FileMode.Create)%3B%20%3CBR%20%2F%3EmyStream.CopyTo(targetFile)%3B%20%3CBR%20%2F%3EmyStream.Close()%3B%20%3CBR%20%2F%3Eresponse.Close()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2FYou%20can%20continue%20to%20use%20Graph%20API%20to%20upload%20document%20back%20to%20OneDrive%20or%20other%20SPO%20site%20%3CBR%20%2F%3E%2F%2Fsince%20we%20used%20loginname%2Fpassword%20above%20already%2C%20we%20will%20use%20simple%20CSOM%20to%20upload%20file%20to%20another%20SPO%20site%20as%20quick%20demo%20%3CBR%20%2F%3Eusing%20(var%20clientContext%20%3D%20new%20ClientContext(spositeUrl))%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3ESecureString%20passWord%20%3D%20new%20SecureString()%3B%20%3CBR%20%2F%3Eforeach%20(char%20c%20in%20loginpassword.ToCharArray())%20passWord.AppendChar(c)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3EclientContext.Credentials%20%3D%20new%20SharePointOnlineCredentials(loginname%2C%20passWord)%3B%20%3CBR%20%2F%3Evar%20web%20%3D%20clientContext.Web%3B%20%3CBR%20%2F%3EclientContext.Load(web)%3B%20%3CBR%20%2F%3EclientContext.ExecuteQuery()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3EList%20dl%20%3D%20web.Lists.GetByTitle(destinationDocumentLibrary)%3B%20%3CBR%20%2F%3EclientContext.Load(dl)%3B%20%3CBR%20%2F%3EclientContext.ExecuteQuery()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%2F%2FUpload%20the%20converted%20file%20to%20SPO%20site%20%3CBR%20%2F%3EtargetFile.Position%20%3D%200%3B%20%3CBR%20%2F%3Evar%20fci%20%3D%20new%20FileCreationInformation%20%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3EUrl%20%3D%20%22orange_converted_spocopy.pdf%22%2C%20%3CBR%20%2F%3EContentStream%20%3D%20targetFile%2C%20%3CBR%20%2F%3EOverwrite%20%3D%20true%20%3CBR%20%2F%3E%7D%3B%20%3CBR%20%2F%3EFolder%20folder%20%3D%20dl.RootFolder%3B%20%3CBR%20%2F%3EFileCollection%20files%20%3D%20folder.Files%3B%20%3CBR%20%2F%3EMicrosoft.SharePoint.Client.File%20file%20%3D%20files.Add(fci)%3B%20%3CBR%20%2F%3EclientContext.Load(files)%3B%20%3CBR%20%2F%3EclientContext.Load(file)%3B%20%3CBR%20%2F%3EclientContext.ExecuteQuery()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3EtargetFile.Close()%3B%20%3CBR%20%2F%3Eresponse.Close()%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3EConsole.WriteLine(%22Converted%20file%20is%20uploaded%20to%20SPO%20site%20-%20orange_converted_spocopy.pdf%22)%3B%20%3CBR%20%2F%3EConsole.ReadKey()%3B%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3EHere%20is%20the%20converted%20file%20downloaded%20locally.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111666iD92C1DDED56A0433%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20converted%20pdf%20is%20also%20uploaded%20to%20this%20SPO%20site%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111667iCF04CE0564599B73%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3EIn%20the%20process%20to%20generate%20the%20url%20for%20the%20Graph%20API%20endpoint%2C%20I%20found%20it%20kind%20of%20tricky%20to%20identify%20the%20drive%20ID%20for%20specific%20SPO%20site%2C%20so%20listing%20the%20approach%20to%20get%20the%20same.%20%3CBR%20%2F%3E%3CBR%20%2F%3EFirst%2C%20Use%20following%20url%20format%20in%20Graph%20Explorer%20to%20retrieve%20the%20drive%20id%3A%20%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fsites%2F%5Bspositehostname%5D%3A%2F%5Bsites%2Fpub%5D%3A%2Fdrive%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fsites%2F%5Bspositehostname%5D%3A%2F%5Bsites%2Fpub%5D%3A%2Fdrive%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20example%2C%20if%20the%20SPO%20site%20url%20is%3A%20%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fmycompany.sharepoint.com%2Fsites%2Ftestsite%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fmycompany.sharepoint.com%2Fsites%2Ftestsite%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3EUrl%20to%20put%20in%20Graph%20Explorer%20is%3A%20%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fsites%2Fmycompany.sharepoint.com%3A%2Fsites%2Ftestsite%3A%2Fdrive%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fsites%2Fmycompany.sharepoint.com%3A%2Fsites%2Ftestsite%3A%2Fdrive%3C%2FA%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111668iCE0027FD9BE023E8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3EOutput%20has%20the%20drive%20id%3A%20%3CBR%20%2F%3E--%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3E%7B%20%3CBR%20%2F%3E%22%40odata.context%22%3A%20%22%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fbeta%2F%24metadata%23drives%2F%24entity%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fbeta%2F%24metadata%23drives%2F%24entity%3C%2FA%3E%22%2C%20%3CBR%20%2F%3E%22createdDateTime%22%3A%20%222017-12-04T19%3A48%3A25Z%22%2C%20%3CBR%20%2F%3E%22description%22%3A%20%22This%20system%20library%20was%20created%20by%20the%20Publishing%20feature%20to%20store%20documents%20that%20are%20used%20on%20pages%20in%20this%20site.%22%2C%20%3CBR%20%2F%3E%22id%22%3A%20%22b!zMNDej1sNEG0SanRDltXfAVTYcdt1pdIggMBPYZYp9Wgdi3ir9sFQJXof6j8GNUD%22%2C%20%3CBR%20%2F%3E%22lastModifiedDateTime%22%3A%20%222018-10-11T04%3A08%3A37Z%22%2C%20%3CBR%20%2F%3E%22name%22%3A%20%22Documents%22%2C%20%3CBR%20%2F%3E%22webUrl%22%3A%20%22%3CA%20href%3D%22https%3A%2F%2Fmycompany.sharepoint.com%2Fsites%2F****%2FDocuments%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fmycompany.sharepoint.com%2Fsites%2F****%2FDocuments%3C%2FA%3E%22%2C%20%3CBR%20%2F%3E%22driveType%22%3A%20%22documentLibrary%22%2C%20%3CBR%20%2F%3E%22createdBy%22%3A%20%7B%20%3CBR%20%2F%3E%22user%22%3A%20%7B%20%3CBR%20%2F%3E%22displayName%22%3A%20%22System%20Account%22%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%7D%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3EI%20have%20a%20file%20named%20%E2%80%9CRepro.docx%E2%80%9D%20in%20the%20root%20of%20above%20drive%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20998px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111669i47B61835D26F7318%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3ESo%20the%20file%E2%80%99s%20conversion%20endpoint%20is%3A%20%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fdrives%2Fb!zMNDej1sNEG0SanRDltXfAVTYcdt1pdIggMBPYZYp9Wgdi3ir9sFQJXof6j8GNUD%2Froot%3A%2FRepro.docx%3A%2Fcontent%3Fformat%3Dpdf%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fdrives%2Fb!zMNDej1sNEG0SanRDltXfAVTYcdt1pdIggMBPYZYp9Wgdi3ir9sFQJXof6j8GNUD%2Froot%3A%2FRepro.docx%3A%2Fcontent%3Fformat%3Dpdf%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-510110%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20TECHNET%20on%20Oct%2026%2C%202018%20This%20post%20is%20a%20contribution%20from%20Jing%20Wang%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20teamMany%20SharePoint%20Online%20customers%20want%20to%20convert%20their%20word%20documents%20or%20some%20other%20documents%20in%20SPO%20to%20pdf%20files%20programmatically.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-510110%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Sep 01 2020 02:27 PM
Updated by: