Sitecore Commerce Connect > Refreshing a Cache via code - sitecore

I am trying to update the commerce catalog from external source. After the incremental update I need to have fresh data in Sitecore tree(data provider should return correct data instead of old(cached) ones). However, if I go to Sitecore right after the data import I can see only the old data till I click on "Refresh Catalog Cache" button in Sitecore Commerce menu.
I found the same info in the documentation for Sitecore Commerce Connect, however I can't find any example how to clean cache via code.
I found several types in "Sitecore.Commerce.Connect.CommerceServer.Caching" namespace. For example, CacheRefresh static class. It has RefreshCatalogCaches method which needs ICommerceServerContextManager contextManager as input parameter. If I create contextManager just using constructor new CommerceServerContextManager() and passing it to the method - it doesn't work(at least I still need to clean cache manually).
I would appreciate any advise/suggestion.
Thank you in advance.

You should do in your code same that happens on "Refresh Catalog Cache" button click:
CacheRefreshEvent eventX = new CacheRefreshEvent("catalogcache", "master", = ID.Null);
EventManager.QueueEvent<CacheRefreshEvent>(eventX, true, true);
For more details, look on Sitecore.Commerce.Connect.CommerceServer.Caching.RefreshCache, Sitecore.Commerce.Connect.CommerceServer implementation via reflector.

Related

How to change users while preserving the store?

I want to implement a "fast login".
I'm developing an enterprise software where a lot of users work in the same organization with the same data in the same computer and I want to be able to know who did what and when. Right now they have to log out and log in and load the data has to be loaded into the store all over again.
What I want is for them to be able to, without logging out, click on a user, from the organization, insert his password and the user is switched while preserving the store.
Any idea how I can accomplish this?
I'm using ember-simple-auth v1.1.0 and ember v2.10.2
The simpliest solution would be disabling page reload when user logs out. As far as I know, it's a reload causes data loss from store, not a logging out by itself. To do this, you need to overwrite sessionInvalidated method in your application route. For example,
sessionInvalidated() {
this.transitionTo('/login');
},
But remember - you lower security with this method: if someone will log out and leave webpage with app open, other person will have a possibility to extract data (if they have enough technical background to at least install ember inspector).
Other solution will require heavy research. I think it should be possible to implement custom authenticator which would allow to authenticate new user without logging out previous, by simply replacing tokens in store. But I don't know how hard it will be to implement and what obstacles you can meet. You will need to read ember-simple-auth's sources a lot, that's for sure.
I was actually able to solve it by simply using authenticate() with another user but never calling invalidateSession() which is the function that calls sessionInvalidated() that looks like this:
sessionInvalidated() {
if (!testing) {
if (this.get('_isFastBoot')) {
this.transitionTo(Configuration.baseURL);
} else {
window.location.replace(Configuration.baseURL);
}
}
}
So by not calling sessionInvalidated() the user isn't redirected or the page refreshed and the new user is able to keep using the store without switching pages.

Refresh Sitecore index to include CD's

I've written some code to refresh an index when an item is programmatically added to Sitecore. Now as the live system is made up of 1 CM and 2 CD Servers I need my code to also trigger the indexing to be refreshed on the CD Servers (unfortunately my dev machine is just a single box so I can't test this fully). I've looked online but can't find anything about this when triggering a re-index programmatically.
So the question is do I need to write code for this or does Sitecore do this by default and if I do need to write code, does anyone have ideas how I go about this. My current code is below.
ISearchIndex index = ContentSearchManager.GetIndex("GeorgeDrexler_web_index");
Sitecore.Data.Database database = Sitecore.Configuration.Factory.GetDatabase("web");
Item item = database.GetItem("/sitecore/content/GeorgeDrexler/Global/Applications");
index.Refresh(new SitecoreIndexableItem(item));
My config for the index has the remotebuild strategy enabled
<strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/remoteRebuild" />
As #Hishaam Namooya pointed out in his comment, publishing from master to web should trigger the web index updates out of the box, unless you've disabled something in the configurations.
Note that items won't publish unless they are in a final workflow state, so if you want a completely automated process that creates the item, updates the local index, and then immediately updates the web index, you will also need to update the workflow state to your final approved state and then trigger a publish of the item.

