Dnn 8: caching module settings - caching

I put some module settings via
var moduleController = new ModuleController();
moduleController.UpdateModuleSetting(moduleId, "key", value);
Later if I try to access the setting using
var rcModule = ModuleController.Instance.GetModuleByDefinition(PortalSettings.PortalId, "MyModule");
var value = rcModule.ModuleSettings["value"]?.ToString() ?? string.Empty;
the same value is returned (even if I resave the setting) until I clear the app cache. The value is correct after every saving settings in the database but not in the module. I also tried to add ModuleController.SynchronizeModule(moduleId); to my save settings method but it didn't help. Module and page cache both disabled.
What's wrong?

You are creating a new instance of moduleController, not getting the existing one from memory.
You can clear the cache programmatically.
DotNetNuke.Common.Utilities.DataCache.ClearModuleCache(TabId);
DotNetNuke.Common.Utilities.DataCache.ClearTabsCache(PortalId);
DotNetNuke.Common.Utilities.DataCache.ClearPortalCache(PortalId, false);
Or get the correct instance and edit the properties.
ModuleInfo moduleInfo = ModuleController.Instance.GetModule(ModuleId, TabId, false);
moduleInfo.ModuleTitle = "New Title";
ModuleController.Instance.UpdateModule(moduleInfo);

Related

How to set JsonSerializer default settings globally

For System.Text.Json.JsonSerializer, I have to set the options every time I serialize or deserialize or have to set attributes on every property of the object, due to lack of a way to set/change the default settings. At least I am not able to find one.
JsonSerializer.Deserialize<TypeListDTO>(
"{\"listNo\":33}",
new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase});
Is there such a way available? If not, is there a workaround available?
EDIT: I am using .Net Core 3 with Endpoint Routing. But could very well not be using it at all.
Try it with AddJsonOptions(Action) in Startup.ConfigureServices:
services.AddMvc()
.AddJsonOptions( options =>
{
options.SerializerSettings.Formatting = Formatting.Indented;
options.SerializerSettings.TypeNameHandling = TypeNameHandling.Objects;
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});

How to cache in sharepoint 2010 based on login user

I need to store dynamically created xmldatasource for menu in cache based on the login user. The below code is returning the same data for all the users since i did not mentioned the login user. where do I need to mention the login name while add a cache? also i want to reset or remove the cache while add a new site from event receiver since sitemap has to recreate.
private static object _lock = new object();
public XmlDocument CacheData()
{
XmlDocument item;
lock (_lock)
{
item = (XmlDocument)Cache["SiteMapCache"];
if (item == null)
{
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
SPWebApplication webapp = site.WebApplication;
item = GenerateMenu(webapp);
}
Cache.Add("SiteMapCache",
item, null,
DateTime.Now.AddMinutes(1),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Default,
null);
}
return item;
}
}
one Not sure why you would be doing this. As if you built a Sharepoint OOTB publishing site, additional sub site's which a user had access to would appear. If you went down the root of caching each you could end up with loads of entries in the cache, as each one would need to be different.
Else add the USERName to the CacheKey name.
Then change your code so it first checks to see if the "SiteMapCache"+Spcontext.Current.Web.CurrentUser.name is null, if it is fetch it and store it as cache.
You will need another cached item, to store a list of users who have been added to the cache.
Then if a new site is created, loop through each one and set cached item to null.

Logout change view sencha touch

