Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, November 28, 2011

Code for creating a Subsite Dynamically using SharePoint Object Model


using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Security;
using System.Data;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Security.Principal;



[assembly: AllowPartiallyTrustedCallers]

namespace PMTCreateProject
{
    [Guid("c365d30c-c203-467a-bb12-fa29c5aa9b97")]
    public class PMTCreateProject : System.Web.UI.WebControls.WebParts.WebPart
    {

        Label lblMessage, lblNew;
        TextBox txtProject, txtDescription, txtEstimatedEffort;
        RequiredFieldValidator rfvProject, rfvStatus, rfvEstimatedEffort, rfvProjectType;
        // rfvAssignedTo,rfvActStartDate, rfvActEndDate, rfvDescription
        PeopleEditor peAssignedTo;
        DateTimeControl dtActStartDate, dtActEndDate, dtExpStartDate, dtExpEndDate;
        DropDownList ddlStatus, ddlProjectType;// ddlOppurtunities;
        Button btnCreate;
        StringCollection strStatus = new StringCollection();
        StringCollection strOppur = new StringCollection();
        StringCollection strProjectType = new StringCollection();
        string strActStartDate = "", strActEndDate = "",strExpEndDate = "",strExpStartDate = "", strProjectName = "", siteUrl = "";
        HyperLink hlink;

