如何使用C#以编程方式从SP Farm获取所有SharePoint Web应用程序?

时间:2015-01-11 08:44:52

标签: c# sharepoint sharepoint-2010 sharepoint-2013

有时需要获取SharePoint场中的所有Web应用程序。那么,如何使用C#以编程方式从SP Farm获取所有SharePoint Web应用程序?

2 个答案:

答案 0 :(得分:3)

您可以按照以下方式实现,

var service = SPFarm.Local.Services.GetValue<SPWebService>(string.Empty);
        foreach (SPWebApplication webApplication in service.WebApplications)
        {

        }

答案 1 :(得分:2)

以下是在SP场中获取所有SharePoint Web应用程序的方法:

public ListItemCollection GetAllWebApplicationsInSPFarm()
{
ddlDataSource = new ListItemCollection();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPServiceCollection services = SPFarm.Local.Services;
foreach (SPService curService in services)
{
if (curService is SPWebService)
{
webService = (SPWebService)curService;
if (curService.TypeName.Equals("Microsoft SharePoint Foundation Web Application"))
{
webService = (SPWebService)curService;
SPWebApplicationCollection webApplications = webService.WebApplications;
foreach (SPWebApplication webApplication in webApplications)
{
if (webApplication != null)
{
//Now you have the required object i.e. webApplication. You can use it like this:
//string webApp = webApplication.AlternateUrls[0].Collection.Name.ToString();
//Write your code here…    
}
}
}
}
}
});
}