Check out my new blog at https://shibumiware.blogspot.com

Wednesday, January 30, 2008

Adventures in Add-Ins Continued...

I had some fun today with this add-in I have been working on.

I am persisting out the maps between SharePoint lists and Project custom fields by serializing a custom object model. This makes life easy... except I haven't really been creating the add-in as an add-in, but rather I have been working by calling out to the add-in assembly from a test harness. I have plans for the core add-in functionality that requires the logic be exposed for consumption from both add-ins as well as other application architectures. So, everything worked fine until I tried it out running as an add-in. My serialization code was failing, which makes complete sense given that the layout is different when running under Microsoft Project versus a .NET Windows test harness.


I had to hook the ResolveAssembly event of the AppDomain and explicitly load one of the assemblies required for serialization because it is installed alongside the add-in and not in the global cache... You can see that below:

    public static FieldMap Load(string path)
    {            
        using (StreamReader streamReader = new StreamReader(path))
        {                
            FieldMap fieldMapper;
            BinaryFormatter binaryFormatter = new BinaryFormatter();                                

            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            
            fieldMapper = (FieldMap) binaryFormatter.Deserialize(streamReader.BaseStream);

            AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;

            streamReader.Close();

            return fieldMapper;
        }
    }

    private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        if (args.Name.Split(',')[0] == "IIL.Products.TW.Libraries.Integration.FieldMap")
        {
            string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), 
                                                            "IIL.Products.TW.Libraries.Integration.FieldMap.dll");

            return Assembly.LoadFile(path);
        }
        else
        {
            Assembly[] lasms = AppDomain.CurrentDomain.GetAssemblies();

            foreach (Assembly lasm in lasms)
            {
                if (args.Name.Split(',')[0] == lasm.FullName.Split(',')[0])
                {
                    return lasm;
                }
            }
        }

        throw new InvalidOperationException("Unavailable assembly requested for serialization operation."); 
    }


The article that got me headed in the right direction can be found here.

The add-in has a little UI where the relationships between SharePoint lists and Project fields are defined. Check it out...



Oh yeah, and don't forget to call EnableVisualStyles() if you want your add-in to look nice ;-)

No comments :

Disclaimer

Content on this site is provided "AS IS" with no warranties and confers no rights. Additionally, all content on this site is my own personal opinion and does not represent my employer's view in any way.