        protected override void Render(HtmlTextWriter writer)
        {
            // TODO: add custom rendering code here.
            // writer.Write("Output HTML");

            try
            {

                writer.Write("<style>");
                writer.Write("input.btn{");
                writer.Write("color:#000;");
                writer.Write("font-family:'trebuchet ms',helvetica,sans-serif;");
                writer.Write("font-size:100%;");
                writer.Write("font-weight:bold;");
                writer.Write("background-color:#fed;");
                writer.Write("border:1px solid #000;");
                writer.Write("filter:progid:DXImageTransform.Microsoft.Gradient");
                writer.Write("(GradientType=0,StartColorStr='#ffffffff',EndColorStr='#d9eaff');}");
                writer.Write("input.btnhov{");
                writer.Write(" border-top-color:#c63;");
                writer.Write("border-left-color:#c63;");
                writer.Write(" border-right-color:#930;");
                writer.Write("border-bottom-color:#930;}");
                writer.Write("</style>");

               

                writer.Write("<table width= \"85%\" >");
                //writer.Write("<tr><td colspan=2 align=center>");
                writer.Write("<tr><td colspan= 4><font color=blue>");
                lblMessage.RenderControl(writer);
                writer.Write("</font></td></tr>");
                writer.Write("<tr><td colspan= 4><font color=blue>");
                lblNew.RenderControl(writer);
                hlink.RenderControl(writer);
                writer.Write("</font></td></tr>");

                writer.Write("<tr><td colspan=2 width= \"13%\"></td><td align=left width= \"20%\">");
                writer.Write("&nbsp;<font color=\"red\">|</font>&nbsp;Mandatory Fields");
                writer.Write("</td></tr>");

                //writer.Write("<tr><td colspan= 4>");
                //writer.Write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
                //writer.Write("</td></tr>");

                writer.Write("<tr><td valign= \"top\" width= \"25%\"  ><b>Project</b></td><td valign=top align=\"right\" width= \"5%\">&nbsp;&nbsp;");
                writer.Write("<font color=\"red\">|</font>&nbsp;");
                writer.Write("</td><td align=left  width= \"25%\">");
                txtProject.RenderControl(writer);
                writer.Write("</td><td align=left  width= \"30%\">");
                rfvProject.RenderControl(writer);

                writer.Write("</td></tr><tr><td valign= \"top\" width= \"25%\" ><b>Project Type</b></td><td valign=top align=\"right\" width= \"5%\">&nbsp;&nbsp;");
                writer.Write("<font color=\"red\">|</font>&nbsp;");
                writer.Write("</td><td align=left  width= \"25%\">");
                ddlProjectType.RenderControl(writer);
                writer.Write("</td ><td align=left width= \"30%\" >");
                rfvProjectType.RenderControl(writer);
            
                writer.Write("<tr><td valign= \"top\" width= \"25%\"  ><b>Assigned To</b></td><td valign=top align=\"right\" width= \"5%\">&nbsp;&nbsp;");
                writer.Write("<font color=\"white\">|</font>&nbsp;");
                writer.Write("</td><td align=left  width= \"25%\">");
                peAssignedTo.RenderControl(writer);
                writer.Write("</td><td align=left  width= \"30%\">");
                //rfvAssignedTo.RenderControl(writer);

                writer.Write("<tr><td valign= \"top\" width= \"25%\"  ><b>Effort Hours</b></td><td valign=top align=\"right\" width= \"5%\">&nbsp;&nbsp;");
                writer.Write("<font color=\"red\">|</font>&nbsp;");
                writer.Write("</td><td align=left  width= \"25%\">");
                txtEstimatedEffort.RenderControl(writer);
                writer.Write("</td><td align=left  width= \"30%\">");
                rfvEstimatedEffort.RenderControl(writer);

                //writer.Write("<tr><td valign= \"top\" width= \"25%\"  ><b>Project Cost</b></td><td valign=top align=\"right\" width= \"5%\">&nbsp;&nbsp;");
                //writer.Write("<font color=\"red\">|</font>&nbsp;");
                //writer.Write("</td><td align=left  width= \"25%\">");
                //txtProjectCost.RenderControl(writer);
                //writer.Write("</td><td align=left  width= \"30%\">");

                writer.Write("</td></tr><tr><td valign= \"top\" width= \"25%\" ><b>Actual Start Date</b></td><td valign=top align=\"right\" width= \"5%\" >&nbsp;&nbsp;");
                writer.Write("<font color=\"red\"></font>&nbsp;");
                writer.Write("</td><td align=left  width= \"25%\">");
                dtActStartDate.RenderControl(writer);
                writer.Write("</td ><td align=left width= \"30%\" >");
                //rfvActStartDate.RenderControl(writer);

                writer.Write("</td></tr><tr><td valign= \"top\" width= \"25%\" ><b>Actual End Date</b></td><td valign=top align=\"right\" width= \"5%\" >&nbsp;&nbsp;");
                writer.Write("<font color=\"red\"></font>&nbsp;");
                writer.Write("</td><td align=left  width= \"25%\">");
                dtActEndDate.RenderControl(writer);
                writer.Write("</td ><td align=left width= \"30%\" >");
                //rfvActEndDate.RenderControl(writer);

                writer.Write("</td></tr><tr><td valign= \"top\" width= \"25%\" ><b>Estimated Start Date</b></td><td valign=top align=\"right\" width= \"5%\" >&nbsp;&nbsp;");
                writer.Write("<font color=\"red\"></font>&nbsp;");
                writer.Write("</td><td align=left  width= \"25%\">");
                dtExpStartDate.RenderControl(writer);
                writer.Write("</td ><td align=left width= \"30%\" >");
                //rfvActStartDate.RenderControl(writer);

                writer.Write("</td></tr><tr><td valign= \"top\" width= \"25%\" ><b>Estimated End Date</b></td><td valign=top align=\"right\" width= \"5%\" >&nbsp;&nbsp;");
                writer.Write("<font color=\"red\"></font>&nbsp;");
                writer.Write("</td><td align=left  width= \"25%\">");
                dtExpEndDate.RenderControl(writer);
                writer.Write("</td ><td align=left width= \"30%\" >");
                //rfvActEndDate.RenderControl(writer);

                writer.Write("</td></tr><tr><td valign= \"top\" width= \"25%\" ><b>Status</b></td><td valign=top align=\"right\" width= \"5%\">&nbsp;&nbsp;");
                writer.Write("<font color=\"red\">|</font>&nbsp;");
                writer.Write("</td><td align=left  width= \"25%\">");
                ddlStatus.RenderControl(writer);
                writer.Write("</td ><td align=left width= \"30%\" >");
                rfvStatus.RenderControl(writer);

                //writer.Write("</td></tr><tr><td valign= \"top\" width= \"25%\" ><b>Oppurtunities</b></td><td valign=top align=\"right\" width= \"5%\">&nbsp;&nbsp;");
                //writer.Write("<font color=\"red\">|</font>&nbsp;");
                //writer.Write("</td><td align=left  width= \"25%\">");
                //ddlOppurtunities.RenderControl(writer);
                //writer.Write("</td ><td align=left width= \"30%\" >");
                //rfvStatus.RenderControl(writer);

                writer.Write("</td></tr><tr><td valign= \"top\" width= \"25%\" ><b>Description</b></td><td valign=top align=\"right\" width= \"5%\">&nbsp;&nbsp;");
                writer.Write("<font color=\"white\">|</font>&nbsp;");
                writer.Write("</td><td align=left  width= \"25%\">");
                txtDescription.RenderControl(writer);
                writer.Write("</td ><td align=left width= \"30%\">");
                //rfvDescription.RenderControl(writer);

                writer.Write("</td></tr><tr><td colspan=4>");
                writer.Write("</td></tr><tr><td colspan=4>");
                writer.Write("</td><td align=left  width= \"25%\">");
                writer.Write("</td></tr><tr><td colspan=4>");
                btnCreate.RenderControl(writer);
                writer.Write("</td></tr></table>");

            }
            catch (Exception ex)
            {
                writer.Write("Error" + ex.ToString());

            }
        }

