I have some custom code which is trying to get some item value from a list. First I wil try create a spsite object. But I got this error:
Exception in GetConfigValue:PortalName, problem: at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite,
SPUserToken userToken)
at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
at MyCompany.Portaal.Helper.ConfigListHelper.GetConfigValue(String key)
Error information:
The Web application at http://myWebsite.myCompany.nl could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
Stacktrace:
at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
at RocNijmegen.Portaal.Helper.ConfigListHelper.GetConfigValue(String key)
I see something strange. The url which is working is starting with https and the url in the event viewer error is starting with http. Does someone know what the problem is?
This is the method:
public static string GetConfigValue(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("Config key is not set.", key);
}
SPSite currentSite = null;
bool siteFromContext = false;
try
{
if (SPContext.Current != null)
{
currentSite = SPContext.Current.Site;
siteFromContext = true;
}
else if (CurrentWebApplication != null)
{
currentSite = new SPSite(CurrentWebApplication.GetResponseUri(SPUrlZone.Default).OriginalString);
}
if (currentSite != null)
{
if (string.IsNullOrEmpty(currentSite.PortalUrl))
{
// No portal connection, assuming this is the root site collection.
return GetConfigurationValueFromList(currentSite, key);
}
// Get site
using (var site = new SPSite(currentSite.PortalUrl))
{
return GetConfigurationValueFromList(site, key);
}
}
}
catch (Exception ex)
{
DiagnosticsService.LogError(ex, "Exception in GetConfigValue:" + key + ", problem:" + ex.StackTrace, 0, DiagnosticsCategory.Jobs);
}
finally
{
if (!siteFromContext && currentSite != null)
currentSite.Dispose();
}
return string.Empty;
}