Friday 31 May 2013

Change the Web URL from Alternate Access Mappings using C#


There are many types of Zones available in the SharePoint access mappings.
  • Default
  • Intranet
  • Extranet
  • Internet
  • Custom 

Using this code you can change the alternate access mappings from Default to Internet.


            string webUrl = string.Empty;
            using (SPWeb web = workflowProperties.Site.RootWeb)
            {
                using (SPSite site = web.Site)
                {
                    foreach (Microsoft.SharePoint.Administration.SPAlternateUrl altUrl in site.WebApplication.AlternateUrls)
                    {
                        if (altUrl.UrlZone == Microsoft.SharePoint.Administration.SPUrlZone.Internet)
                            webUrl = altUrl.IncomingUrl .ToString() + site.ServerRelativeUrl.ToString();
                        //fullweburl = webUrl + site.ServerRelativeUrl.ToString();
                    }
                }
            }

2 comments:

  1. @Prasath , In above code in which line you changing the alternate access mappings from Default to Internet.
    Do let me know if u have any code snippet to update or change zones from default to other

    ReplyDelete
  2. Hi @Suchithra,

    I had some problems in obtaining the alternate access mapping url. It was returning me only default url that cant be used in some places as per my requirements. This code gets the default url and checks for alternate access mapping url that was provided in the server in that "if" case. Here, if you have provided the alternate access mapping as Internet in your server, then it gets the url from there and stores it in "webUrl" string. Now this string contains alternate access mapping url for the default url.

    Justs add an "else if" case below "if" case and change SPUrlZone.Intranet and so on conditions to check what alternate access mapping url was provided at server. Whenever the condition matches it picks up that particular url from that particular zone.

    ReplyDelete