Wednesday, June 1, 2016

FileNet SharePoint Integration - II

Here is my second post related to Implementing FileNet P8 API in SharePoint. In my previous post, I explained a bit about FileNet, FileNet P8 API and connecting to FileNet from C# application.

Here is how you can read a folder from FileNet, fetch its properties, fetch subfolders.

FileNet API allows you to get Folders / Files using Ids and Path. In my case, I used the path as those are configurable and can work in different scenarios.

  1. Connect to FileNet
  2. Fetch a Folder
  3. Fetch Subfolders from FileNet & Properties

1. Connect to FileNet:
public static IObjectStore Connect()
{
  try
  {
   // Set connection parameters; substitute for the placeholders.
   string uri =  GetConfigValue("FileNetServerURL");
   //"http://filenetserverurl:9082/wsi/FNCEWS40MTOM/"; 
      string username = GetConfigValue("FileNetServerAdmin"); //"P8Admin";
   string password = GetConfigValue("FileNetServerAdminPassword"); //"Password";
   string objectstorename = GetConfigValue("FileNetObjectStore");//ECMStore
   // Get client context.
   IConnection conn = Factory.Connection.GetConnection(uri);
   // UsernameToken 
   UsernameCredentials creds = new UsernameCredentials(username, password);
   ClientContext.SetProcessCredentials(creds);
     // Get default domain.
   IDomain domain = Factory.Domain.FetchInstance(conn, null, null);
   // Get object stores for domain.
   IObjectStore os = Factory.ObjectStore.FetchInstance(domain, objectstorename, null);
   return os;
 }
 catch (Exception ex)
 {   
  return null;
 }
}
2. Get Folder By Path:
   private static PropertyFilter PFFolder;
   public static string GetFolderIDFromPath(string path, string siteUrl=null )
            {
                try
                {

                PFFolder = new PropertyFilter();
                PFFolder.AddIncludeProperty(new FilterElement(null, null, null, PropertyNames.ID  , null)); 
//property that is not fetched directly can be fetched using a property filter
  IObjectStore os = Connect(siteUrl); // returns IObjectStore from Connect Method, check connect method
                    IFolder ifolder = Factory.Folder.FetchInstance(os, path,PFFolder );
                    return ifolder.Id.ToString ();
                }
                catch (EngineRuntimeException ex)
                {
             LogError(ex, " GetFolderIDFromPath("+path+")", siteUrl);
                    return null;
                }
            }

3. Fetch Sub-folders from FileNet:
   public static List<FileNetFolder> GetAllFolders(string folderpath, bool includeContent, string siteUrl = null)
            {
                List<FileNetFolder> allFolders = new List<FileNetFolder>();
                try
                {
                    IObjectStore os = Connect(siteUrl );
                    IFolder folder = Factory.Folder.GetInstance(os, ClassNames.FOLDER, folderpath);
                    IFolderSet subFolders = folder.SubFolders;
                    for (IEnumerator iterator = subFolders.GetEnumerator(); iterator.MoveNext(); )
                    {
                        IFolder subFolder = (IFolder)iterator.Current;
                        FileNetFolder newSubFolder = new FileNetFolder();
                        newSubFolder.Title = subFolder.FolderName;
// fetch OOB Properties
                        newSubFolder.TitleEn = Convert.ToString(subFolder.Properties["TitleEn"]); 
//Fetch Custom properties of a folder
                        newSubFolder.folderId = Convert.ToString(subFolder.Id);
                        allFolders.Add(newSubFolder);
                    }
                }
                catch (Exception ex)
                {
                    LogError(ex, "GetAllFolders()", siteUrl);
                  
                }
                return allFolders;

            } 


I have used the property filter while fetching properties of a folder. A property filter adds properties that are not directly received when fetching/getting a folder. such properties can be added to a property Filter add retrieved while fetching/getting a folder/file. 

Example:

private static PropertyFilter PFFolder; 
PFFolder = new PropertyFilter();
                PFFolder.AddIncludeProperty(new FilterElement(null, null, null, PropertyNames.ID  , null));

   IFolder ifolder = Factory.Folder.FetchInstance(os, path,PFFolder );