Sharepoint 2010 event handler to create subsites

You may also like...

19 Responses

  1. Dennis says:

    Does this work for SharePoint 2013? Can I use VS 2012 to build?


    • Arild Aarnes says:

      It should work pretty much the same for Sharepoint 2013 and Visual Studio 2012. Have not tried it yet but as far as I know this should work fine.

  2. Varun says:

    HI Arlid,

    I am trying to do that same thing as Chad mentioned. I am stuck and trying find out how do I get the GUID column from another list but not sure how to do that if you let me know that would be great . I mean to say hard code to get the value of GUID if I create Site Type lookup field column in Subsite.

  3. Frenzy says:

    Is it possible that when the site is automatically deployed that a specific security group is automatically associated with the newly (and automatically) created site? I’m looking at doing the same as what this article states but the user will be using a workflow that, when he or she approves a certain decision (or step) that when they select “approve” from a drop down that event automatically generates a new sub-site and I’d like to have a specific security group (AD or SharePoint Group) associated with that site.

    How can I do that?

    Thank you

    • Arild Aarnes says:

      You could use code similar to this to add specific permissions to the newly created site.

      void setLoginAccess(string loginName, SPWeb newsubsite)
      SPRoleAssignment roleAssign = new SPRoleAssignment(loginName, “”, loginName, “Added by SP”);
      SPRoleDefinition roleDefinition = newsubsite.RoleDefinitions[“Contribute”];
      This will add the user given in loginName to the site in the Contributor role. This is just an example so use the SPRoleAssignment and SPRoleDefinition clases to add the user or group you need to the Role you need on the Sharepoint Site.

  4. Kenny Bright says:

    I was just playing around and found your site. I wrote a a sample client program using client object model that can find the webtemplate id of any site (limited by sites the Client object model will allow you to browse). Thanks for sharing your

    p.s. – replace with your site.

    sample.using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Client;

    namespace ConsoleApplication1
    class Program
    static void Main(string[] args)
    ClientContext ctx = new ClientContext(“http://”);
    Web web = ctx.Web;

    WebTemplateCollection coll = web.GetAvailableWebTemplates(1033, false);
    IEnumerable result = ctx.LoadQuery(coll.Include(o => o.Id, o => o.Description, o => o.Name));

    foreach (WebTemplate item in result)
    Console.WriteLine(“Item Id:{0}\n Description:{1}\n Name:{2}\n “, item.Id, item.Description, item.Name);


    Console.WriteLine(“press any key to continue….”);

  5. Sagar says:

    Thanks you so much !!

  6. Eric says:

    You’re brilliant dude.

    Can’t overstate how much I love when you’re looking for a solution to a problem and find a guide where everything works 100% (at least for me it did).

    Great job!

  7. Satya says:

    I need a confirmation before creating a subsite (ex. a popup dailogue (yes/No)….Is there any way to do this??

    • Arild Aarnes says:

      You would have to do the confirmation before the event handler executes. You cannot show a user dialog from the event handler.
      You would have to edit the New Item form for the list and use javascript to ask confirmation from the user before actually creating the item.
      If you are running Sharepoint 2010 Enterprise you can easily replace the built-in form with a Infopath form and create the confirmation dialog there, if not you could edit the New Item form for the list in Sharepoint Designer.

  8. Satya says:

    Awesome, Same thing i am looking for… Thanks a lot…

  9. Paul Perry says:

    Nice post, I’m doing a similar thing right now, actually, except in my case, the naming convention hasn’t been agreed for each site, so I’d like to use the list item’s ID: do we have a way to get that before making the subsite, so we can use it?



    • Arild Aarnes says:

      I’m afraid I’m not sure which ID you mean? If it’s the ID of the item the user creates in the SubSite list you should be able to get that. You can try properties.AfterProperties[“ID”] to get the ID of the item that raised the event. I haven’t tested this yet so maybe there is an issue with the ID property beccause we are in the ItemAdding event and the item is not actually created yet.


  10. Jo Arnsppiger says:

    I don’t have VS 2010. I have only VS 2008. Can I do the same thing?

    • Arild Aarnes says:

      Yes you should be able to use the exact same code for the event handler in Visual Studio 2008.

  11. Chad says:

    is it possible for subsite list to have a pulldown list of site types, like project site, team site, accounting site, etc… and based on the choice the event handler would choose the appropiate custom template when creating the subsite? that would be cool. I don’t have an VS experience so how do you deploy the solution to the farm/site collection from VS?

    • Arild Aarnes says:

      Yes it would be possible to have the users select specific template from a dropdown list. I guess I would implement this as a lookup field in the Subsite list and connect this to a list where we have 2 fields. One for the template name and one for the template GUID. The code for the event handler would then have to look up the GUID for the selected template in this list and create a new site based on this.

      In Visual Studio 2010 you can build a package from the solution. The solution will then be packaged into a WSP file that you can install on a Sharepoint server/farm. the event handler will be installed as a Feature and you enable the feature on sites where it’s needed.

  1. July 24, 2012

    […] can read the full article here on SharePoint 247. Props on the great article, guys! This entry was posted in Development, SharePoint 2010 by […]

Leave a Reply

Your email address will not be published. Required fields are marked *