How can I login programmatically into Sitecore? - sitecore

How can I login programmatically into Sitecore?
For example if you would like to connect a small part of the Sitecore API to a desktop application, you would need to login into sitecore first to access the databases etc.
Can this be done?

As Mark said, you will need to create a web service that your desktop app will talk to. If you need to deal with permissions in that service you have two options.
Use a SecurityDisabler to make your webservice run in the context of an Admin user.
using (new Sitecore.SecurityModel.SecurityDisabler())
{
// do stuff here
}
For more specific control you can use a UserSwitcher.
From the Security API Cookbook page 34
string domainUser = #"domain\user";
if (Sitecore.Security.Accounts.User.Exists(domainUser))
{
Sitecore.Security.Accounts.User user =
Sitecore.Security.Accounts.User.FromName(domainUser,false);
using (new Sitecore.Security.Accounts.UserSwitcher(user))
{
//TODO: code to invoke as user
}
}

Not really. What you can do, however, is write a supporting web service for your desktop application, and have that run in a Sitecore context.

There is an easier way to accomplish your goal:
Create a desktop application and
reference the same version of the
Sitecore binaries that your web app
uses.
Configure your desktop application
to point to the same Sitecore DBs as
your web site.
Use the security disabler and then
set the context as follows:
Sitecore.Context.SetActiveSite("website");
Sitecore may tell you that what you are trying to do will not work. But trust me, this works and I've used this method in a project before.
Additional details:
Sitecore uses the Master, Core and Web DBs as it's data store. My suggested method uses Sitecore APIs to write directly to the DBs. When using this method, you'll need to be aware of the cache implications.
cheers!

Related

Azure Resource Template for new api app resource

I want to create an Azure Resource Group template which includes the new api app resource. I say new since api apps have changed: App Service API Apps - What's changed
.
I tried this Create a Logic App plus API app using a template . Note: I don't want to create a logic app but this example also includes an api app. When I run this example I get a message: There is a new version of API Apps. Click here to learn more. So this is not the correct example. I can't find a better example.
Note: when I simply add "kind: apiapp" to the "Microsoft.Web/sites" resource, no apiapp is deployed, I also don't get an error.
So my question is how to create a new api with Azure Resource Templates.
I contacted MS for this, they helped me and now it's fixed. Their answer:
Have you installed the new Azure SDK version 2.8.2.1? In that release (actually in 2.8.1 and beyond) we added “Export ARM Template” capability to the provisioning UI. So when you go to publish, then click the New button, you can export your ARM template. That way you’d be able to see exactly the right syntax for creating an API App.
So in VS create a web application, publish it to app service, check api app and then click the export button and you get the correct json.
See also:
https://feedback.azure.com/forums/223579-azure-portal/suggestions/7163577-ability-to-export-a-resource-group-as-a-template

OpenCart - Can I access data externally?

I have a project in mind for a desktop aplication that interacts with e-shops directly. My goal is to create an application that uses the e-shop´s table and presents it to the shop through this app.
Before I get too into it I want to find a shoping cart software (preferably the oneclick installs that many webhosts offer) that will let me access it´s tables and modify/read/write at will without going throught the website.
Could I do this with OpenCart?
if not which way should I shoot?
Thanks in advance
Normally this is not possible unless You'd stick directly with some DB administration tool that will connect directly to the database server. There are some options though:
write an API for the desktop application - it could be based on web services - that the desktop application will comunicate with (more work has to be done but this should be the best solution)
let the desktop application connect directly to the database server (the same settings as OpenCart uses; requires only the desktop application development)
or as I mentioned, use a desktop DBMS tool for MySQL (or one that is universal), there are plenty of them, many also free... (no development at all but I'm not sure if this would be the desired solution)

Sitecore: Access Context from an asmx webservice

As stated in the subject. I am trying to access my Sitecore context from an independent .asmx web service. Was wondering whether or not theres a proper way of doing it.
The Sitecore Context should be available in a classic ASMX web service, however, because a request to the service may not provoke the default Sitecore pipelines, it is a good idea to set the site context as follows:
[WebMethod(EnableSession = true)]
public string MyServiceMethod()
{
using (new SiteContextSwitcher(SiteContextFactory.GetSiteContext("website")))
{
// Here you can access Sitecore.Context
}
}
Sitecore is fairly greedy about handling all URL requests that hit the application. The easy way around this is to add the URL to the IgnoreUrlPrefixes setting. However, this denies you access to some Sitecore Context values, such as Site and Database, which Sitecore could still resolve based on the host name.
I'd recommending adding a processor to the httpRequestBegin pipeline which aborts the pipeline at the right moment (right before ExecuteRequest) if the URL appears to be your asmx service. An example of doing this for MVC requests can be found here. For your asmx service, you'll probably need to check the URL string directly. Bonus points for configuring that URL using a property on the processor class (c.f. Sitecore Configuration Factory).

How do I create a rest web service in Grails?

The idea is to call a method from a website(in php) to my application (in Grails). The application will serve data in json format.
The website and the application are hosted in two different servers. The website is on Yahoo and the application is on Rackspace.
Now, I want to create a web service in my Grails application which serves list of cities in json format.
City Class
class City {
String name
String code
}
How do i write the web service method?
Try the grails jaxrs plugin (https://github.com/krasserm/grails-jaxrs) which will do excactly what you want without any hassle.
Simply install it, create a Resource object with the introduced create-resource command and create and annotate the methods as you wish. all other things are managed by the plugin so you don't have to worry about Controller or UrlMapping...
You need only the annotation #Resource(uri='/cities') on your domain and call the url/cities.json (but, its'n RESTful)
You will want to use a few tools, first you will create a controller that deals with the requests and pushes them off to your service layer.
You can use URL Mappings to make it more RESTFul check out the doc that way all the http methods will be mapped to actions in your controller.
Also if you will be doing a fair bit of json I would recomend starting with the gson plugin it has a fuller feature set then the built in JSON support.
The link from the comment above is a great resource to read as well.
I have found that I most of the time want to support the accept header as well in which case you will need to update your config with the following code. See withFormat doc for more info.
grails.mime.use.accept.header = true

creating liquid synthaxx app

I'm a new developer to Shopify but been developing websites for 10+ years. Just got my partner account and private application setup. Been looking through the available documentations on the site and wasn't sure if applications are allowed to live on the users' shopify site or admin. is this possible? All I've seen so far are api call documentations but I'm having no luck finding anything regarding, how to add options to the admin or the liquid theme. Any references or pointers would be great!
I'm trying to figure out a way so when a user installs my app, A can add a 'part' that they can use and even new variables that they can use.
Try building with Greasemonkey for Firefox or a native Chrome App. Then convince your clients to install your code in their shop admin. Partner App with Shopify lets you inject links too. Hook that all up to your backend, and you're golden.