In one interview, interviewer asked this question. I have one table in sql server database which contains the Title and its id(int). Now I want to retrieve those values from database and based on the title, I have to create sites under one root website. How many values are there, that many sites needs to be create.

    Title                    ID  
    A                        1
    B                        2
    C                        3

These are the values. Now I want to creata these three sites at a time. How to achieve it and if I change title of the site then it should update in the database as well. Any suggestions please


This should not be hard, here is pseudocode:

Connect to this database and retrieve all the rows in this table
Connect to the desired SharePoint Site Collection (SPSite)

Foreach row in rows
       Add a web site under SPSite.RootWeb.Webs using Add method

For more details check SPSite, SPWeb, SPWebCollection objects and SPWebCollection.Add methods.

  • What about the update method. When we update the title of a site it should reflect in the database – Mihir Aug 11 '12 at 9:12
  • 1
    Unfortunately SharePoint only has event handler that fires when an SPWeb object is created, there is no onupdate event. msdn.microsoft.com/en-us/library/…. You would have to build some scheduled check that iterates SPSite.AllWebs collection and checks if changes were made. – Toni Frankola Aug 11 '12 at 9:36
  1. One other alternative would be to create a BDC Entity out of your Table with SharePoint Designer, then from the newly External List have a new Workflow again via SharePoint Designer to start on item Creation and create a site automatically based on details. On update (ONLY via SharePoint list), use "Wait for a field change" activity you could update respective site. Both Create/Update site would rely on a custom Workflow Activity which is the only piece of code the would create/update site.
  2. Or directly from within your SQL Server (particularly if you have a single-server farm) rely on SQL Triggers to call .NET Managed procedures which could use SharePoint Client Side Managed Code to create/update sites. - more complex honestly (security wise particularly).
  • I am not sure item 1. is an option. SharePoint workflows can't be triggered by External Lists. They can, once running from someplace else, read/write to external lists. Ref: msdn.microsoft.com/en-us/library/ff394479.aspx "Workflows cannot be associated with an external list. In SharePoint Server 2010, although workflows work with external lists, they cannot be associated directly with external lists. You can create a site workflow, or have a list workflow on a SharePoint list, such as a document library, and have it read or update from an external list." – lwbecker2 Aug 13 '12 at 15:41
  • 1. Indeed you cannot direclty but ingenious alternatives allow to bridge that gap (more here sharepoint.stackexchange.com/questions/19418/…) – Marius Constantinescu - MVP Aug 13 '12 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.