Hi I need to implement keep login in my sencha touch application
Please see my code below:
Login.js - Once user click login, it will store "sessionToken" in local storage.Then it will go to main Page
onBtnLoginClick: function(){
var loginviewGetValue = Ext.getCmp('loginview').getValues();
var bbid = Ext.getCmp('bbID').getValue();
var bbpassword = Ext.getCmp('bbPassword').getValue();
var LoginLS = Ext.getStore('LoginLS');
LoginLS.add({
sessionId: 'sadsadsadasd'
,deviceId:'1'
,bb_id :bbid
});
LoginLS.sync();
var mainForm= Ext.create('bluebutton.view.Main');
Ext.Viewport.setActiveItem(mainForm);
App.js~ Everytime launch function will check sessionToken in localStorage. If Localstorage is empty then it will go to login page.Else it will go to main Page
launch: function() {
// Destroy the #appLoadingIndicator element
Ext.fly('appLoadingIndicator').destroy();
// Initialize the main view
var LoginLS = Ext.getStore('LoginLS');
LoginLS.load();
var record = LoginLS.getAt(0);
if(record != undefined){
var sessionId = record.get('sessionId');
if (sessionId !=undefined){
Ext.Viewport.add(Ext.create('bluebutton.view.Main'));
}
else
Ext.Viewport.add(Ext.create('bluebutton.view.Login'));
}
else{
Ext.Viewport.add(Ext.create('bluebutton.view.Login'));
}
// Ext.create('bluebutton.view.TopMenuList');
},
Logout.js~Logout will clear the sessionToken and go to login page again
onLogoutClick: function scan() {
var LoginLS = Ext.getStore('LoginLS');
Ext.Viewport.setMasked({
xtype: 'loadmask',
message: 'Loading...'
});
LoginLS.load();
var record = LoginLS.getAt(0);
LoginLS.removeAll();
LoginLS.sync();
//Load a new view
// Ext.getCmp('tabpanel').destroy();
var loginForm = Ext.create('bluebutton.view.Login');
Ext.Viewport.setActiveItem(loginForm);
Ext.Viewport.setMasked(false); // hide the load screen
But i having a problem now. I not able to go back login page. It go to the blank page. Please give me some solution. Thanks.
Here is the error i get
[WARN][Ext.data.Batch#runOperation] Your identifier generation strategy for the model does not ensure unique id's. Please use the UUID strategy, or implement your own identifier strategy with the flag isUnique. Console.js:35
[WARN][Ext.Component#constructor] Registering a component with a id (`loginview`) which has already been used. Please ensure the existing component has been destroyed (`Ext.Component#destroy()`. Console.js:35
[WARN][Ext.Component#constructor] Registering a component with a id (`bbID`) which has already been used. Please ensure the existing component has been destroyed (`Ext.Component#destroy()`. Console.js:35
[WARN][Ext.Component#constructor] Registering a component with a id (`bbPassword`) which has already been used. Please ensure the existing component has been destroyed (`Ext.Component#destroy()`. Console.js:35
[WARN][Ext.Component#constructor] Registering a component with a id (`btnLogin`) which has already been used. Please ensure the existing component has been destroyed (`Ext.Component#destroy()`. Console.js:35
[DEPRECATE][bluebutton.view.Login#show] Call show() on a component that doesn't currently belong to any container. Please add it to the the Viewport first, i.e: Ext.Viewport.add(component);
Looking at the error messages it is clear that you are trying to create login panel again without destroying existing component. Error comes because you are not allowed to use same id more than once in application.
To avoid this you should not create same view multiple times, you should reuse views which is good for performance also. One more thing, you should give id to n element if and only if you can't do without it.
Assuming you cannot avoid id attributes you should do one of these 2 things:
Create new view only if it doesn't exist
var loginView = Ext.getCmp("loginview");
if(!loginView){
loginView = Ext.create('bluebutton.view.Login');
}
destroy login view as soon as it is out(hide/erased) of viewport by calling:
var loginView = Ext.getCmp("loginview");
loginView.destroy();
Use itemId for your components instead of idand reference them accordingly in you Controller. Check this question out: Warning saying `Id` exist and should be destroyed

How to save a record and immediately use its GUID

I'm executing some javascript from a ribbon button and what I want to do is save the record that I am creating and then immediately use its GUID for some code a bit further on. Each time I try it the GUID is coming back null even though I'm requesting it after the record has been saved. If I try the button again after I've saved it then it works, but not as I'm saving it.
Is there a way to do this?
function RibbonButton_AddProduct()
{
//Save the Record
Xrm.Page.data.entity.save();
LoadProductCreate();
}
function LoadProductCreate()
{
var serverUrl;
var errorMessage = "Context to retrieve the Server URL is not available.";
if (typeof GetGlobalContext != "undefined"){
serverUrl = GetGlobalContext().getServerUrl();
} else {
if (typeof Xrm != "undefined"){
serverUrl = Xrm.Page.context.getServerUrl();
} else {
alert(errorMessage);
return;
}
}
if (serverUrl.match(/\/$/)){
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
var recordId = Xrm.Page.data.entity.getId();
alert(recordId);
var url = serverUrl + "/main.aspx?etc=10030&extraqs=%3f_CreateFromId%3d%"+recordId
+"%257d%26_CreateFromType%3d10029%26etc%3d10030%26"
+"pagemode%3diframe%26preloadcache%3d1345465354543&pagetype=entityrecord";
window.open(url);
}
Here’s a different approach to solving this problem.
What you are trying to do is ‘working against the system’ - you are effectively making two save buttons. In the rest of Crm when the Id is required for a ribbon button the record must first be saved. E.g. you can’t use the dialog or workflow buttons on an unsaved record, you also can’t 'add new/existing' to an unsaved record.
So my solution would be to disable the button on unsaved forms, force the user to save the record manually and then allow them to use your button - this is the way Crm is meant to be used, and is the way the rest of Crm will work.
You should not work against the system, you should work with it, you have a product to customise and extend – not change.
If this doesn’t meet your requirement I would suggest uses Greg’s suggestion (1) of having flags, though it sounds a bit messy - but then this is a requirement that inherently is.
You could try one of two things:
Add a hidden boolean attribute to your form(e.g. "new_launchProductCreate"), set it in code prior to save and then read it onLoad.
Instead of setting the value prior to create (and therefore potentially commiting it to the database), you could create a plugin registered against the "Create" step of your record that injects a boolean value into the Entity.Attributes collection as the record is returned to the user. This would prevent the value persisting into the database and running every time your form loads.
You can instead use AJAX to reset the value as you launch your onLoad code so that it doesn't trigger on every form load
Assign the record guid manually, use AJAX to save your record, pop your new window using th enew guid and then reload your original form (so that the form is no longer in an "unsaved" state).
At the risk of being proven wrong as I cannot verify this right away... you will need to save and then reload the page.
The value stored in Xrm.Page.data.entity.getId() is set when the page is loaded/initialised and hence won't be updated when you access it after you have called Save().
It is also why it does work when you reload the page.
Perhaps you could call save and then reload the window adding a querystring variable of your own, to indicate that this event has just occurred?
e.g.
function DoSomething() {
//do your stuff
Xrm.Page.data.entity.save();
//something like - sure someone can do better!
window.location = window.location.href + '&foo=bar';
}
and then register something like this onFormLoad
function OnLoad() {
var queryStringParms = Xrm.Page.context.getQueryStringParameters();
//test to see if your query string param exists here
for (var i in queryStringParams) {
//if you find query string, do extra processing here
}
}

How to read in other class from isolated storage?

How can I read a setting in MainPage.xaml/.cs ?
I use this setting sample but I dont have accces to/from other class.
For example: In MainPage.xaml we have TextBlock, in SettingsWithoutConfirmation.xaml we have TextBox, How TextBlock can read this string from TextBox (and also save this to isolated storage) ?
I am newbie, please be gentle ;)
I'm not sure I completely understood your question.
In any xaml page, you can use the following to access settings that have been stored in isolated storage:
var settings = IsolatedStorageSettings.ApplicationSettings;
if( settings.Contains("name") ) {
//the value is available here
}
//to store a value permanently
settings["name"] = "New Name"
Try this:
Save data to settings:
var settings = IsolatedStorageSettings.ApplicationSettings;
settings.Add("myemail", "support#gmail.com");
Get data from settings:
var location = settings["myemail"].ToString();
settings["myemail"] = "support#gmail.com";

Resources