Create Site Automatically When a List Item is Added

In this article I would like to take you through a real-world scenario.

Scenario

In your company there are multiple projects going on. Each project requires a separate site. The site creation must be done through a list item creation.

A list named Projects exists in the root SharePoint site. A user can add a new item in the list along with project information.

  1. Project Name
  2. Project Description
  3. Start Date
  4. End Date
  5. Team Members

Step 1: Create Projects list

Please proceed to create a new list named Projects. Add the columns as displayed below.

Share1.jpg

Ensure that the Team Members column is of type User or Group and that it allows multiple selections.

Step 2: Create SharePoint Project

Now we can start with our Event Handler Project. Open Visual Studio 2012 and create an empty SharePoint Project.

Share2.jpg

If you do not have Visual Studio 2012 then you need to install it first. After that, install the Office Tools for Visual Studio 2012, it contains the SharePoint project templates.

In the next page, choose the Farm Solution option.

Now, right-click on the project and add an Event Receiver.

Share3.jpg

Choose the custom list option and "item was added" event as in the following:

Share4.jpg

In the item added event, place the following Site Creation code.

public override void ItemAdded(SPItemEventProperties properties)

{

    base.ItemAdded(properties);

 

    if (properties.List.Title == "Projects")

    {

        // Get Properties

        string name = properties.ListItem["Title"].ToString();

        string description = properties.ListItem["Description"].ToString();

        DateTime startDate = (DateTime)properties.ListItem["Start Date"];

        DateTime endDate = (DateTime)properties.ListItem["End Date"];

 

        // Create sub site

        SPWeb web = properties.Site.AllWebs.Add(name.Replace(" ", string.Empty), name,

            description, 0, SPWebTemplate.WebTemplateSTS, false, false);

        web.Update();

    }

}

Step 3: Execute Project

Now execute the project using the F5 key and while debugging is active, create a new project item in the Projects list.

Share5.jpg

You will see that a new sub site is created based on the input information.

Step 4: View sub site

Use the Site Contents link to view the sub sites.

Share6.jpg

Scrolling down to the bottom of the page, you will see our new site named Supercast.

Share7.jpg

On clicking the sub site, it opens as in the following:

Share8.jpg

In this example, we have stopped after creation of the sub sites. In real-world projects we need to:

  1. Create an Internal List with all project information
  2. In the add event, create a web part display project information from list and add to home page
  3. In the original Projects list, create a new property named URL and set that to the newly created site
  4. In the new site, Owners and Members permission groups can be automatically created
  5. Create common project library, list, meeting, discussion lists in the site.

In an appropriate sense, the custom solution can include a custom site template with all pre-defined lists and libraries.

References

http://bit.ly/137vrNA

Summary

In this article we have explored a real-world scenario of project site creation by adding an event from a list item. The source code is attached with the article.
 

Up Next
    Ebook Download
    View all
    Learn
    View all