Dela via


Code Snippet: Implementing a Custom Connector

Applies to: SharePoint Server 2010

In this article
Description
To use this sample
Code

Description

The following code example shows how to implement the ISystemUtility interface provided by the Business Data Connectivity (BDC) service to implement a custom connector. This example provides a connector to the file system. Note that only the ISystemUtility interface is mandatory. This example overrides the default connection manager by implementing the IConnectionManager interface. This example also implements the IAdministrableSystem interface to provide Admin UI property management support.

To use this sample

  1.  Start a new Microsoft Visual Studio class project, and then name it FileSystemConnector.

  2. Replace the auto-generated code in the .cs file with the C# code provided below.

  3. Compile the assembly, and then add the assembly to the GAC.

  4. Get the public key token for the assembly.

  5. Copy the BDC Model sample from Sample BDC Model: Connecting to a Custom File System Connector to an XML file. Name it FileSystemConnectorSample.xml.

  6. Update line #9 of the XML file with the new assembly information.

  7. Update line #22 of the XML file to match the directory where you have files.

  8. Import the XML file into the BDC.

  9. At this point, you should be able to create external lists and see the data from the file system.

Code

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.BusinessData.Infrastructure;
using Microsoft.BusinessData.Runtime;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
using System.Collections;



namespace MS.Internal.Motif.Office.Lobi.ConnectorTest
{
    class FileSystemConnector : ISystemUtility, IDisposable, IConnectionManager, IAdministrableSystem
      {
        #region private variables
        String DirectoryPath;
        String DirectoryPropertyName = "Directory";
        String FileIDName = "FileName";

        #endregion
        #region IAdministrableSystem Members

        public IList<AdministrableProperty> AdministrableLobSystemProperties
        {
            get
            {
                return new List<AdministrableProperty>()
                {
                    new AdministrableProperty("SystemUtilityTypeName","Name of the custom connector  utility",typeof(string),"SystemUtilityTypeName", typeof(string),true), 
                    new AdministrableProperty("SystemUtilityInstallDate","Date custom connector was installed",typeof(DateTime),"SystemUtilityInstallDate", typeof(DateTime),false)
                };
            }
        }

        public IList<AdministrableProperty> AdministrableLobSystemInstanceProperties
        {
            get
            {
                return new List<AdministrableProperty>()
                {
                   new AdministrableProperty("ServerName","Name of server where filesystem exists",typeof(string),"ServerName", typeof(string),false)  
                };
            }
        }
        #endregion

        #region ISystemUtility Members

        public void ExecuteStatic(IMethodInstance methodInstance, ILobSystemInstance lobSystemInstance, object[] methodSignatureArgs, IExecutionContext context)
        {
            if (methodInstance == null)
            {
                throw (new ArgumentNullException("methodInstance"));
            }
            if (lobSystemInstance == null)
            {
                throw (new ArgumentNullException("lobSystemInstance"));
            }
            if (methodSignatureArgs == null)
            {
                throw (new ArgumentNullException("args"));
            }

            Dictionary<String, object> propertyValues = new Dictionary<string, object>();

            if (context != null)
            {
                if (context.IsPropertyDefined("FullPath"))
                    propertyValues.Add("FullPath", context["FullPath"]);

                if (context.IsPropertyDefined("Count"))
                {
                    context["Count"] = (Int32)context["Count"] + 1;
                    propertyValues.Add("Count", context["Count"]);
                }

                if(context.IsPropertyDefined("CountInMethod"))
                    propertyValues.Add("CountInMethod", context["CountInMethod"]);
            }
                      
            DirectoryPath = GetFullPath(methodInstance);
            ExecuteInternal(DirectoryPath, methodInstance, methodSignatureArgs,propertyValues);

        }

        public virtual IEnumerator
            CreateEntityInstanceDataEnumerator(Object rawAdapterEntityInstanceStream, ISharedEntityState sharedEntityState)
        {
           
                return ((IEnumerable)rawAdapterEntityInstanceStream).GetEnumerator();
           
            
        }
        #endregion
        #region ITypeReflector
        public ITypeReflector DefaultTypeReflector
        {
            get
            {
                return null;
            }
        }

#endregion

        #region DefaultConnectionManager
        public IConnectionManager DefaultConnectionManager
        {
            get
            {
                return null;
            }
        }
        #endregion

