Partager via


Returning Central Administrative Properties

Use classes in the Microsoft.SharePoint.Administration namespace to access global administrative property settings for a deployment of Microsoft Windows SharePoint Services. The top-level class, SPGlobalAdmin, provides accessor properties to various supplementary classes, such as SPVirtualServer, SPGlobalConfig, and SPUsageSettings. Together, these classes provide access to global administrative settings that are maintained within the configuration database. This programming task shows how to create a custom report of administrative settings and usage.

Use the SPGlobalAdmin constructor to create an SPGlobalAdmin object, and then use its properties to return instances of the other classes. The following example shows how to create a global administration object and then use it to return various property settings:

[Visual Basic .NET]

Dim globAdmin As New SPGlobalAdmin()

Dim mailFrom As String = globAdmin.MailFromAddress
Dim mailTo As String = globAdmin.MailReplyToAddress
Dim mailCodePg As String = globAdmin.MailCodePage.ToString()

Dim apPools As System.Collections.IDictionary = globAdmin.ApplicationPools
Dim myEntry As DictionaryEntry

For Each myEntry In  apPools

    applicationPools += myEntry.Key + "  " + myEntry.Value + "<BR>"

Next myEntry

[C#]

SPGlobalAdmin globAdmin = new SPGlobalAdmin();

string mailFrom = globAdmin.MailFromAddress;
string mailTo = globAdmin.MailReplyToAddress;
string mailCodePg = globAdmin.MailCodePage.ToString();
System.Collections.IDictionary apPools = globAdmin.ApplicationPools;

foreach (DictionaryEntry myEntry in apPools)
{
    applicationPools += myEntry.Key + "  " + myEntry.Value + "<BR>";
}

The example iterates through the collection of dictionary key-and-value pairs returned by the ApplicationPools property, which represent the name and type of each application pool currently being used in Internet Information Services (IIS).

In the next example, the Config property of the SPGlobalAdmin class returns an SPGlobalConfig object, which in turn gets the name of the SharePoint Administration group, as well as a list of the file types that cannot be saved or retrieved on the server:

[Visual Basic .NET]

Dim globConfig As SPGlobalConfig = globAdmin.Config
Dim admGroup As String = globConfig.AdminGroup
Dim blkdFileTypes As System.Collections.Specialized.StringCollection = globConfig.BlockedFileTypes
Dim i As Integer

For i = 0 To blkdFileTypes.Count - 1

    blockedTypes += blkdFileTypes(i) + "<BR>"

Next i

[C#]

SPGlobalConfig globConfig = globAdmin.Config;
string admGroup = globConfig.AdminGroup;
System.Collections.Specialized.StringCollection blkdFileTypes = globConfig.BlockedFileTypes;
            
for (int i=0; i<blkdFileTypes.Count; i++)
{
    blockedTypes += blkdFileTypes[i] + "<BR>";
}

As in the above example, use the BlockedFileTypes property to return a string collection of the blocked file extensions, and then use an indexer to iterate through the collection and concatenate the strings into a single string.

Use the UsageSettings property of the SPGlobalAdmin class to return an SPUsageSettings object, whose properties define usage analysis settings. In this example, the object is used to report the directory where log files are stored, the number of log files created, and whether logging is enabled, as well as the processing start and end times and whether usage processing is enabled.

[Visual Basic .NET]

Dim useSettings As SPUsageSettings = globAdmin.UsageSettings
Dim logFilesDir As String = useSettings.LogFilesDirectory
Dim numberLogFiles As String = useSettings.NumberLogFiles.ToString()

If useSettings.LoggingEnabled = True Then

    logEnab = "Enabled"

Else

    logEnab = "Not enabled"

End If

Dim processEnd As String = useSettings.ProcessingEndTime.ToString()
Dim processStart As String = useSettings.ProcessingStartTime.ToString()

If useSettings.UsageProcessingEnabled = True Then

    useProcEnab = "Enabled"

Else

    useProcEnab = "Not enabled"

End If

[C#]

SPUsageSettings useSettings = globAdmin.UsageSettings;

string logFilesDir = useSettings.LogFilesDirectory;
string numberLogFiles = useSettings.NumberLogFiles.ToString();

if (useSettings.LoggingEnabled == true)
{
    logEnab = "Enabled";
}
else
{
    logEnab = "Not enabled";
}

string processEnd = useSettings.ProcessingEndTime.ToString();
string processStart = useSettings.ProcessingStartTime.ToString();

if (useSettings.UsageProcessingEnabled == true)
{
    useProcEnab = "Enabled";
}
    else
{
    useProcEnab = "Not enabled";
}

The VirtualServers property of the SPGlobalAdmin class returns the collection of all the virtual servers on a server running Windows SharePoint Services. In addition to reporting several administrative settings, the following example iterates through the collection of virtual servers and prints the version number of the installation if the server state (represented by an SPVirtualServerState value) is Ready.

[Visual Basic .NET]

Dim serverVersion As String
Dim virtServers As String
Dim vServers As SPVirtualServerCollection = globAdmin.VirtualServers
Dim j As Integer

For j = 0 To vServers.Count - 1

    If vServers(j).State.ToString().Equals("Ready") Then

        serverVersion = vServers(j).Version.ToString()

    Else

        serverVersion = "N/A"

    End If

    virtServers += "Application Pool ID: " _
        & vServers(j).ApplicationPoolId.ToString() _
        & "<BR>Description: " & vServers(j).Description.ToString() _
        & "<BR>Host Name: " & vServers(j).HostName.ToString() _
        & "<BR>IIS Instance ID: " & vServers(j).IISInstanceId.ToString() _
        & "<BR>Port Number: " & vServers(j).Port.ToString() _
        & "<BR>State: " & vServers(j).State.ToString() & "<BR>URL: " _
        & vServers(j).Url.ToString() & "<BR>Version: " & serverVersion _
        & "<BR><BR>"

Next j

[C#]

string serverVersion, virtServers = "";

SPVirtualServerCollection vServers = globAdmin.VirtualServers;

for (int j = 0;j<vServers.Count;j++)
{
    if (vServers[j].State.ToString().Equals("Ready"))
    {
        serverVersion = vServers[j].Version.ToString();
    }
    else
    {
        serverVersion = "N/A";
    }

    virtServers += "Application Pool ID: " + vServers[j].ApplicationPoolId.ToString() +
    "<BR>Description: " + vServers[j].Description.ToString() +
    "<BR>Host Name: " + vServers[j].HostName.ToString() +
    "<BR>IIS Instance ID: " + vServers[j].IISInstanceId.ToString() +
    "<BR>Port Number: " + vServers[j].Port.ToString() +
    "<BR>State: " + vServers[j].State.ToString() +
    "<BR>URL: " + vServers[j].Url.ToString() +
    "<BR>Version: " + serverVersion + "<BR><BR>";
}