Sunday, April 17, 2011

How to access Salesforce Content in .Net application using API

In today’s competitive marketplace,no one has time to wait months or years to implement a solution to help the sales team. That’s the beauty of the Salesforce model—because it’s on-demand, Salesforce Content takes a fraction of the implementation time that traditional software requires


I have created a application which can be used to open and save into salesforce content. I have used Enterprise.wsdl in .Net application. Below is the class which is used to login into salesforce account and have the methods related to salesforce content.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SalesforceContent.Enterprise;
using System.IO;

namespace SalesforceContent
{
    public class Content
    {
        public Content() { }
        public static string username { get; set; }
        public static string password { get; set; }
        public static SforceService binding {
            get
            {
                SforceService binding = new SforceService();
                binding = new SforceService();
                
                LoginResult lr = binding.login(username, password);
                binding.SessionHeaderValue = new SessionHeader();
                binding.SessionHeaderValue.sessionId = lr.sessionId;
                binding.Url = lr.serverUrl;
                binding.Url = lr.serverUrl;
                return binding;
            } 
        }

        public static sObject [] GetAvailableWorkspaces() {
                QueryResult query
                    = binding.query("select Id, Name from" 
                    + " ContentWorkspace");

                sObject[] records = query.records ;
                
                
                return records;
            }

        public static void SaveContent(String workspaceId,String path) {

        // Create a content version object
        ContentVersion contentVersion = new ContentVersion();

        // Set the mandatory field pathOnClient
        contentVersion.PathOnClient = path;
        
        FileStream fs = new FileStream(path, FileMode.Open,FileAccess.Read);

        // Create a byte array of file stream length
        byte[] data = new byte[fs.Length];

        //Read block of bytes from stream into the byte array
        fs.Read(data,0,System.Convert.ToInt32(fs.Length));

        //Close the File Stream
        fs.Close();

        // Set the binary file data
        contentVersion.VersionData = data;

        // We can set the title to something other than the
        // filename
        contentVersion.Title = "My Content File";

        // When publishing into a public workspace, the current
        // user must have publish permissions
        contentVersion.FirstPublishLocationId = workspaceId;

        // As we're publishing into a workspace we can set
        // additional meta-data
        contentVersion.TagCsv = "Test";
        
        
        // You can add multiple versions if required by adding
        // them to the array
        sObject[] array = new sObject[] { contentVersion };
        SaveResult[] saveResultArray = binding.create(array);
        foreach (SaveResult sr in saveResultArray) {
            if (sr.success) {
                String versionId = sr.id;
                // success created version in content.
                
            } else {
                //Failed to create entity
                foreach (Error error in sr.errors) {
                    // get error messages by error.getMessage()
                }
            }
        }
    }

        public static sObject[] OpenFromContent(String workspaceId)
        {
            QueryResult query
                = binding.query("select Id, Title, "
              + "ContentSize, FileType, VersionData, "
              + "PathOnClient from ContentVersion v where "
              + "v.FirstPublishLocationId = '" + workspaceId + "'");

            sObject[] records = query.records;
            return records;
        }
    }
}

No comments:

Post a Comment