Search code examples
c#sharepoint-2007workflowsharepoint-designer

How to create a new sharepoint site based on a template after a new item is added to a List


How can I create a new sharepoint site based on a template after a new item is added to a list, for example I have the the sharepoint site "A" with a list with info about projects, and the idea is that after I add a new project to that list, I need to automatically create a new sharepoint site (teamspace) for that project.

Also I want to know if it is possible to create this using a workflow or maybe if I can call the webservice(c#) with the workflow.


Solution

  • Ok well first you have to create a sharepoint project with visual studio in there you can write c#

    to create a site in c#

     SPSite topLevelSite = new SPSite("http://localhost");
     SPWeb spWebInstance = topLevelSite.OpenWeb();
     String siteTemplate = spWebInstance.WebTemplate;
     try
     {
    SharePointWebInstance.Webs.Add("the name", "name", "new site added",    (UInt32)System.Globalization.CultureInfo.CurrentCulture.LCID, siteTemplate, false, false);
      }
     catch(Exception ex)
     {
      //...
     }
     finally
     {
     topLevelSite.Close();
     SharePointWebInstance.Dispose();
     }
    

    just to get you started, btw you cannt use c# if you only use sharepoint designer, c# is with visual studio.

    to retrieve something from a list

    using Microsoft.SharePoint;  
    
    
    class SPTest {  
    
    public void ReadList() {  
    
    // Use using to make sure resources are released properly  
    using(SPSite oSite = new SPSite(pathToSite)) {  
    using(SPWeb oWeb = oSite.AllWebs[nameOfWeb]) {   
      // Alternately you can use oSite.RootWeb if you want to access the main site  
    
      SPList oList = oWeb.Lists[listName];  // The display name, ie. "Calendar"  
    
      foreach(SPListItem oItem in oList.Items) {  
        // Access each item in the list...  
        DateTime startTime = (DateTime)oItem["Start Time"];  
        // etc....  
      }  
    
      }  
      }  
      }  
      } 
    

    just to get you started, i would recomend youtube to see how you can use sharepoint and visual studio together also, or better follow Microsoft offical course 10175A (i did) codes above where puled from the internet not from the book.

    i dont think its possible to do this with normal workflows, or with javascript despite some java code is verry powerfull with ajax etc. So you should spend the time of figuring this out within visual studio. (c# is not that hard to read, take your time)

    oh i see your using 2007, its less friendly to use from a programming viewpoint, if this code doesnt work on it i think its still close the way it can be done, just check object and what you do with it, it might slightly differ.