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.

Jul 24

Here's the code to download files of a certain type (in the example, I set it to find documents in the list with the .doc extension) from a document library to your hard drive.

    SPWeb mySite = SPContext.Current.Web;
    mySite.AllowUnsafeUpdates = true;
    SPList oList = mySite.Lists[<ListName>];
    SPQuery oQuery = new SPQuery();
    oQuery.Query = "<Where><Eq><FieldRef Name='DocIcon'/><Value Type='Computed'>doc</Value></Eq></Where>";
    SPListItemCollection listItems = oList.GetItems(oQuery);

    for (int i = 0; i < listItems.Count; i++) {
            SPListItem item = listItems[i];

            // copy this file to the temp folder
            byte[] binfile = item.File.OpenBinary();

            FileStream fs = new FileStream("C:\\Temp\\" + item["Name"], FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(binfile);
            bw.Close();
    }

Jul 21

Here's a high overview of how to complete this along with the names of the files required to do this. Most of these files are available from the microsoft site. Just do a google search for the file name and you will be redirected to the download pages. Some files will require licensing (eg. Windows 2003 Server, MOSS and Visual Studio)

1. Install Windows server 2003 64 bit with ISO (SW_CD_Windows_Svr_Std_2003_R2_64-BIT_English_-2_x64_R2_CD1_ISO_MLF_X12-58050.ISO)
2. Install Windows Server 2003 SP2 (WindowsServer2003.WindowsXP-KB914961-SP2-x64-ENU.exe)
3. Install Windows Installer 64bit (WindowsServer2003-KB942288-v4-x64.exe)
4. Install .net Framework 2.0 SP2 64 bit (NetFx20SP2_x64.exe)
5. Install SQL Server Express 64 bit (SQLEXPR_x64_ENU.exe)
6. Install .net 3.5 (dotNetFx35setup.exe)
7. Install SharePoint 64 bit (OfficeServerwithSP1.exe)
8. Install Visual Studio 2008 from CD
9. Upgrade Visual Studio to SP1 (VS90sp1-KB945140-ENU.exe)
10. Install Visual Studio Extensions (VSeWSSv13_AMD64_Build-433.exe)
11. Install PowerShell 64 bit (WindowsServer2003.WindowsXP-KB926139-v2-x64-ENU.exe)
12. Install SQL Server Management Studio Express 64-bit (SQLManagementStudio_x64_ENU.exe)

Optional: Install IE8 (IE8-WindowsServer2003-x64-ENU.exe)

« Previous Entries Next Entries »