【问题标题】:Create a new site programmatically and select parent site? (SharePoint)以编程方式创建一个新站点并选择父站点? (共享点)
【发布时间】:2011-07-26 15:38:01
【问题描述】:

我正在使用以下代码创建一个新站点:

newWeb = SPContext.GetContext(HttpContext.Current).Web.Webs.Add(newSiteUrl, newSiteName, null, (uint)1033, siteTemplate, true, false);

                try
                {
                    newWeb.Update();
                }

NewSiteUrl 和 newSiteName 是来自两个文本框的值,在我使用此代码(在 Web 部件中)的任何站点上,新站点都将成为该站点的子站点。

我现在希望能够选择父网站,以便新网站可以位于网站集中的任何位置,而不仅仅是作为我使用 Web 部件的网站的子网站。

我创建了以下函数来获取网站集中的所有网站,并使用每个网站的名称和网址填充下拉列表

private void getSites()
{
    SPSite oSiteCollection = SPContext.Current.Site;
    SPWebCollection collWebsite = oSiteCollection.AllWebs;

    for (int i = 0; i < collWebsite.Count; i++)
    {
        ddlParentSite.Items.Add(new ListItem(collWebsite[i].Title, collWebsite[i].Url));
    }
    oSiteCollection.Dispose();
}

如果用户在下拉列表中选择了一个站点,是否可以在 newSiteUrl 中使用该 URL 来决定新站点的位置?我没有让它真正工作,新站点仍然成为当前站点的子站点。我想这与 HttpContext.Current 有关吗?关于我应该如何做的任何想法?

这是我第一次编写自定义 Web 部件,sharepoint 对象模型目前有点压倒性。

提前致谢。

编辑:更新代码 给出错误:Trying to use an SPWeb object that has been closed or disposed and is no longer valid.

if (!siteExists(newSiteName) && newSiteName.Length > 0)
            {
                using (var parent = SPContext.GetContext(HttpContext.Current).Site)
                { 
                    using(var parentWeb = parent.OpenWeb(new Guid(parentSite)))
                    {
                    newWeb = parentWeb.Webs.Add(newSiteUrl, newSiteName, null, (uint)1033, siteTemplate, true, false);

                try
                {
                    newWeb.Update();
                }

                catch
                {
                    lblErrorCreateSite.Text = "An error occured when trying to create a new site, please try again.";
                }
                finally
                {

                    txtSiteName.Text = "";

                    // Show link to new site
                    lblNewSite.Text = "A new site was successfully created at ";
                    hplNewSite.Visible = true;
                    hplNewSite.NavigateUrl = siteURL() + newSiteName;
                    hplNewSite.Text = newSiteName;

                    // Dispose to reload the SharePoint content database
                    newWeb.Dispose();

                }

                // Set permissions
                try
                {
                    string site = siteURL();
                    SPSite spSite = new SPSite(site + newSiteName);
                    SPWeb web = spSite.OpenWeb();

                    // Assign Full Access role to the selected groups
                    string fullAccessGroup = null;
                    string fullAccessRole = null;
                    foreach (ListItem item in lbFullAccess.Items)
                    {
                        fullAccessGroup = item.Value;
                        fullAccessRole = "Full Control";
                        SPRoleAssignment roleAssignment = new SPRoleAssignment(web.SiteGroups[fullAccessGroup]);
                        SPRoleDefinitionBindingCollection roleDefinition = roleAssignment.RoleDefinitionBindings;

                        roleDefinition.Add(web.RoleDefinitions[fullAccessRole]);
                        web.RoleAssignments.Add(roleAssignment);
                        web.Properties[fullAccessGroup] = fullAccessRole;
                        web.Properties.Update();
                    }

                    // Assign Contributor role to the selected groups
                    string contributeGroup = null;
                    string contributeRole = null;
                    foreach (ListItem item in lbContributor.Items)
                    {
                        contributeGroup = item.Value.ToString();
                        contributeRole = "Contribute";

                        SPRoleAssignment roleAssignment = new SPRoleAssignment(web.SiteGroups[contributeGroup]);
                        SPRoleDefinitionBindingCollection roleDefinition = roleAssignment.RoleDefinitionBindings;

                        roleDefinition.Add(web.RoleDefinitions[contributeRole]);
                        web.RoleAssignments.Add(roleAssignment);
                        web.Properties[contributeGroup] = contributeRole;
                        web.Properties.Update();
                    }

                    // Assign Reader role to the selected groups
                    string readerGroup = null;
                    string readerRole = null;
                    foreach (ListItem item in lbReadOnly.Items)
                    {
                        readerGroup = item.Value.ToString();
                        readerRole = "Read";

                        SPRoleAssignment roleAssignment = new SPRoleAssignment(web.SiteGroups[readerGroup]);
                        SPRoleDefinitionBindingCollection roleDefinition = roleAssignment.RoleDefinitionBindings;

                        roleDefinition.Add(web.RoleDefinitions[readerRole]);
                        web.RoleAssignments.Add(roleAssignment);
                        web.Properties[readerGroup] = readerRole;
                        web.Properties.Update();
                    }

                }
                catch
                {
                    lblErrorSetPermissions.Text = "Error trying to set permissions for this site, please try again.";
                }
                finally
                {

                }

            }
                }
            }
            else
            {
                if (siteExists(newSiteName))
                {
                    lblErrorCreateSite.Text = "A site with that name already exists. Please select another name.<br/>";
                }

                if (newSiteName.Length == 0)
                {
                    lblErrorCreateSite.Text = "A Site Name is required.<br/>";
                }

                hplNewSite.Visible = false;
            }

