Monday, February 25, 2013

Custom Site Provisioning - SP2013 way....

Before I move on to the  content for my blog today .. here's some importnant information..SP2013 preview is ending soon to give way to the live version of SP2013 on the cloud ( o365)
 
O365 launch event info below. Please Sign up to see the new stuff.

Virtual Launch Event on February 27th 2013 as Microsoft celebrates the availability of a major new release coming to Office 365 for businesses.  Demo of new features in enterprise social and show how this has transformed the full Office experience you know into an always up-to-date service. Some real-world stories from customers about their move to the cloud. 
 
Now coming back to code....
 
Recently I had a requirement to do some feasibility study for SP2013 for sandboxed solutions. Well as we all know Microsoft has deprecated sandboxed solutions in o365 2013
This means that the functionality will still be available for use in SharePoint 2013, but primarily for backwards compatibility with existing solutions built using the sandbox solution approach.
 
Microsoft's guidance is that if you are building new applications that in the past you would have deployed as sandbox solutions, you should use the new App model for those solutions instead of using sandbox solutions.
 
More info on an article here...
 
Some SharePoint components are used in end-user scenarios but do not have any equivalents in the SharePoint app model. For these you must use farm solutions.

Well the requirement in this case is to "Create a custom user interface to provision a sub site based off a custom template, add some details to the site and add users"

Well - I plan to try this as an app too.. but from the looks of it if I want to create a custom web Template this is not supported. Hence, my first reaction is to create a Sandbox solution.

Upload a custom SP 2013 Site template in the solutions gallery!

 
 
 
 
 


CODE :
 using System;  
 using System.ComponentModel;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 using System.Web.UI.WebControls.WebParts;  
 using Microsoft.SharePoint;  
 using Microsoft.SharePoint.WebControls;  
 using Microsoft.SharePoint.Client;  
 using System.Collections.Generic;  
 using Microsoft.SharePoint.Navigation;  
 namespace SharePointProject1.CreateSubSite  
 {  
   [ToolboxItem(false)]  
   public partial class CreateSubSite : System.Web.UI.WebControls.WebParts.WebPart  
   {  
     string AccountGroup = "";  
     protected override void OnInit(EventArgs e)  
     {  
       base.OnInit(e);  
       InitializeControl();  
     }  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       AccountGroup = txturl.Text + " Account Group - Contributor";  
       // Create more gropups based on requirements  
     }  
     protected string GetCustomTemplate(SPWeb web)  
     {  
       SPWebTemplate SiteTemplate = null;  
       // Find correct template  
       foreach (SPWebTemplate wt in web.GetAvailableWebTemplates((uint)1033)) // for EN-US use LCID 1033, http://msdn.microsoft.com/en-us/goglobal/bb964664  
       {  
         if (wt.Title == "AccountSiteTemplate")  
         {  
           SiteTemplate = wt;  
           break;  
         }  
       }  
       return SiteTemplate.Title;  
     }  
     protected SPWebTemplate GetCustomSiteTemplate(SPWeb web)  
     {  
       SPWebTemplate SiteTemplate = null;  
       // Find correct template  
       foreach (SPWebTemplate wt in web.GetAvailableWebTemplates((uint)1033))  
       {  
         if (wt.Title == "AccountSiteTemplate")  
         {  
           SiteTemplate = wt;  
           break;  
         }  
       }  
       return SiteTemplate;  
     }  
     private void CreateSPGroup(SPWeb newWeb)  
     {  
      //  AccountGroup = txturl.Text + " Account Group - Contributor";  
       SPGroup groupContributor = newWeb.SiteGroups[AccountGroup];  
       SPRoleDefinition roleDefinitionContributor = newWeb.RoleDefinitions.GetByType(SPRoleType.Contributor);  
       SPRoleAssignment roleAssigmentContributor = new SPRoleAssignment(groupContributor);  
       roleAssigmentContributor.RoleDefinitionBindings.Add(roleDefinitionContributor);  
       newWeb.RoleAssignments.Add(roleAssigmentContributor);  
       lblStatus.Text += "</br>Subsite Groups successfully created!!</br> ";  
     }  
     private void AddUsersToGroups(SPWeb newWeb)  
     {  
      //  AccountGroup = txturl.Text + " Account Group - Contributor";  
       SPUser spUser = newWeb.EnsureUser(txtContributor.Text);  
       if (spUser != null)  
       {  
         SPGroup spGroup = newWeb.Groups[AccountGroup];  
         if (spGroup != null)  
           spGroup.AddUser(spUser);  
       }  
       txtSiteDesc.Text = "";  
       txtSiteName.Text = "";  
       txturl.Text = "";  
       lblStatus.Text += "</br>User Successfully added to the group! ";  
       lblStatus.ForeColor = System.Drawing.Color.Green;  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       using (SPSite site = new SPSite(SPContext.Current.Web.Url))  
       {  
         using (SPWeb web = site.OpenWeb())  
         {  
           if (string.IsNullOrEmpty(txturl.Text))  
             txturl.Text = "Demo";  
            if (string.IsNullOrEmpty(txtSiteName.Text))  
             txtSiteName.Text = "Demo";  
            try  
            {  
              web.AllowUnsafeUpdates = true;  
              web.SiteGroups.Add(AccountGroup, web.CurrentUser, web.CurrentUser, string.Empty);  
              SPWeb newWeb = site.AllWebs.Add(txturl.Text, txtSiteName.Text, txtSiteDesc.Text, 1033, GetCustomSiteTemplate(web), true, false);  
              newWeb.Description = " New Account site created!!";  
              lblStatus.Text = "Subsite successfully created!! " + "<a href ='" + newWeb.Url.ToString() + "'> New Subsite Link </a> ";  
              CreateSPGroup(newWeb);  
              AddUsersToGroups(newWeb);  
              newWeb.Dispose();  
              web.Dispose();  
            }  
            catch (Exception ex)  
            {  
              lblStatus.Text = "Exception Occured! Error message : " + ex.Message.ToString();  
              lblStatus.ForeColor = System.Drawing.Color.Red;  
            }  
           finally  
           {  
             web.AllowUnsafeUpdates = false;  
           }  
         }  
       }  
     }  
     protected void txtTemplate_Click(object sender, EventArgs e)  
     {  
       using (SPSite site = new SPSite(SPContext.Current.Web.Url))  
       {  
         using (SPWeb web = site.OpenWeb())  
         {  
           txtSubsiteTemplate.Text = GetCustomTemplate(web);  
         }  
       }  
     }  
   }  
 }  


Upload the sandbox soultion to the solutions gallery

 
 


 



Add the web part top a page and start provisioning sites your way in SP2013...!!

Note:
In case you are thinking about the cool User Interface... This is because I have chosen a Theme - Orbit.. This is super simple and easy...
Please go through my blog to see how this can be done!



 
 

No comments:

Post a Comment