04 July 2011

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 .. :)

No comments:

Post a Comment