编辑2: 所以我用

SPSite currentContext = SPContext.GetContext(HttpContext.Current).Site;
SPWeb parentID = currentContext.OpenWeb(new Guid(parentSiteValue));

newWeb = parentID.Webs.Add(newSiteUrl, newSiteName, null, (uint)1033, siteTemplate, true, false);

但是我怎样才能最容易地获取新创建站点的 url(在我创建的链接中显示正确的 url 并在我设置权限时使用)?

【问题讨论】:

    标签: c# sharepoint


    【解决方案1】:

    您应该打开在下拉列表中选择的 SPWeb。使用 web id 作为下拉值,获取上下文 spsite,然后使用 SPSite.OpenWeb(GUID) 打开您想要成为父级的 web。将新网站添加到该网站的网站:

    private void getSites()
    {
       SPSite oSiteCollection = SPContext.Current.Site;
       SPWebCollection collWebsite = oSiteCollection.AllWebs;
          for (int i = 0; i < collWebsite.Count; i++)
       {
           ddlParentSite.Items.Add(new ListItem(collWebsite[i].Title, collWebsite[i].Id));
       }
    
       // oSiteCollection.Dispose(); // NEVER DISPOSE THE SPContext.Current.Site or Web
    }
    
    using( var site = SPContext.GetContext(HttpContext.Current).Site)
    {
      using(var parentWeb = site.OpenWeb(new Guid(DDLVALUE))
      {
        newWeb = parentWeb.Webs.Add(newSiteUrl, newSiteName, null, (uint)1033, siteTemplate, true, false);
        try
        {
          newWeb.Update();
        }
      }
    }
    

    【讨论】:

    • 您好科林,感谢您的意见。我用更多代码更新了我的帖子(我还在创建站点时设置了权限),它可能不是最漂亮的代码,但在我尝试设置父站点之前它就起作用了。现在我收到错误“尝试使用已关闭或处置且不再有效的 SPWeb 对象。”关于如何更改代码的任何想法?再次感谢
    【解决方案2】:
    using( var site = SPContext.GetContext(HttpContext.Current).Site)
    {
    }
    

    当您处理从 SPContext 获得的站点对象时,可能会导致该问题。

    并且微软表示不要处置上下文网络。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 2010-12-13
      相关资源
      最近更新 更多