Archive for category Salesforce Connector

Storing user profile image using Salesforce Connector with Sitecore

When using the salesforce connector with Sitecore, we came across the need to store user profile images as well in salesforce.
Once the connector is set up (which includes setup in Web.config / Domains.config and S4S.config), any user user profile interaction automatically happens with salesforce, and fields which are mapped in web.config with corresponding sitecore fields – will automatically get mapped / updated on these interactions!

The special case we came across, was with Profile images. Our site allowed the user to upload profile images, which we needed to store in salesforce, and additionally also wanted the image to show up in sitecore.

The solution we came up with, was to create a document folder in salesforce and upload all profile images there with the contact id as the file name as a document, and then store the url in the Sitecore user portrait field.

In the view, we added the form to upload images:

using (Html.BeginForm("SubmitProfilePhoto", "SharedUpdateProfilePhoto",
    FormMethod.Post, new Dictionary<string, object>
    {
        {"enctype", "multipart/form-data"},
        {"role", "form"}
    }))
{
     <input type="file" accept="image/jpeg,image/gif,image/png" name="profilePhoto" id="profilePhoto" />
}

In the post method:

        [HttpPost]
        public void SubmitProfilePhoto(HttpPostedFileBase profilePhoto)
        {
            BinaryReader binaryReader = new BinaryReader(profilePhoto.InputStream);
            byte[] bytes = binaryReader.ReadBytes((int)profilePhoto.InputStream.Length);

            string documentId = S4SHelper.AddDocument(Convert.ToBase64String(bytes), profilePhoto.FileName.Substring(profilePhoto.FileName.LastIndexOf('.')));

            if (documentId != null)
            {
                // Update Salesforce field in user object - which contains the profile picture document id
                S4SHelper.UpdateContactField(Constants.Salesforce.ContactFields.ContactProfilePhotoDocumentId, documentId);
                // Update profile image url in sitecore user profile                        
                Sitecore.Context.User.Profile.Portrait = S4SHelper.GetDocumentUrl(documentId);
            }

        }

Here’s the code used to add the document in salesforce:

public static string AddDocument(string base64FileContents, string fileExtension)
        {
            if (string.IsNullOrWhiteSpace(_Salesforce_Profile_Image_Folder_Id)) return string.Empty;

            UserInformation userInformation = GetUserInformation();
            DocumentService documentService = new DocumentService(GetSalesforceSession);
            Document document;

            // If a document already exists for this user, delete it! 
            // (We dont want to update it, because image with the same url gets cached)
            if (!string.IsNullOrWhiteSpace(userInformation.ProfilePhotoDocumentId))
            {
                document = documentService.GetByEntityId(userInformation.ProfilePhotoDocumentId);
                if (document != null)
                {
                    documentService.DeleteEntity(document);
                }
            }

            document = new Document
            {
                Name = userInformation.ContactId + fileExtension,
                Body = base64FileContents,
                FolderId = _Salesforce_Profile_Image_Folder_Id,
                IsPublic = true
            };

            SaveResult saveResult = documentService.Save(document);

            if (saveResult.success)
            {
                return saveResult.id;
            }
            return null;
        }

Once the document is added in salesforce, the document url will give you the base document url of your salesforce instance, which you can use and replace the intended document id to get the right url for each new document created. This is what the S4SHelper.GetDocumentUrl(documentId) method does.

, , , , ,

1 Comment