2009-10-28 172 views
2

我想部署一個選擇的功能,並要做到這一點,我需要選擇目標站點,然後使用:部署功能單一的SharePoint網站/如何選擇一個SharePoint網站

objWeb。 Features.Add(新的Guid({guid of feature}));

我的問題是我怎麼會選擇這個網站,所有我已經找到了幫助創建使用其構造的網站,然後跟蹤它,在這裏,我想打開一個現有的。

謝謝。

回答

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對象,但它取決於你有什麼關於該網站的信息(名稱,url,在heirarchy中的位置)

如果要激活針對網站集或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

因此,我有一個webApp http:// test:10413 /要部署到整個應用程序我會將網址傳遞到sitecollection創建者? – Nath 2009-10-28 15:16:45

+0

您能否澄清該功能的範圍(Web應用程序,網站集或網站)?我以爲你試圖部署到一個單一的網站,但如果你想部署到WebApp,我可以找到一些代碼爲 – AlexWilson 2009-10-29 08:29:02

+0

我添加了一些例子來激活一個解決方案作用於Web應用程序和網站集合 – AlexWilson 2009-10-29 08:44:20

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}")); 
}