Populate SharePoint List for load testing with dummy data

Leave a comment

February 4, 2014 by Ravi C Khambhati

I will do this task in two steps, first step is to create folder structure and then add items.

This code will be helpful in load testing.

Step 1: Create folder structure

private static void CreateFolderStructure()
{
    using (SPSite spSite = new SPSite("http://servername/sites/test/"))
    {
        using (SPWeb spWeb = spSite.OpenWeb())
        {
            SPList spList = spWeb.Lists["LargeList"];

            CreateFolderStructure(spWeb, spList);
        }
    }
}

private static void CreateFolderStructure(SPWeb spWeb, SPList spList)
{
    string addItemFormat = "<Method ID=\"{0:D3}\">" +
                                "<SetList>{1}</SetList>" +
                                "<SetVar Name=\"Cmd\">Save</SetVar>" +
                                "<SetVar Name=\"ID\">New</SetVar>" +
                                "<SetVar Name=\"urn:schemas-microsoft-com:office:office#Title\">{0:D3}</SetVar>" +
                                "<SetVar Name=\"Type\">1</SetVar> " +
                                "<SetVar Name=\"owsfileref\">{2}</SetVar>" +
                            "</Method>";

    StringBuilder sb = new StringBuilder();
    sb.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ows:Batch OnError=\"Return\">");

    for (int counter = 1; counter <= 10; counter++)
    {
        sb.AppendFormat(addItemFormat,
                            string.Format("{0:D3}", counter),
                            spList.ID,
                            string.Format("{0}/{1}/{2:D3}", spWeb.ServerRelativeUrl, spList.RootFolder, counter));

        for (int counter2 = 1; counter2 <= 10; counter2++)
        {
            sb.AppendFormat(addItemFormat,
                            string.Format("{0:D3}", counter2),
                            spList.ID,
                            string.Format("{0}/{1}/{2:D3}/{3:D3}", spWeb.ServerRelativeUrl, spList.RootFolder, counter, counter2));
        }
    }

    sb.Append("</ows:Batch>");
    Console.WriteLine(spWeb.ProcessBatchData(sb.ToString()));
}

Step 2: Create bulk entries

private static void UploadItems()
{
    using (SPSite spSite = new SPSite("http://servername/sites/test/"))
    {
        using (SPWeb spWeb = spSite.OpenWeb())
        {
            SPList spList = spWeb.Lists["LargeList"];

            SPListItemCollection items = spList.GetItems(new SPQuery());
            SPListItemCollection itemColl = spList.Items;

            foreach (SPListItem item in spList.Folders)
            {
                bool isFolder = (Convert.ToInt16(item["FSObjType"]) == 1 ? true : false);

                if (isFolder)
                {
                    Console.WriteLine(item.Folder.ServerRelativeUrl);

                    StringBuilder sb = new StringBuilder();

                    string addItemFormat = "<Method ID=\"{0}\">" +
                                                "<SetList>{1}</SetList>" +
                                                "<SetVar Name=\"Cmd\">Save</SetVar>" +
                                                "<SetVar Name=\"ID\">New</SetVar>" +
                                                "<SetVar Name=\"urn:schemas-microsoft-com:office:office#Title\">{0}</SetVar>" +
                                                "<SetVar Name=\"RootFolder\">{2}</SetVar>" +
                                            "</Method>";

                    sb.AppendFormat("<?xml version=\"1.0\" encoding=\"UTF-8\"?><ows:Batch OnError=\"Return\" RootFolder=\"{0}\">", item["FileRef"]);

                    for (int counter = 1; counter <= 1000; counter++)
                    {
                        sb.AppendFormat(addItemFormat,
                            string.Format("{0}_{1}", item.Title, counter),
                            spList.ID,
                            item["FileRef"]);
                    }

                    sb.Append("</ows:Batch>");

                    Console.WriteLine(spWeb.ProcessBatchData(sb.ToString()));
                }
            }
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: