• sfdc org 1
  • 0 Points
  • Member since 2014

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 3
Hi all,I want to integrate share point with sfdc to upload file from sfdc to sharepoint and I have sharepoint account just what else we need and how to do it i have heard abt Microsoft azure so do i need account for it?plaz help me out!
Requirement: We have a requirement to download a particular version of file (contentVersion: using the versiondata field on contentversion) and upload this docuement to sharepoint. Documents can be of any type. For e.g. PDF, Doc, CSV, Png, txt etc. -

Problem Statement:  We are able to read the content version data from contentversion object. But Salesforce encodes the document into the binary file while storing it into Salesforce. While uploading the document(via REST integration) into SharePoint, we are not able to decode the binary file into the actual file stored and hence the uploaded document is not opening in SharePoint site with following error “Failed to load the document. 

Many solutions in forums suggest to use EncodingUtil.base64Decode(inputString) or convertToHex(inputBlob) but these conversions are failing with error message (Not a valid UTF 8 character or  Illegal hexadecimal character � at index )

Can someone shred some light on how to convert this before sending to sharepoint so that is is in readable format?
How to integrate sharepoint with salesforce??

I have completed code for sharepoint/salesforce integration code below,but I don't know how to implement this step by step i.e within one class or in different class.

I am new in salesforce development,please help me.

How Salesforce Performs Step 1
The main point for cloud based integration is to host a running service on Microsoft’s cloud app platform Azure, and leveraging it to interact with SharePoint. Since the service is hosted on a cloud platform, we usually access it via web-based URL. So our methods from Salesforce side to request authentication token look something like this:
public static String getToken() {
String token;
if(!Test.isRunningTest()) {
token = SharePointAPIUtility.SharePointAPIGet('http://testingalgoworks.azurewebsites.net/Api/Values/GetAuthToken','Test@test.com','TestingPassword');
system.debug('token>>> '+token);
if(token != null) {
return EncodingUtil.urlEncode(token.replaceAll('"',''), 'UTF-8');
return null;
public static String SharePointAPIGet(String endpointUrl,String username, String password) {
try {
HttpRequest httpRequestObject = new HttpRequest();

Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
httpRequestObject.setHeader('Authorization', authorizationHeader);

system.debug('httpRequestObject>> '+httpRequestObject);

Http http = new Http();
HttpResponse httpResponse ;

httpResponse = http.send(httpRequestObject);

if(httpResponse != null && httpResponse.getStatus() == 'OK' && httpResponse.getStatusCode() == 200) {
return httpResponse.getBody();
else if(httpResponse != null) {
return 'SharePoint Server Error: Status '+ httpResponse.getStatus()+' Status Code '+ httpResponse.getStatusCode() +' Body '+httpResponse.getBody();
} catch(CalloutException ce) {
throw ce;
} catch(Exception ex) {
throw ex;
return null;
This code hits the Azure service using the URL and receives the authentication token which the Azure service sends.
We will come to the steps 2,3 and 5 right after the 5th:
How Salesforce Performs Step 5
Once Salesforce has authentication token, it uses that to request files and folders from the adapter. Once again it uses the Azure service URL to hit the service.
Here’s a method to request files and a method to request folders
public static List<String> getAllFolders(SharePoint365APIParser objSharePoint365APIParser){
try {
list<String> objFolders = new list<String>();
if(objSharePoint365APIParser.folders != null && objSharePoint365APIParser.folders.size()>0) //null check
for(SharePoint365APIParser.folders sp:objSharePoint365APIParser.folders) {
return objFolders;
} catch(Exception ex) {
throw ex;
return null;
public static List<String> getFilesByFolder(String folderName, SharePoint365APIParser objSharePoint365APIParser) {
//if(!test.isRunningTest()) {
if(objSharePoint365APIParser.folders != null && objSharePoint365APIParser.folders.size()>0)
for(SharePoint365APIParser.folders sp:objSharePoint365APIParser.folders) {
if(sp.name.equalsIgnoreCase(folderName)) {
if(sp.files.size() > 0) { 
return sp.files;
} else {
return new list<String>();
} catch(Exception ex) {
throw ex;
//}//end running test loop

return null;
How Azure Performs Step 2, 3, and 4
Once Salesforce has sent the request for authentication token, here’s how Azure platform service authenticates login.
public bool Login(string email, string password) {
//throw new Exception("This is error!!");
bool validateLogin = false;
List<string> MessageList = new List<string>();
//string decryptedPassword = Encryption.Decrypt(encryptedPassword);
if (email == ConfigurationManager.AppSettings["Email"] && password == ConfigurationManager.AppSettings["Password"]) {
string authInfo = email + ":" + password;
authInfo = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(authInfo));
//authInfo = Encryption.Encrypt(authInfo);
System.Web.HttpContext.Current.Response.AppendHeader( "Authorization", "Basic " + authInfo);
// Insert User Token
MessageList.Add("Login Successful");
validateLogin = true;
else {
MessageList.Add("Invalid Username Or Password");
return validateLogin;
How Azure service handles Step 6
Now that Salesforce has authentication token and is logged in on SharePoint, here’s how our Azure service parses the request for file and folder lists.
public Folders GetResourceData() {
Folders fld = new Folders();
try {
using (ClientContext clientContext = new ClientContext("https://yourprojectname.SharePoint.com/Resources"))
SecureString passWord = new SecureString();
foreach (char c in "TestPassword".ToCharArray()) 

clientContext.Credentials = new SharePointOnlineCredentials("Test@test.com", passWord);
Web rootweb = clientContext.Web;
var folders = rootweb.GetFolderByServerRelativeUrl("/Resources").Folders;
string pString = @"\Resources\";
fld.folders = new List<Folders>();
fld.name = "Resources";
foreach (Microsoft.SharePoint.Client.Folder myFolder in folders)
fld.folders.Add(GetFoldersAndFiles(myFolder, clientContext, pString));
catch (Exception)

fld.name = "Some error happened."; }
return fld;
private Folders GetFoldersAndFiles(Microsoft.SharePoint.Client.Folder mainFolder, ClientContext clientContext, string pathString) {
Folders fldr = new Folders();
List<string> fls = new List<string>();
fldr.folders = new List<Folders>();
clientContext.Load(mainFolder, k => k.Files, k => k.Folders);
foreach (var folder in mainFolder.Folders)
string folderPath = string.Format(@"{0}{1}\", pathString, folder.Name);
if (folder.Name != "Forms")
fldr.folders.Add(GetFoldersAndFiles(folder, clientContext, folderPath));
foreach (var file in mainFolder.Files)
fldr.files = fls;
if (mainFolder.Name != "Forms")
fldr.name = mainFolder.Name;
return fldr;