Dec 11

I found that when I created a custom outlook add-in to run on the Send event, that it doesn't run when I deploy it on another machine (works fine on my own). After much research, I found that a requirement on the other machine is not included in the deployment files. We needed Extensibility.dll to be installed on the target machine. For some reason, this is not included in VS 2008, despite there being a KB article for this file for VS2005. Research shows that the majority of people had to downgrade back to VS2005 to get Extensibility.dll installed on target machines, but that is not necessary.

First you need to have Visual Studio 2005 installed somewhere so you can download the kb article from here and install it: http://support.microsoft.com/kb/908002. Without VS 2005, you won't be able to install this kb (which I find silly because you need it for VS2008).

After installing, you can then do one of the following 2 ways.

The hard way...

1.    Navigate to C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\BootStrapper\Packages, locate KB908002 folder.
2.    Log on to the machine which has Visual Studio 2008 installed and copy KB908002 folder to C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages

Now if you go to the pre-requisites list on VS 2008 (right click on the Setup and view properties, then click prerequisites), you'll see a new prerequisite, Shared Add-in Support Update for Microsoft .NET Framework 2.0. Check that and select "Download prerequisites from the same location as my application. This way, when you build it, a new folder is created with the required MSI in your deployment folder.

Or the easier way...

Navigate to C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\BootStrapper\Packages on the PC with VS2005 on it, locate KB908002 folder. Copy the extensibilityMSM.msi file to the target machine and install it there.

Which method you choose will depend on your own personal requirements.

Note that depending on what you need, you may need to right click on "Office.dll" in the Detected Dependencies folder and include it. I had to include Office.dll in my project too for the add-in to function.

Dec 11

In Visual Studio, create a new Project > Other Project Types > Extensibility > Shared Add-in and select the application or applications you are writing the add-in for. In my case, the add in is written for Outlook, so I unchecked everything and selected Outlook only. Hit next, give it a name and description. Then next again and check both boxes. Then Finish.

Since I'm writing code for the Send event, I need to have a new method:

    void OutlookApplication_ItemSend(object Item, ref bool Cancel)
    {
        MessageBox.Show("Item sent");
    }

That's it, now you need to package it into an MSI to install it on another machine. If all goes well, it will work on the new machine and you will have no problems, however that is not the case on most machines as I will be discussing in my next article.

Off topic

When you see the properties of the Setup file, set the Manufacturer to the name of the folder where you want this installed by default and also set the InstallAllUsers to True. On installation, the user has the option of changing it anyway, but it's good to have these set as default.

Sep 17

Here's a small snippet of code to upload a file from the filesystem into a SharePoint document library:

    Stream fStream = File.OpenRead("C:\\Temp\\Filename.pdf");
    byte[] contents = new byte[fStream.Length];

    fStream.Read(contents, 0, (int)fStream.Length);
    fStream.Close();

    SPList list = web.Lists[DocumentLibraryListName];
    Hashtable properties = new Hashtable();
    properties.Add("Title", "This is the title");
    properties.Add("Other_x0020_Properties", "Some other property");

    SPFile destFile = list.RootFolder.Files.Add("Filename.pdf", contents, properties, true);

    if (destFile == null)
    {
        lblError.Text = "<p>Error in adding file. Waiting to re-try.</p>";
    }

There is a free class available named DocLibHelper (search Google to download it) that would help you in uploading files among other things, but if all you need is to upload a document into a SharePoint library, then the above code will do.

Sep 14
Using C# to update windows registry
Posted by Wei in C# on 09 14th, 2009| | No Comments »

Here's the code to update the registry:

string strPath = "Software\\Path\\To\\Registry\\Location";
RegistryKey regKeyAppRoot = Registry.CurrentUser.OpenSubKey(strPath, true);
regKeyAppRoot.SetValue("Key1", "Value1");
regKeyAppRoot.SetValue("Key2", "Value2");
regKeyAppRoot.Close();

If the key doesn't exist, then you will get a null exception error: "System.NullReferenceException: Object reference not set to an instance of an object." This just means that you have to create the key before you can update it.

RegistryKey rk = Registry.CurrentUser.CreateSubKey(strPath);
rk.Close();

Initially, I thought it was a permission issue with updating the registry. So I added owner permissions to pretty much anyone and everyone. But I still got the issue. Finally figured out that all I had to do was create the Subkey. Hopefully I just saved someone hours of work.

Aug 25

This code will take a file as an input from the file system and add a watermark to that file.

    string FileLocation = "c:\\Temp\\SomeFile.pdf";
    string WatermarkLocation = "c:\\Temp\\watermark.gif";

    Document document = new Document();
    PdfReader pdfReader = new PdfReader(FileLocation);
    PdfStamper stamp = new PdfStamper(pdfReader, new FileStream(FileLocation.Replace(".pdf","[temp][file].pdf"), FileMode.Create));

    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(WatermarkLocation);
    img.SetAbsolutePosition(250,300); // set the position in the document where you want the watermark to appear (0,0 = bottom left corner of the page)

    PdfContentByte waterMark;
    for (int page = 1; page <= pdfReader.NumberOfPages; page++)
    {
        waterMark = stamp.GetUnderContent(page);
        waterMark.AddImage(img);
    }
    stamp.FormFlattening = true;
    stamp.Close();

    // now delete the original file and rename the temp file to the original file
    File.Delete(FileLocation);
    File.Move(FileLocation.Replace(".pdf", "[temp][file].pdf"), FileLocation);

For the above to work, you need to make sure that the script has permissions to read and write from the C:\Temp folder. You also need to reference iTextSharp.dll.

« Previous Entries