        #region IConnectionManager Members

        public void CloseConnection(object connection)
        {

        }

        public void FlushConnections()
        {

        }

        public object GetConnection()
        {
            return null;
        }

        public void Initialize(ILobSystemInstance properties)
        {

        }

        #endregion
        #region IDisposable Members

        public void Dispose()
        {
       
            
        }
        #endregion

        #region private methods
        private String GetFullPath(IMethodInstance methodInstance)
        {
            IDataClass dataClass = methodInstance.GetMethod().GetDataClass();
            return (string)dataClass.GetProperties()[DirectoryPropertyName];
        }

        private void ExecuteInternal(
            string directory, /* SP List Name */
           IMethodInstance methodInstance,
           object[] args,
           Dictionary<String,object> propertyBag)
        {
            String methodName = methodInstance.GetMethod().Name;
            String path;
            DynamicType dynamicParameter = new DynamicType();

            switch (methodName)
            {
                case "ReadFile":
                    String[] fields = GetFieldValues(methodInstance);
                    path = directory + @"\" + args[0];
                    if (!File.Exists(path))
                        throw new Exception(" The file does not exist for the FindSpecific");
                    else
                    {
                        foreach (String field in fields)
                        {
                            switch (field)
                            {
                                case "FileName":
                                    if (propertyBag.ContainsKey("FullPath"))
                                    {
                                        if ((Boolean)propertyBag["FullPath"])
                                            dynamicParameter.Add("FileName", path);
                                        else
                                            dynamicParameter.Add("FileName", args[0]);
                                    }
                                    else
                                        dynamicParameter.Add("FileName", args[0]);
                                    break;

                                case "Date Modified":
                                    dynamicParameter.Add("Date Modified", File.GetLastWriteTime(path));
                                    break;

                                case "Attributes":
                                    FileAttributes attributes = File.GetAttributes(path);                    
                                    
                                    dynamicParameter.Add("Attributes", attributes);
                                    break;

                                case "Date Created":
                                    dynamicParameter.Add("Date Created", File.GetCreationTime(path));
                                    break;

                                case "Date Accessed":
                                    dynamicParameter.Add("Date Accessed",File.GetLastAccessTime(path));
                                    break;

                                case "Type":
                                    FileStream stream = File.Open(path, FileMode.Open);
                                    dynamicParameter.Add("Type", stream.GetType().ToString());
                                    stream.Close();
                                    stream.Dispose();
                                    break;

                                default:
                                    throw new Exception(" Field does not belong to File");

                            }

                        }
                    }  
                    args[1] = dynamicParameter;
                    break;

               case "CreateFile":
                    Dictionary<string, object> createValues = new Dictionary<string, object>();
                    int count = 0;

                    //
                    // Get the method parameters.
                    //
                    IParameterCollection parameterCollection = methodInstance.GetMethod().GetParameters();
                    foreach (IParameter parameter in parameterCollection)
                    {
                        if (parameter.Direction == DirectionType.In
                            || parameter.Direction == DirectionType.InOut)
                        {
                            createValues.Add(parameter.Name, args[count]);
                        }
                        count++;
                    }

                    path = directory + @"\" + args[0];
                    FileStream stream1 = File.Create(path);
                    stream1.Close();
                    stream1.Dispose();
                    args[args.Length - 1] = args[0];

                    break;
                    
                case "DeleteFile":
                    path = directory + @"\" + args[0];
                    if (!File.Exists(path))
                        throw new Exception("File does not exist to Delete");
                    File.Delete(path);
                    break;

                case "ReadFiles":
                    String[] fieldValues = GetFieldValues(methodInstance);
                    List<DynamicType> returnParameters = new List<DynamicType>();
                   // path = directory + @"\" + args[0];
                    if (!Directory.Exists(directory))
                        throw new Exception(" The directory does not exist for the Finder");
                    else
                    {
                        string[] Files = Directory.GetFiles(directory);
                        foreach(string FileName in Files)
                        {
                            DynamicType paraValue = new DynamicType();                                                      
                            foreach (String fieldValue in fieldValues)
                            {
                                
                                switch (fieldValue)
                                {
                                       
                                    case "FileName":
                                        FileInfo info = new FileInfo(FileName);
                                      
                                        paraValue.Add("FileName", info.Name);
                                        break;

                                    case "Date Modified":
                                        paraValue.Add("Date Modified", File.GetLastWriteTime(FileName));
                                        break;

                                    case "Attributes":
                                        FileAttributes attributes = File.GetAttributes(FileName);

                                        paraValue.Add("Attributes", attributes);
                                        break;

                                    case "Date Created":
                                        paraValue.Add("Date Created", File.GetCreationTime(FileName));
                                        break;

                                    case "Date Accessed":
                                        paraValue.Add("Date Accessed", File.GetLastAccessTime(FileName));
                                        break;

                                    case "Type":
                                        FileStream stream = File.Open(FileName, FileMode.Open);
                                        paraValue.Add("Type", stream.GetType().ToString());
                                        stream.Close();
                                        stream.Dispose();
                                        break;

                                    default:
                                        throw new Exception(" Field does not belong to File");

                                }
                            }
                            returnParameters.Add(paraValue);

                        }
                    }
                    args[1] = returnParameters.ToArray();
                    break;

                case "UpdateFile":
                   
                    Microsoft.BusinessData.Runtime.DynamicType updatedValues = args[0] as Microsoft.BusinessData.Runtime.DynamicType;
                    path = directory + @"\" + updatedValues[FileIDName];
                    if (!File.Exists(path))
                        throw new Exception("File does not exist to update");
                    foreach (KeyValuePair<string, object> kvp in updatedValues)
                    {
                        switch(kvp.Key)
                        {
                            case "FileName":                                 
                                break;

                            case "Date Modified":
                                File.SetLastWriteTime(path,(DateTime) kvp.Value);
                                break;

                            case "Attributes":
                                FileAttributes updatedAttribs = (FileAttributes)kvp.Value;
                                File.SetAttributes(path, updatedAttribs);                                
                                break;

                            case "Date Created":
                                File.SetCreationTime(path,(DateTime)kvp.Value);
                                break;

                            case "Date Accessed":
                                File.SetLastWriteTime(path,(DateTime)kvp.Value);
                                break;

                           default:
                                throw new Exception(" Field does not belong to File");



                        }
                    }
                    break; 

                default:
                    throw new Exception("Method Not Implemented called");
                }
            } 
     
