Monday
Mar012010

Making Updates to SharePoint from JavaScript

While working on a project involving SharePoint, our effort to provide a better UI required making some updates from JavaScript. If you make an attempt at this you are likely to get the following error.

Microsoft.SharePoint.SPException was unhandled by user code Message="The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again." Source="Microsoft.SharePoint"

What's going on is SharePoint trying to prevent cross-site scripting. You will find many articles saying to wrap your update setting AllowUnsafeUpdates like below.

myWeb.AllowUnsafeUpdates = true;
// Call Update();
myWeb.AllowUnsafeUpdates = false;

Here you can find an explanation of AllowUnsafeUpdates.

http://hristopavlov.wordpress.com/2008/05/16/what-you-need-to-know-about-allowunsafeupdates/

I opted to not use AllowUnsafeUpdates. Instead I went with the POST approach. This requires a control added to your page.

<sharepoint:formdigest runat="server"/>

MSDN states: “To make posts from a Web application that modify the contents of the database, you must include the FormDigest control in the form making the post. The FormDigest control generates a security validation, or message digest, to help prevent the type of attack whereby a user is tricked into posting data to the server without knowing it.

So now your page will have a few new additions to it. Namely a small JavaScript call on form submit and a hidden input field with id=”__REQUESTDIGEST”. The OnSubmit call is to WebForm_OnSubmit() and will populate the value of the hidden input field. All that is needed is for your JavaScript code to call this method itself and then send the key-value of __REQUESTDIGEST in along with your post data.

Monday
Feb012010

Creating A Bootable USB Flash Drive

As a developer I often find it useful to be able to quickly set up a new machine. I find the best way to do so is to install Windows from a  bootable USB flash drive. I find a flash drive to be more readily available and much easier to carry around then a DVD not to mention the read times are many times faster. The steps needed to get this going couldn't be easier.

 

1. Prepare Flash Drive

With the help of our old friend diskpart we can easily ready the drive for booting.

  1. Open the command prompt.
  2. Start diskpart
    C:\>diskpart
  3. Type list disk to ensure we have the correct disk to clean
    DISKPART> list disk
      Disk ###  Status         Size     Free     Dyn  Gpt
      --------  -------------  -------  -------  ---  ---
      Disk 0    Online          186 GB      0 B
      Disk 1    Online         3831 MB      0 B
  4. My disk is addressed as Disk 1 in this case so the next command is select disk 1
    DISKPART> select disk 1
  5. clean
    DISKPART> clean
  6. create partition primary
    DISKPART> create partition primary
  7. select partition 1
    DISKPART> select partition 1
  8. active
    DISKPART> active
  9. format fs=fat32
    DISKPART> format fs=fat32
  10. assign
    DISKPART> assign
  11. exit
    DISKPART> exit

2. Copy DVD to Flash Drive

My DVD is in drive d:\ so I will use xcopy to copy the files from d:\ to my flash drive in e:\

C:\>xcopy d:\*.* /s/e/f e:\

3. Ensure BIOS on target machine set to boot from USB

Since BIOS menus vary I cannot go into the details of setting up your computers BIOS to boot from USB. Typically you can enter the BIOS by hitting Delete, F1, or some other key at the beginning of the boot process. Find the boot order portions and set the USB to be the first item in the boot order.

4. Install Windows

Insert the USB flash drive and restart the system. It should boot from the USB drive and begin the setup process.

 

Abbreviated steps from above:

  1. diskpart
  2. select disk 1
  3. clean
  4. create partition primary
  5. select partition 1
  6. active
  7. format fs=fat32
  8. assign
  9. exit
  10. xcopy d:\*.* /s/e/f e:\

Links: xcopy Documentation, DiskPart