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.

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();
    }

« Previous Entries Next Entries »