Skip to main content
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();

httpRequestObject.setEndPoint(endpointUrl);

httpRequestObject.setmethod('GET');

Blob headerValue = Blob.valueOf(username + ':' + password);

String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);

httpRequestObject.setHeader('Authorization', authorizationHeader);

httpRequestObject.setTimeout(120000);

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

Http http = new Http();

HttpResponse httpResponse ;

if(!test.isRunningTest())

httpResponse = http.send(httpRequestObject);

if(httpResponse != null && httpResponse.getStatus() == 'OK' && httpResponse.getStatusCode() == 200) {

system.debug('httpResponse.getBody();>>>>'+httpResponse.getBody()+'httpResponse.getBody();>>>>');

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) {

objFolders.add(sp.name);

}

return objFolders;

} catch(Exception ex) {

throw ex;

}

return null;

}

public static List<String> getFilesByFolder(String folderName, SharePoint365APIParser objSharePoint365APIParser) {

//if(!test.isRunningTest()) {

try{

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.

[HttpPost]

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.

[AuthorizeWebAPI()]

[HttpGet]

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()) 

passWord.AppendChar(c);

clientContext.Credentials = new SharePointOnlineCredentials("Test@test.com", passWord);

Web rootweb = clientContext.Web;

var folders = rootweb.GetFolderByServerRelativeUrl("/Resources").Folders;

string pString = @"\Resources\";

clientContext.Load(folders);

clientContext.ExecuteQuery();

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);

clientContext.ExecuteQuery();

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)

{

fls.Add(file.Name);

}

fldr.files = fls;

if (mainFolder.Name != "Forms")

fldr.name = mainFolder.Name;

return fldr;

}
7 respostas
  1. 26 de jun. de 2019, 09:53
    Hi Kapil,

    SharePointAPIUtility is a class where you have to create a method with name of 'SharePointAPIGet' and pass your all required parameter in the same method.

    Thanks.
0/9000