       private String[] GetFieldValues(IMethodInstance methodInstance)
        {
           List<String> fieldsList = new List<string>();
            IMethod method = methodInstance.GetMethod();
            ITypeDescriptor typeDescriptor;
                
               typeDescriptor = method.GetParameters()[1].GetRootTypeDescriptor();
               ITypeDescriptor childTypeDescriptor = typeDescriptor.GetChildTypeDescriptors()[0];

               if (methodInstance.MethodInstanceType == MethodInstanceType.Finder)
               {
                   foreach (ITypeDescriptor childDescpritor in childTypeDescriptor.GetChildTypeDescriptors())
                   {

                       fieldsList.Add(childDescpritor.Name);

                   }
               }
               else
               {

                   foreach (ITypeDescriptor childDescpritor in typeDescriptor.GetChildTypeDescriptors())
                   {
                       fieldsList.Add(childDescpritor.Name);
                   }
               }
            return fieldsList.ToArray();
        }

        private String[] GetUpdateFieldValues(IMethodInstance methodInstance)
        {
            List<String> fieldsList = new List<string>();
            IMethod method = methodInstance.GetMethod();
            ITypeDescriptor typeDescriptor = method.GetParameters()[0].GetRootTypeDescriptor();

            foreach (ITypeDescriptor childDescpritor in typeDescriptor.GetChildTypeDescriptors())
            {
                fieldsList.Add(childDescpritor.Name);
            }
            return fieldsList.ToArray();
        }

       
        }

        #endregion

    }

See Also

Reference

AdministrableProperty

ISystemUtility

ITypeReflector

IConnectionManager

IAdministrableSystem

IMethodInstance

GetMethod()

IDataClass

ILobSystemInstance

IExecutionContext

IsPropertyDefined(String)

ISharedEntityState

DynamicType

Add(String, Object)

IParameterCollection

GetParameters()

IParameterCollection

IParameter

Direction

GetRootTypeDescriptor()

ITypeDescriptor

GetChildTypeDescriptors()

MethodInstanceType

MethodInstanceType

Concepts

Sample BDC Model: Connecting to a Custom File System Connector