Get Alternate Access Mappings by code

At the time I explained to you what problems occur when the wrong set of Alternate Access Mappings.
This time I will show you how to get the Alternate Access Mappings by code.

The following function returns an array of links for Current Web Application - Alternate Access Mappings.

/// <summary>
/// Create a list with the alternate access mapping urls
/// </summary>
/// <returns></returns>
private List<Uri> GetCurrentAlternateAccessMappingsUrls()
{
    List<Uri> alternateUrls = null;
    // Make sure anonymous users can access the web application properties
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        alternateUrls = new List<Uri>();
        // Retrieve the WebApplication object
        SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
        // Get Alternate Url
        foreach (SPAlternateUrl alternateUrl in webApplication.AlternateUrls)
        {
            Uri url = new Uri(alternateUrl.IncomingUrl);
            // Fill the List
            alternateUrls.Add(url);
        }
    });
    // Return the list with the urls              
    return alternateUrls;      
}

Nice .. :)

Comments

Popular posts from this blog

A sharepoint list view of the current month

The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters

Export SharePoint 2010 List to Excel with PowerShell