        protected override void CreateChildControls()
        {
            try
            {
                //initializing labels               

                lblMessage = new Label();
                lblMessage.ID = "lblMessage";
                this.Controls.Add(lblMessage);
                lblMessage.Text = " ";

                lblNew = new Label();
                lblNew.ID = "lblNew";
                this.Controls.Add(lblNew);
                lblNew.Text = " ";

                //initializing Textboxes

                txtDescription = new TextBox();
                txtDescription.ID = "txtDescription";
                txtDescription.TextMode = TextBoxMode.MultiLine;
                txtDescription.Width = 350;
                txtDescription.Height = 80;
                this.Controls.Add(txtDescription);

                txtProject = new TextBox();
                txtProject.ID = "txtProject";
                txtProject.Width = 350;
                this.Controls.Add(txtProject);

                txtEstimatedEffort = new TextBox();
                txtEstimatedEffort.ID = "txtEstimatedEffort";
                this.Controls.Add(txtEstimatedEffort);

                //txtProjectCost = new TextBox();
                //txtProjectCost.ID = "txtProjectCost";
                //this.Controls.Add(txtProjectCost);

                //initializing Dropdownlists


                ddlStatus = new DropDownList();
                ddlStatus.ID = "ddlStatus";
                SPWeb Objweb = SPControl.GetContextWeb(Context);
                SPListItemCollection Objlist = Objweb.Lists["Project Status"].Items;
                if (Objweb.Lists["Project Status"].ItemCount > 0)
                {
                    foreach (SPListItem li in Objlist)
                    {
                        strStatus.Add(li["Title"].ToString());
                    }
                }
                ddlStatus.DataSource = strStatus;
                ddlStatus.DataBind();
                ddlStatus.Items.Insert(0, "---Select---");
                this.Controls.Add(ddlStatus);

                ddlProjectType = new DropDownList();
                ddlProjectType.ID = "ddlProjectType";
                SPWeb Objweb1 = SPControl.GetContextWeb(Context);
                SPListItemCollection ObjType = Objweb1.Lists["ProjectType"].Items;
                if (Objweb1.Lists["ProjectType"].ItemCount > 0)
                {
                    foreach (SPListItem TypeItem in ObjType)
                    {
                        strProjectType.Add(TypeItem["Title"].ToString());
                    }
                }
                ddlProjectType.DataSource = strProjectType;
                ddlProjectType.DataBind();
                ddlProjectType.Items.Insert(0, "---Select---");
                this.Controls.Add(ddlProjectType);

                hlink = new HyperLink();
                hlink.Visible = false;
                hlink.Text = "Click Here to navigate.";
                this.Controls.Add(hlink);

                //ddlOppurtunities = new DropDownList();
                //ddlOppurtunities.ID = "ddlOppurtunities";

                //SPSite mySiteCollection = new SPSite("http://sps:116");
                //SPWeb SalesSite = mySiteCollection.AllWebs["Sales"];
                //SPWeb myRootSite = mySiteCollection.RootWeb;
                //SPWeb PmtSite = mySiteCollection.AllWebs["PMT"];

                //foreach (SPWeb subSite in SalesSite.Webs)
                //{
                //    if(subSite.WebTemplate == "ACCOUNTS")
                //    {
                //        SPWeb oppurSite = subSite.GetSubwebsForCurrentUser();
                //        SPListItemCollection oppurListItems = oppurSite.Lists["Oppurtunities"].Items;
                //        foreach (SPListItem opListItem in oppurListItems)
                //        {
                //            strOppur.Add(opListItem["Title"].ToString());
                //        }

                //    }

                //}
                //ddlOppurtunities.DataSource = strOppur;
                //ddlOppurtunities.DataBind();
                //ddlOppurtunities.Items.Insert(0, "---Select---");
                //this.Controls.Add(ddlOppurtunities);

                //initializing Buttons

                //btnCancel = new Button();
                //btnCancel.ID = "btnCancel";
                //btnCancel.Text = "Cancel";
                //btnCancel.CssClass = "btn";
                //btnCancel.Attributes.Add("onmouseover", "this.className='btn btnhov'");
                //btnCancel.Attributes.Add("onmouseout", "this.className='btn'");
                //btnCancel.Click += new EventHandler(btnCancel_Click);
                //this.Controls.Add(btnCancel);

                btnCreate = new Button();
                btnCreate.ID = "btnCreate";
                btnCreate.Text = "Create Project";
                btnCreate.CssClass = "btn";
                btnCreate.Attributes.Add("onmouseover", "this.className='btn btnhov'");
                btnCreate.Attributes.Add("onmouseout", "this.className='btn'");
                btnCreate.Click += new EventHandler(btnCreate_Click);
                this.Controls.Add(btnCreate);

                dtActStartDate = new DateTimeControl();
                dtActStartDate.DateOnly = true;
                dtActStartDate.SelectedDate = System.DateTime.Now;
                if (strActStartDate.Trim() != "")
                {
                    dtActStartDate.SelectedDate = Convert.ToDateTime(strActStartDate).Date;
                }
                this.Controls.Add(dtActStartDate);

                dtActEndDate = new DateTimeControl();
                dtActEndDate.DateOnly = true;
                dtActEndDate.SelectedDate = System.DateTime.Now;
                if (strActEndDate.Trim() != "")
                {
                    dtActEndDate.SelectedDate = Convert.ToDateTime(strActEndDate).Date;
                }
                this.Controls.Add(dtActEndDate);

                dtExpStartDate = new DateTimeControl();
                dtExpStartDate.DateOnly = true;
                dtExpStartDate.SelectedDate = System.DateTime.Now;
                if (strExpStartDate.Trim() != "")
                {
                    dtExpStartDate.SelectedDate = Convert.ToDateTime(strExpStartDate).Date;
                }
                this.Controls.Add(dtExpStartDate);

                dtExpEndDate = new DateTimeControl();
                dtExpEndDate.DateOnly = true;
                dtExpEndDate.SelectedDate = System.DateTime.Now;
                if (strExpEndDate.Trim() != "")
                {
                    dtExpEndDate.SelectedDate = Convert.ToDateTime(strExpEndDate).Date;
                }
                this.Controls.Add(dtExpEndDate);

                //Initializing RequiredFieldValidators

                //rfvAssignedTo = new RequiredFieldValidator();
                //rfvAssignedTo.ID = "rfvAssignedTo";
                //rfvAssignedTo.ControlToValidate = peAssignedTo.ID;
                //rfvAssignedTo.ErrorMessage = "Assigned To is required.";
                //this.Controls.Add(rfvAssignedTo);



                //rfvActEndDate = new RequiredFieldValidator();
                //rfvActEndDate.ID = "rfvActEndDate";
                //rfvActEndDate.ControlToValidate = dtActEndDate.ID;

                //if (dtActEndDate.IsDateEmpty == true)
                //{
                //dtActEndDate.ErrorMessage = "End Date is required.";
                //}
                //this.Controls.Add(rfvActEndDate);

                rfvProject = new RequiredFieldValidator();
                rfvProject.ID = "rfvProject";
                rfvProject.ControlToValidate = txtProject.ID;
                rfvProject.ErrorMessage = "Project is required.";
                this.Controls.Add(rfvProject);

                rfvProjectType = new RequiredFieldValidator();
                rfvProjectType.ID = "rfvProjectType";
                rfvProjectType.ControlToValidate = ddlProjectType.ID;
                rfvProjectType.InitialValue = "---Select---";
                rfvProjectType.ErrorMessage = "Project Type is required.";
                this.Controls.Add(rfvProjectType);

                rfvStatus = new RequiredFieldValidator();
                rfvStatus.ID = "rfvStatus";
                rfvStatus.ControlToValidate = ddlStatus.ID;
                rfvStatus.InitialValue = "---Select---";
                rfvStatus.ErrorMessage = "Status is required.";
                this.Controls.Add(rfvStatus);

                rfvEstimatedEffort = new RequiredFieldValidator();
                rfvEstimatedEffort.ID = "rfvEstimatedEffort";
                rfvEstimatedEffort.ControlToValidate = txtEstimatedEffort.ID;
                rfvEstimatedEffort.ErrorMessage = "Effort Hours are required.";
                this.Controls.Add(rfvEstimatedEffort);

                peAssignedTo = new PeopleEditor();
                peAssignedTo.ID = "peAssignedTo";
                peAssignedTo.MultiSelect = false;
                peAssignedTo.Width = 195;
                this.Controls.Add(peAssignedTo);



            }
            catch (Exception ex)
            {
                lblMessage.Text = "Error in Create child Control" + ex.ToString();
            }

        }

