Iucounu
Iucounu

Reputation: 1660

Any way to access Sitecore programmatically outside of a web context?

I am using the Sitecore direct access API for current projects. I sometimes run into situations where I'd like to have a standalone assembly which calls into Sitecore, and not necessarily in a web context. Is there any way to get access to a Sitecore context without having one's code deployed in a website?

There are of course tons of configuration settings. So far, my attempts to spoof a web context and provide the necessary settings have met with failure. Is there any documentation on what particular set of steps would need to be performed to load global objects, and do everything else that would normally be done by Sitecore request handling / pipeline processing?

Upvotes: 0

Views: 1452

Answers (1)

Matthew Dresser
Matthew Dresser

Reputation: 11442

If you don't want to go down the web service route, this blog post describes setting up a class library project to access Sitecore information. In this case, it's for NUnit testing, but I believe the technique is the same.

To summarise, it involves duplicating the Sitecore web.config information into an app.config file (along with the other configuration files), referencing the relevant Sitecore assemblies and making a few config tweaks to fix references to the App_Config folder.

Note, when using this technique, your Sitecore.Context.Item will be null so you would probably need to access items directly via their path or Id e.g.

var db = Sitecore.Configuration.Factory.GetDatabase("master");

var homeItem = db.GetItem("/sitecore/content/home");
//or
var homeItem = db.GetItem(new Sitecore.Data.ID("<your GUID here>"));

Upvotes: 2

Related Questions