sharepoint 2013 根据网站模版创建网站,并赋值网站权限 create a site by custom site template

通过程序,根据网站模版,创建新的网站

private void CreateSiteBySiteTemplate()

        {

            SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                SPSite site =new SPSite("http://moss:5001/");

                SPWeb web =site.OpenWeb();

                web.AllowUnsafeUpdates = true;

                string WebName ="subsite1";//创建后的站点是:http://moss:5001/subsite1

                if(!web.Webs[WebName].Exists)

                {

                    //1.根据网站模版,创建网站

                    string TemplateName ="SubSiteTemplate";//网站模版定义

                    SPWebTemplate CustomTemplate =null;

                    SPWebTemplateCollection subWebTemplate =web.GetAvailableWebTemplates((uint)web.Locale.LCID);

                    foreach (SPWebTemplate template in subWebTemplate)

                    {

                       if (template.Title ==TemplateName)

                       {

                           CustomTemplate =template;

                           break;

                       }

                    }

                    SPWeb NewWeb =web.Webs.Add(WebName, WebName,string.Empty, (uint)2052, CustomTemplate,true,false);

                    NewWeb.AllowUnsafeUpdates =true;

                    //2.给创建好的网站分配权限,

                    //2.1断开网站权限

                   NewWeb.BreakRoleInheritance(false);

                    //2.2添加网站的"完全控制"权限.

                    SPUser user =web.EnsureUser(@"kw\cxx");

                    SPRoleAssignment MyRoleAssignment =new SPRoleAssignment(user);

                   MyRoleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions.GetByType(SPRoleType.Administrator));

                   NewWeb.RoleAssignments.Add(MyRoleAssignment);

                    NewWeb.Update();

                }

                web.Update();

                web.Dispose();

 

            });

        }

 

你可能感兴趣的:(SharePoint)