        void btnCreate_Click(object Sender, EventArgs e)
        {
            try
            {


                // Adding data to the "Projects" list of topsite
                strProjectName = txtProject.Text;

                SPWeb curWeb = SPControl.GetContextWeb(Context);
          
                SPList objList = curWeb.Lists["Projects"];
                SPListItem objItem = objList.Items.Add();
                SPListItemCollection StatusCollection = curWeb.Lists["Project Status"].Items;

                if (ddlStatus.SelectedIndex != 0)
                {
                  
                    foreach (SPListItem StatusItem in StatusCollection)
                    {
                        if (ddlStatus.SelectedItem.Text == StatusItem["Title"].ToString())
                        {
                            objItem["Status"] = StatusItem["ID"];
                            break;
                        }
                    }
                   
                }

                SPList PrjList = curWeb.Lists["Projects"];
                SPListItem PrjItem = PrjList.Items.Add();
                SPListItemCollection TypeCollection = curWeb.Lists["ProjectType"].Items;

                if (ddlProjectType.SelectedIndex != 0)
                {
                    if( curWeb.Lists["ProjectType"].ItemCount > 0)
                    {
                    foreach (SPListItem TypeItem in TypeCollection)
                    {
                        if (ddlProjectType.SelectedItem.Text == TypeItem["Title"].ToString())
                        {
                            objItem["ProjectType"] = TypeItem["ID"];
                            break;
                        }
                    }
                    }
                }

                //updating the project name in topsite

                if (txtProject.Text != "")
                {
                    objItem["Title"] = txtProject.Text;
                }

                //updating the Project Description in topsite

                if (txtDescription.Text != "")
                {
                    objItem["Description"] = txtDescription.Text;
                }

                if (txtEstimatedEffort.Text != "")
                {
                    objItem["EstimatedEffort"] = txtEstimatedEffort.Text;
                }

               

                //if (txtProjectCost.Text != "")
                //{
                //    objItem["ProjectCost"] = txtProjectCost.Text;
                //}

                //updating the Assigned To field in topsite
                              
                bool bAssignTo = false;
                foreach (SPGroup grp in curWeb.Groups)
                {
                    foreach (SPUser user in grp.Users)
                    {
                        if (peAssignedTo.Accounts[0].ToString() == user.LoginName)
                        {
                            objItem["AssignedTo"] = user;
                            bAssignTo = true;
                            break;

                        }
                    }
                    if (bAssignTo)
                    {
                        break;
                    }
                }


                //updating the Start Date and End Date of the Project in topsite

                objItem["ActualStartDate"] = dtActStartDate.SelectedDate.ToString();
                objItem["ActualEndDate"] = dtActEndDate.SelectedDate.ToString();
                objItem["ExpectedStartDate"] = dtExpStartDate.SelectedDate.ToString();
                objItem["ExpectedEndDate"] = dtExpEndDate.SelectedDate.ToString();
                objItem.Update();

                //Creating a subsite under the top site

                string siteTemplate = "ProjectTemplate.stp";
                string strSiteTitle = txtProject.Text.Trim();
                siteUrl = strProjectName;

                string strSiteDescription = txtDescription.Text.ToString();

                if (strSiteDescription.Length > 100)
                {
                    strSiteDescription = strSiteDescription.Remove(101);
                }

                curWeb.Webs.Add(strProjectName, strSiteTitle, strSiteDescription, 1033, siteTemplate, true, false);

                // Adding data to the "Projects" list of subsite      

                SPWeb programweb = curWeb.Webs[strProjectName];

                SPRoleAssignmentCollection rolAssignCol = curWeb.RoleAssignments;

                foreach (SPRoleAssignment objrol in rolAssignCol)
                {
                    if (objrol.Member.Name == "PMO" || objrol.Member.Name == "Administrator")
                    {
                        programweb.RoleAssignments.Add(objrol);
                        break;
                    }
                }
                //Adding data to the "Projects" list of subsite      

                SPList subList = programweb.Lists["Projects"];
                SPListItem subListItem = subList.Items.Add();

                //updating the project name in subsite

                if (txtProject.Text != "")
                {
                    subListItem["Title"] = txtProject.Text;
                }

                // updating the status in subsite

                SPListItemCollection subStatuscol = programweb.Lists["Project Status"].Items;

                if (ddlStatus.SelectedIndex != 0)
                {
                    if (programweb.Lists["Project Status"].ItemCount > 0)
                    {
                    foreach (SPListItem substatusItem in subStatuscol)
                    {
                        if (ddlStatus.SelectedItem.Text == substatusItem["Title"].ToString())
                        {
                            subListItem["Status"] = substatusItem["ID"];
                            break;
                        }
                    }
                    }
                }

                SPList subPrjList = programweb.Lists["Projects"];
                SPListItem subPrjItem = subPrjList.Items.Add();
                SPListItemCollection subTypeCollection = programweb.Lists["ProjectType"].Items;

                if (ddlProjectType.SelectedIndex != 0)
                {
                    if (programweb.Lists["ProjectType"].ItemCount > 0)
                    {
                    foreach (SPListItem subTypeItem in subTypeCollection)
                    {
                        if (ddlProjectType.SelectedItem.Text == subTypeItem["Title"].ToString())
                        {
                            subListItem["ProjectType"] = subTypeItem["ID"];
                            break;
                        }
                    }
                    }
                }

                //updating the project Description in subsite

                if (txtDescription.Text != "")
                {
                    subListItem["Description"] = txtDescription.Text;
                }

                if (txtEstimatedEffort.Text != "")
                {
                    subListItem["EstimatedEffort"] = txtEstimatedEffort.Text;
                }

                //if (txtProjectCost.Text != "")
                //{
                //    subListItem["ProjectCost"] = txtProjectCost.Text;
                //}

                //updating the Start Date and End Date of the Project in subsite


                subListItem["ActualStartDate"] = dtActStartDate.SelectedDate.ToString();
                subListItem["ActualEndDate"] = dtActEndDate.SelectedDate.ToString();
                subListItem["ExpectedStartDate"] = dtExpStartDate.SelectedDate.ToString();
                subListItem["ExpectedEndDate"] = dtExpEndDate.SelectedDate.ToString();

                //updating the "Assigned To" field in subsite


                bool subAssignTo = false;
                foreach (SPGroup grp1 in curWeb.Groups)
                {
                    foreach (SPUser user1 in grp1.Users)
                    {
                        if (peAssignedTo.Accounts[0].ToString() == user1.LoginName)
                        {
                            subListItem["AssignedTo"] = user1;
                            subAssignTo = true;
                            break;
                        }

                    }
                    if (subAssignTo)
                    {
                        break;
                    }

                }

                subListItem.Update();

                //Creating Groups in the subsite

                curWeb.SiteGroups.Add(strProjectName + " PM", curWeb.SiteGroups["PMO"], curWeb.CurrentUser, strProjectName + " PM");
                SPPrincipal PMPrincipal = curWeb.SiteGroups[strProjectName + " PM"];

                curWeb.SiteGroups.Add(strProjectName + " Department Head", curWeb.SiteGroups["PMO"], curWeb.CurrentUser, strProjectName + " Department Head");
                SPPrincipal DepartmentHeadPrincipal = curWeb.SiteGroups[strProjectName + " Department Head"];

                curWeb.SiteGroups.Add(strProjectName + " Production Personnel", curWeb.SiteGroups["PMO"], curWeb.CurrentUser, strProjectName + " Production Personnel");
                SPPrincipal ProductionPersonnelPrincipal = curWeb.SiteGroups[strProjectName + " Production Personnel"];

                curWeb.SiteGroups.Add(strProjectName + " Client", curWeb.SiteGroups["PMO"], curWeb.CurrentUser, strProjectName + " Client");
                SPPrincipal ClientPrincipal = curWeb.SiteGroups[strProjectName + " Client"];

                curWeb.SiteGroups.Add(strProjectName + " Sales Executive", curWeb.SiteGroups["PMO"], curWeb.CurrentUser, strProjectName + " Sales Executive");
                SPPrincipal SalesExecutivePrincipal = curWeb.SiteGroups[strProjectName + " Sales Executive"];

                SPRoleDefinitionCollection roleDefinitions = programweb.RoleDefinitions;
                SPRoleAssignmentCollection roleAssignments = programweb.RoleAssignments;

                SPRoleAssignment PMroleAssignment = new SPRoleAssignment(PMPrincipal);
                SPRoleDefinitionBindingCollection PMroleDefBindings = PMroleAssignment.RoleDefinitionBindings;

                SPRoleAssignment DepartmentHeadroleAssignment = new SPRoleAssignment(DepartmentHeadPrincipal);
                SPRoleDefinitionBindingCollection DepartmentHeadroleDefBindings = DepartmentHeadroleAssignment.RoleDefinitionBindings;

                SPRoleAssignment ProductionPersonnelroleAssignment = new SPRoleAssignment(ProductionPersonnelPrincipal);
                SPRoleDefinitionBindingCollection ProductionPersonnelroleDefBindings = ProductionPersonnelroleAssignment.RoleDefinitionBindings;

                SPRoleAssignment ClientroleAssignment = new SPRoleAssignment(ClientPrincipal);
                SPRoleDefinitionBindingCollection ClientroleDefBindings = ClientroleAssignment.RoleDefinitionBindings;

                SPRoleAssignment SalesExecutiveroleAssignment = new SPRoleAssignment(SalesExecutivePrincipal);
                SPRoleDefinitionBindingCollection SalesExecutiveroleDefBindings = SalesExecutiveroleAssignment.RoleDefinitionBindings;

                PMroleDefBindings.Add(roleDefinitions["Full Control"]);

                DepartmentHeadroleDefBindings.Add(roleDefinitions["Contribute"]);

                ProductionPersonnelroleDefBindings.Add(roleDefinitions["Contribute"]);

                ClientroleDefBindings.Add(roleDefinitions["Contribute"]);

                SalesExecutiveroleDefBindings.Add(roleDefinitions["Contribute"]);

                roleAssignments.Add(PMroleAssignment);
                roleAssignments.Add(DepartmentHeadroleAssignment);
                roleAssignments.Add(ProductionPersonnelroleAssignment);
                roleAssignments.Add(ClientroleAssignment);
                roleAssignments.Add(SalesExecutiveroleAssignment);

                //Add user in the assigned to field to PM group in top site and subsite.
                System.Collections.ArrayList users = peAssignedTo.Accounts;
                for (int i = 0; i < users.Count; i++)
                {

                    curWeb.Groups["PM"].AddUser(users[i].ToString(), "", "", "");
                    programweb.Groups[strProjectName + " PM"].AddUser(users[i].ToString(), "", "", "");

                }

                lblMessage.Visible = true;
                lblMessage.Text = "Successfully Created Project ' " + siteUrl + " '.";

                lblNew.Visible = true;
                hlink.Visible = true;
                string url1 = programweb.Url;
                hlink.NavigateUrl = url1;
                Clear();
               
            }
           

            catch (Exception ex)
            {
                lblMessage.Text = "Error in btnCreate_Click event" + ex.ToString();

            }
        }

        public void Clear()
        {
            txtProject.Text = "";
            txtDescription.Text = " ";
            lblNew.Text = "";
            ddlStatus.SelectedIndex = 0;


        }

    }
}

No comments:

Post a Comment