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

本文关键字:获取 SharePoint 应用程序 Web SP 何使用 编程 方式 | 更新日期: 2023-09-27 18:37:01

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

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

您可以按如下方式实现它,

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

下面是获取 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…    
}
}
}
}
}
});
}