Sitecore Scheduled Task: Cannot access any items in run class

I configured a scheduled task via database, which is triggered as expected and calls a method "Execute" in a C# class. My problem is that in my Execute method no Sitecore items can be accessed. I even get a NullPointerExecption when calling Sitecore.Context.Database.
I guess this must be a security problem, which makes sense. So I tried to get the permissions using UserSwitcher and SecurityDisabler, but that did not help.
Can anyone point me to what I'm missing?
You cannot use Sitecore.Context in a scheduled task. It does not exist there. Sitecore.Context is tied with HttpRequest, and there is no request in a scheduled task. And as John W writes in his article (The Sitecore Context):
The Sitecore context, exposed by the static Sitecore.Context class, contains information about the Sitecore installation and the current HTTP request. Processors in the httpRequestBegin pipeline defined in the web.config file are largely responsible for defining the Sitecore context. After Sitecore determines the context database, it determines the context item in that database.
In a schedule job you should get the database by its name, e.g.:
var masterDb = Sitecore.Data.Database.GetDatabase("master");
var webDb = Sitecore.Data.Database.GetDatabase("web");

Using New Relic with Sitecore

I am testing New Relic with Sitecore CMS. All of the New Relic web transactions are being sent to the items layout file, so I am unable to drill into item level details in New Relic.
I am trying to use the New Relic API to call SetTransactionName and set it to the items URL, but I can't seem to make it work. I have created an httprequestbegin pipeline processor, and I have put it right at the end, right after:
<processor type="Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel"/>
I have the New Relic API assembly installed, and is also in my bin folder. Here is the line of code that I am trying to run.
NewRelic.Api.Agent.NewRelic.SetTransactionName("Custom", Sitecore.Context.RawUrl);
Any ideas what I am possibly doing wrong? All web transactions still show up as the items layout file.
I'm setting the transaction name in the httpRequestProcessed pipeline and that works. Started out using the httpRequestBegin but I found that is was not working every time. Also remember that your request must take longer than 500 ms to execute before NewRelic picks it up.
Additional integration points I did with Sitecore:
Log4Net Appender that reports to NewRelic using NoticeError
HttpModule picking up Application_Error and reporting to NewRelic using NoticeError
Use Item path to name transactions and use AddCustomParameter to add Language, Database, User etc.
There is a module on the marketplace that sorts all this out:
http://marketplace.sitecore.net/en/Modules/New_Relic.aspx
We had similar issues when we started using New Relic with our Sitecore application about 18 or so months back. Unfortunately nobody was using New Relic with Sitecore at the time. What we settled on was to add the following code to a base Page class that every page in our site inherits:
// --- Set custom transaction name for New Relic.
NewRelic.Api.Agent.NewRelic.SetTransactionName("Pages", Sitecore.Context.Item.Template.FullName));
// --- Set custom parameter to store raw url to assist with diagnostics.
NewRelic.Api.Agent.NewRelic.AddCustomParameter("RawUrl", Request.RawUrl);
For our application template names are enough to distinguish trends and we added the custom parameter to stuff the entire RawUrl (we noticed oddities at the time where New Relic wasn't capturing the complete url for us, that might not be the case any longer).

How to uninstall metro device meta data for more option

I have got device meta data for my camera app from my team.I installed the device metadata using meta data authoring tool.I have following query.since this device keeps changing as team is working on it,i do update the device metadata everyday.
my question is
does updating device meta data created new entry in the cache or it updates the existing one.
how to uninstall device meta data using tool. presently i am doing it manually by deleting the meta data cache.
I Guess the problem is due to multiple copies of metadata of a device App for device(camera).
I checked all the metadata in
%programdata%\Microsoft\Windows\DeviceMetadataStore
using Metadata Authoring tools and compared the package name and deleted it if they were same. I deleted all related entry from
%programdata%\Microsoft\Windows\DeviceMetadataCache\dmrccache\multiloc
one can also compare package Name in SoftwareInfo.xml.
I re installed the metadata.now it is working fine.
Please confirm my finding whether multiple copies of metadata for same device/device app cause problem.