1

I am creating a custom library for sending emails. Idea is to use System.Net.Mail or Microsoft.SharePoint. Code will be executed in enviroment that has System.Net.Mail but not necessarily Microsoft.SharePoint.

Sharepoint library will be used only if Sharepoint address is set in configuration file.

The question is how to use Microsoft.SharePoint library without crashing program if whole library doesnt exist in a executed enviroment?

I do not want to add Microsoft.SharePoint library because it is against Microsoft rules.

 If(Enviroment.Libraries.Contains(Microsoft.Sharepoint)
    Use Microsoft.Sharepoint
 Else 
    Use System.Net.Mail

:)

This is the problem:

Could not load file or assembly 'Microsoft.SharePoint, Version=14.0.0.0,
Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies.
The system cannot find the file specified.

I have tried

using (Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Shared Tools\\Web Server Extensions\\14.0", false))
        {
            if (regKey != null)
            {
                string installStatus = System.Convert.ToString(regKey.GetValue("SharePoint"), System.Globalization.CultureInfo.InvariantCulture);
                if (!string.IsNullOrEmpty(installStatus) && installStatus.Equals("Installed", System.StringComparison.OrdinalIgnoreCase))
                {
                    System.Console.WriteLine("Installed");
                }
            }

            System.Console.WriteLine("NOT Installed");
        }

I can see (using regedit) that SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0\SharePoint is there but that code just does not see it :(

Painkiller
  • 109
  • 1
  • 1
  • 10
  • 2
    You'd be better of shipping the sharepoint DLL as part of your assembly. I think this nuget package might contain what you want https://www.nuget.org/packages/Microsoft.SharePoint.Client.dll/ – Jamie Dixon Aug 13 '15 at 07:57
  • Which source line causes the exception? The solution will be: Check the SharePoint assembly is available with Assembly.Load(), and _only_ conditionally execute any statement, variable declaration etc which uses types defined in that assembly – g.pickardou Aug 13 '15 at 07:58
  • thank you both. i will try and respond later if any of that worked for me – Painkiller Aug 13 '15 at 08:07
  • That usually happens when you don't set `Copy to local` to true for the referenced file. So whatever you ship might be missing the file. – Dumisani Aug 13 '15 at 08:13

0 Answers0