0

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;
        }

1 Answer 1

1

Even the error description seems nothing to see with it, I've seen this error sometimes because your code is not built in x64. review the project properties and ensure your are building in x64. If this is not the problem. This code is a .net console app, a webpart? (seems a console app because is failing creating a new SPSite...)

Hope it helps.

Luis Manez.

1
  • Ran into the same issue last week, building a console app and not building in x64 and received the same errors. It could not get a reference to the site object and changing it to x64 it all started working. Commented Jan 16, 2014 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.