SiteContext Blank

Andre Pfanz asked on April 19, 2016 20:59

I'm trying to create an alias for a page but am getting an error due to SiteContext.CurrentSiteID being 0 and SiteContext.CurrentSiteName being blank. Here's the code:

DocumentAliasInfo alias = new DocumentAliasInfo();
alias.AliasURLPath = oldURL;
alias.AliasNodeID = postNode.NodeID;
int currentSideID = SiteContext.CurrentSiteID;
alias.AliasSiteID = currentSideID;
string currentSiteName = SiteContext.CurrentSiteName;
DocumentAliasInfoProvider.SetDocumentAliasInfo(alias, currentSiteName);

How do I get the SiteContext to initialize its properties?

Correct Answer

Andre Pfanz answered on April 19, 2016 23:47

I think I found the answer. I'm running the code in a console application. I think SiteContext only works within an IIS application.

1 votesVote for this answer Unmark Correct answer

Recent Answers

Roman Hutnyk answered on April 19, 2016 23:29

You don't have to initialize SiteContext, this is something Kentico takes care of and it should never be blank. Could you please post here what error says?

0 votesVote for this answer Mark as a Correct answer

Andre Pfanz answered on April 19, 2016 23:37

Here are the details of the error message I get when I call the SetDocumentAliasInfo method:

INSERT INTO CMS_DocumentAlias ([AliasNodeID], [AliasCulture], [AliasURLPath], [AliasExtensions], [AliasWildcardRule], [AliasPriority], [AliasGUID], [AliasLastModified], [AliasSiteID], [AliasActionMode])

VALUES (@AliasNodeID, @AliasCulture, @AliasURLPath, @AliasExtensions, @AliasWildcardRule, @AliasPriority, @AliasGUID, @AliasLastModified, @AliasSiteID, @AliasActionMode);


Caused exception:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CMS_DocumentAlias_AliasSiteID_CMS_Site". The conflict occurred in database "Kentico-Kirksey", table "dbo.CMS_Site", column 'SiteID'.

The statement has been terminated.

0 votesVote for this answer Mark as a Correct answer

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