Wednesday, August 3, 2011

Create Site Collection programmatically in Sharepoint 2010

try
            {
                sProjectListSite = ConfigurationManager.AppSettings["ProjectListSite"].ToString();
                sSAPResourceProviderURL = ConfigurationManager.AppSettings["SAPResourceProviderURL"].ToString();
                sDocumentUrl = ConfigurationManager.AppSettings["FinalDocumentURL"].ToString();
                sPrimaryAdmin = ConfigurationManager.AppSettings["SiteCollectionAdminPrime"].ToString();
                sSecondaryAdmin = ConfigurationManager.AppSettings["SiteCollectionAdminSecond"].ToString();
                sPublishedUrl = ConfigurationManager.AppSettings["PublishedDocURL"].ToString();
                sDisclosedUrl = ConfigurationManager.AppSettings["DisclosedDocURL"].ToString();

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite siteCollectionOuter = new SPSite(sProjectListSite))
                    {
                        string paramProjID = string.Empty;
                        if (Request.QueryString["ProjID"] != null)
                        {
                            paramProjID = Request.QueryString["ProjID"].ToString();
                            SPWebApplication webApp = siteCollectionOuter.WebApplication;

                            //SPGlobalAdmin globalAdmin = new SPGlobalAdmin();
                            //Context.Items[SPGlobalAdmin.RequestFromAdminPort] = true;
                            //Page.RegisterHiddenField("__REQUESTDIGEST", globalAdmin.AdminFormDigest);

                            oTeraXML = new XmlDocument();
                            XmlTextReader reader = new XmlTextReader(sSAPResourceProviderURL + paramProjID);
                            oTeraXML.Load(reader);
                            // Read the XML elements

                            XmlNode root = oTeraXML.DocumentElement;
                            if (root.SelectSingleNode("//project") != null)
                            {
                                XmlNode keywordsNode = root.SelectSingleNode("//project");
                                sProjectID = keywordsNode.Attributes["id"].Value;

                            }

                            if (root.SelectSingleNode("//title") != null)
                            {
                                XmlNode keywordsNode = root.SelectSingleNode("//title");
                                sProjectTitle = keywordsNode.InnerText;
                            }

                            try
                            {
                                if (root.SelectSingleNode("//siteowner") != null)
                                {
                                    XmlNode keywordsNode = root.SelectSingleNode("//siteowner");
                                    sProjectOwner = keywordsNode.InnerText;
                                }
                            }
                            catch (Exception ex)
                            {
                                Response.Write("Site owner does not exists");
                            }

                            SPSiteSubscription subscription = SPSiteSubscription.Create();
                            // Remember this value read from web.config file
                            //SPUserInfo usrPrimaryAdmin = new SPUserInfo();
                            //usrPrimaryAdmin.LoginName = PrimaryAdmin;    

                            SPUser usrPrimaryAdmin = siteCollectionOuter.OpenWeb().EnsureUser(sPrimaryAdmin);
                            SPUser usrSecondaryAdmin = siteCollectionOuter.OpenWeb().EnsureUser(sSecondaryAdmin);
                            SPUser usrAuthenticatedUser = siteCollectionOuter.OpenWeb().EnsureUser(@"NT AUTHORITY\authenticated users");
                            //site = webApp.Sites.Add(subscription, "sites/" + projectID, projectTitle + "-" + projectID, "", 1033, "OPSTeamDefinition#0", "wb\\sp_farm", "SP farm", "nsreegadde@worldbank.org", "", "", "", false); // exception                                                                                                                                                                                              
                            oSite = webApp.Sites.Add(subscription, "sites/" + sProjectID, sProjectTitle + "-" + sProjectID, "", 1033, "OPSTeamDefinition#0", usrPrimaryAdmin.LoginName, usrPrimaryAdmin.Name, usrPrimaryAdmin.Email, usrSecondaryAdmin.LoginName, usrSecondaryAdmin.Name, usrSecondaryAdmin.Email, false); // exception                                                                                                                                                                                              
                            oSite.AdministrationSiteType = SPAdministrationSiteType.TenantAdministration;
                            SPWeb parentWeb = oSite.OpenWeb();

                            using (SPWeb oWebsite = oSite.OpenWeb(parentWeb.ID))
                            {
                                parentWeb.AllowUnsafeUpdates = true;
                                string groupName1 = sProjectTitle + "-" + sProjectID + " Owners";
                                string groupName2 = sProjectTitle + "-" + sProjectID + " Members";
                                string groupName3 = sProjectTitle + "-" + sProjectID + " Visitors";
                                try
                                {
                                    if (sProjectOwner != string.Empty)
                                    {
                                        parentWeb.SiteUsers.Add("wb\\" + sProjectOwner, "", "", "");
                                        SPUser ownerUser = parentWeb.SiteUsers["wb\\" + sProjectOwner];

                                        SPGroup OwnersGroup = parentWeb.Groups[groupName1];
                                        SPGroup MembersGroup = parentWeb.Groups[groupName2];
                                        SPGroup VisitorsGroup = parentWeb.Groups[groupName3];

                                        OwnersGroup.AddUser(ownerUser);
                                        VisitorsGroup.AddUser(usrAuthenticatedUser);

                                        if (root.SelectSingleNode("//teammembers") != null)
                                        {
                                            XmlNode teamMembersNode = root.SelectSingleNode("//teammembers");

                                            if (teamMembersNode.ChildNodes.Count > 0)
                                            {
                                                foreach (XmlNode node in teamMembersNode.ChildNodes)
                                                {
                                                    parentWeb.SiteUsers.Add("wb\\" + node.InnerText, "", "", "");
                                                    SPUser memberUser = parentWeb.SiteUsers["wb\\" + node.InnerText];
                                                    MembersGroup.AddUser(memberUser);
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Response.Write("Issue in adding users into Group" + ex.Message);
                                }
                            }

                            parentWeb.AllowUnsafeUpdates = false;
                            // Add the blog site
                            SPWebCollection subSiteCollection = parentWeb.Webs;
                            SPWeb newblogSite = subSiteCollection.Add("TeamBlog", "TeamBlog -" + sProjectID, "", 1033, "OPSTeamBlog#0", false, false);

                            parentWeb.AllowUnsafeUpdates = false;

                            lblSuccessMsg.InnerHtml = "<h4>Collaboration Workspace successfully created</h4><br>";
                            lblSuccessMsg.InnerHtml += "<h4><A href='" + parentWeb.Url.ToString() + "'>Click here to access the site </A>";
                            lblSuccessMsg.Style.Add("color", "Green");
                        }
                        else
                        {
                            lblSuccessMsg.InnerHtml = "<h4>Unable to create workspace since invalid ProjectID</h4>";
                            lblSuccessMsg.Style.Add("color", "Red");
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Response.Write(ex.StackTrace.ToString());
            }
        }

1 comment:

  1. Hi Surya,

    we would like create site colletions automatically once user filled form

    Environement details

    we have two environments

    WSS and sharepoint 2010

    WSS does not need any approval

    sp2010 required approval

    Here is the requirement:

    once user submitted request,user has to receive mail to fill the form with URL,site template,

    site owners details

    once filled the form,it should check for the environment details(WSS,sp2010).If its wss then,
    site colletion has to create automatically and send site details to site admin over mail

    if it is Sp2010,the form should go for approval to approvers,if it approved,it should create site
    colletion automatically,if rejected,user should get mail to explanation of rejection

    I am new to development.I am very much thank ful to provide any scripts.

    Please provide your mail ID to contact

    Regards,
    Ibraheem

    ReplyDelete

Followers

Blog Archive