SiteContext.CurrentSite returns null

Rafał Cypcer asked on April 14, 2016 11:20

Hello,

I'm using Kentico 8.2 as a content platform on domain 'ABC' and MVC project on domain 'XYZ'.

When I call method like...

public ActionResult Index(string path)
{
    var alias = DocumentAliasInfoProvider.GetDocumentAliases().Where("AliasURLPath", QueryOperator.Equals, String.Format("/{0}", path ?? String.Empty)).FirstOrDefault();
    if (alias != null)
    {
        var node = DocumentHelper.GetDocument(alias.AliasNodeID, alias.AliasCulture, new TreeProvider()); 
        // ... rest of code
    }
}

... everything is properly found from database (alias and node).

node.AbsoluteURL points to proper url so I'm sure that connectionString is configured properly.

Code like:

var aliases = DocumentAliasInfoProvider
              .GetDocumentAliases()
              .ToList();

properly returns all aliases from Kentico.

The problem is when I want to add below code in this action method, I've got weird results:

var allTreeNodes = DocumentHelper.GetDocuments().ToList(); // returns empty collection (0 results)
var allTreeNodes2 = DocumentHelper
     .GetDocuments()
     .OnSite(ConfigUtils.Tribes.SiteName)
     .ToList(); // returns empty collection (0 results)
var allTreeNodes3 = DocumentHelper
     .GetDocuments()
     .OnCurrentSite()
     .ToList(); // returns empty collection (0 results)
var site = SiteContext.CurrentSite; // returns null
var siteName = SiteContext.CurrentSiteName; // returns ""
var currentSiteID = SiteContext.CurrentSiteID; // returns 0

Do you have any idea why DocumentHelper.GetDocuments().ToList() doesn't return any TreeNode?

Is is connected with fact that SiteContext.CurrentSite returns null?

How SiteContext works (what need to be configured to properly use values from content platform)?

It's strange because on my other Kentico projects those methods returns values and I cannot see why in those case not.

Thanks,

Rafal

Correct Answer

Rafał Cypcer answered on April 18, 2016 10:04

I found the solution.

It turned out that, by default when I run the project culture was set to 'de-DE' (I checked this by method)

CultureHelper.GetPreferredCulture();

which didn't exist at my site. That's why DocumentHelper.GetDocuments() returned 0 elements in collection. When I called method

 CultureHelper.SetPreferredCulture("en-US"); 

which exists in my project, the method

 var allTreeNodes = DocumentHelper.GetDocuments().ToList(); 

returned proper results.

Best regards,

Rafał

0 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.