Pages

Search This Blog

Wednesday, January 18, 2012

Get Current Web Application - Alternate Access Mappings Programmatically | SharePoint

Some times we need to get all the Alternate Access Mapping of the current site.

Below code snipped will help you to achieve this:

Code:


class Program
    {
        private static List<Uri> GetSiteAlternateAccessMappingsUrls(SPSite site)
        {
            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 = 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;
        }

        static void Main(string[] args)
        {
            try
            {
                using (SPSite site = new SPSite("http://br-pc-275:6000/"))
                {
                    List<Uri> test = GetSiteAlternateAccessMappingsUrls(site);
                }

            }//end of try
            catch (Exception ex)
            {
            }
        }
    }

Thanks,

1 comment: