Freigeben über


Take Consideration of AAM When Coding WebParts

When you code webparts, you really design it carefully and avoid use/expect/demand absolute url in webparts configuration settings, this will make moving from the site from environment to environment less painful and also make it work with AAM.

Here is one example:

A webpart that is used to output a few links stored in a list so one of the settings the webpart needs is the siteUrl, which is where the location of the list.

 site = new SPSite(siteURL);
web = site.OpenWeb();
SPList spList = web.Lists(ListName);

foreach (SPListItem li in spList.Items)
{
    //construct the link from one of the column with URL data type
}

If you have AAM on this site and you are using the absolute path for siteURL then every access URL will produce link with that hardcoded siteURL. The right way of doing this is to use relative path for siteUrl and you would get the base address from System.Web.HttpContext.Current and insert the base url in front of relative siteURL.

Comments