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.
- Project Name
- Project Description
- Start Date
- End Date
- Team Members
Step 1: Create Projects list
Please proceed to create a new list named Projects. Add the columns as displayed below.
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.
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.
Choose the custom list option and "item was added" event as in the following:
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.
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.
Scrolling down to the bottom of the page, you will see our new site named Supercast.
On clicking the sub site, it opens as in the following:
In this example, we have stopped after creation of the sub sites. In real-world projects we need to:
- Create an Internal List with all project information
- In the add event, create a web part display project information from list and add to home page
- In the original Projects list, create a new property named URL and set that to the newly created site
- In the new site, Owners and Members permission groups can be automatically created
- 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.