将功能部署到单个共享点站点/如何选择单个共享点站点

时间:2009-10-28 14:40:07

标签: sharepoint deployment install

我正在尝试部署一系列功能,为此我需要选择目标网站,然后使用:

objWeb.Features.Add(new Guid({guid of feature}));

我的问题是如何选择此网站,我发现的所有帮助都是使用其构造函数创建网站,然后跟踪它,我想在哪里打开现有网站。

谢谢。

2 个答案:

答案 0 :(得分:2)

这取决于您要执行代码的位置。如果您有一个sharepoint上下文,那么您可以使用

SPWeb oWebsite = SPContext.Current.Web;
oWebsite.Features.Add(new Guid({guid of feature}));

using(SPWeb oWebsite = SPContext.Current.Site.OpenWeb("Website_URL"))
{
    oWebsite.Features.Add(new Guid({guid of feature}));
}

例如,如果您使用的是控制台应用程序,并且没有SPContext,则可以使用

using(SPSite oSiteCollection = new SPSite("http://Server_Name"))
{
    using(SPWeb oWebsite = oSiteCollection.OpenWeb("Website_URL"))
    {
        oWebsite.Features.Add(new Guid({guid of feature}));
    }
}

有许多其他方法可以获取SPWeb对象,但这取决于您对该网站的信息(名称,网址,层次结构中的位置)

如果要激活针对网站集或Web应用程序作用域的功能,则可以以类似方式获取SPSite或SPWebApplication。

的SPSite:

SPContext.Current.Site

SPSite oSiteCollection = new SPSite("Absolute_URL")

SPWebApplication:

SPContext.Current.Site.WebApplication

SPWebApplication.Lookup(new Uri("http://MyServer:989"));

并且在这些对象中的任何一个上,您都可以调用

object.Features.Add(...))

与上述代码相同。

注意:功能的范围在feature.xml中指定,有关详细信息,请参阅以下内容: http://msdn.microsoft.com/en-us/library/ms436075.aspx

答案 1 :(得分:0)

对于Web范围的功能,请使用:

using (SPWeb currentSite = SPContext.Current.Web)
{
  currentSite.WebFeatures.Add(new Guid("{GUID}"));
}

对于网站范围的功能,请使用:

using (SPWeb currentSite = SPContext.Current.Web)
{
  currentSite.Features.Add(new Guid("{GUID}"));
}
相关问题