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.

Dec 2

stsadm -o setproperty -propertyname max-template-document-size -propertyvalue 500000000
(hard limit of 500Mb)
 
Then to export the site and import it, run the following commands on your intranet
 
Note: you will need to give full control permissions to whoever is logged in on the intranet server for the site being imported to. What I did was to give full control to that account, then remove the user once the import is completed.
 
stsadm -o export -url http://intranet/path/to/website -includeusersecurity -nofilecompression -filename C:\somebackupfolder

Create the new site on your intranet.

stsadm -o import -url http://intranet/new/site -includeusersecurity -nofilecompression -filename C:\somebackupfolder

Oct 14

Here's a quick code snippet for updating user profile information in SharePoint

    SPSite siteCollection = null;
    SPWeb web = null;
    siteCollection = SPContext.Current.Site;
    web = siteCollection.RootWeb;

    web.AllowUnsafeUpdates = true;
    SPList list = web.Lists["User Information List"];
    SPUser user = web.SiteUsers["DOMAIN\user"];
    SPListItem item = list.Items.GetItemById(user.ID);
    item["FirstName"] = "John";
    item["LastName"] = "Smith";
    item["WorkPhone"] = "55378008";
    item["Office"] = "BigScholar.com";
    item.Update();

The List name needs to remain as "User Information List".

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.

« Previous Entries Next Entries »