In this article we can explore a scenario I have encountered while working with SharePoint.
My client had a Production issue where the Request Tracking report is too huge in size and cannot be downloaded. Although the problem is fixed, we need to test this in our UAT environment.
To test this we need 1000 requests logged in the SharePoint site. How to create the 1000 requests?
I had the following interesting ideas & solutions.
Idea 1: Create an SQL stored procedure that inserts 1000 records to the Content Database.
Problems: Content Database access issue, directly modifying is not recommended
Idea 2: Create a web part that inserts records into the Audit Log using Server Object Model
Problems: Some of the fields will remain null
Idea 3: Click the browser refresh button for 1000 times
Problems: Too hectic for a human to perform
Idea 4: Place a lock on the F5 key
Problems: The site requires 3 seconds for loading, so skips all the key requests if continuous
Idea 5: Create a Web Client automation
Problems: The site is under security proxies. So the URL automation is not possible.
Idea 6: Send Keys to Browser
I selected the Idea 6 as the site is authenticated in a browser window. Through a separate windows application we can send Refresh keys (F5) on regular intervals using Windows API.
Windows API invocation can be done in .Net using Platform Invoke infrastructure.
Following are the steps involved in the application.
1. Find the Window Handle using the Title
a. For example: "Bing - Windows Internet Explorer" is the title of Internet Explorer while opening website http://www.bing.com/
2. Make the Window as Active Window
a. Use the SetForegroundWindow API method to make the window as active, so that we can send keys to active window
3. Send the Key to the window
a. Send Key (F5) to the active window
4. Repeat the loop
a. Continue the loop with an interval specified by the user.
Following are the Windows API methods required by our application.
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindow (IntPtr ZeroOnly, string lpWindowName);
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, IntPtr dwExtraInfo);
static extern bool SetForegroundWindow(IntPtr hWnd);
Following is the code to get the Window Handle by caption.
_pointer = FindWindow(new IntPtr(0), TitleText.Text);
If the _pointer has a value greater than 0, we are good to have a valid Handle.
A handle (HWND) is the return value from CreateWindowEx which the Windows Operating System uses to identify a window.
Create a windows forms application and set the following controls.
On the start button click add the following code.
private void StartButton_Click(object sender, EventArgs e)
private bool _start;
private int _count;
private void Start()
_start = true;
StartButton.Enabled = false;
StopButton.Enabled = true;
Thread.Sleep(int.Parse(textBox3.Text) * 1000);
public void SendKeys()
int key = int.Parse(KeyText.Text);
const int KEYEVENTF_KEYUP = 0x0002;
keybd_event((byte)key, 0, 0, IntPtr.Zero);
keybd_event((byte)key, 0, KEYEVENTF_KEYUP, IntPtr.Zero);
// The keybd_event() method SENDS the key
Open a browser and browse to our SharePoint site. Ensure you have logged in correctly.
Now execute our application and enter the title of the Browser in the application. In this case it is: "Reports - All Documents - Windows Internet Explorer"
Enter the Key value. For F5 key, it is ASCII 116. Set the Interval as 4 seconds which will create around 900 requests per hour.
After setting the values click the Start button. You will see that the browser is made as foreground window and the Refresh process starts repeating every 4 seconds. (Observe the Refresh of browser status bar)
In this article we have explored a Send Key scenario with Windows API as the solution. You can comment on better ideas for the problem.