Display sitecore treelist selected in cshtml - sitecore

Source Image
Display selected values in cshtml file
wrote this code but it's not working
List<Item> lstWellnessName = null;
Sitecore.Data.Fields.MultilistField WellnessList =
WellnessItem.Fields["Property Wellness"];
if (WellnessList != null && WellnessList.TargetIDs != null)
{
Item[] result = WellnessList.GetItems();
lstWellnessName = result.ToList();
}

Related

How to edit list elements in kotlin or add to second list?

I tried to edit a list in-place but wasn't able to get it working. Now trying to edit individual elements and add them to a second list. However the second list remains null and does not get updated. Any help would be appreciated.
var localFiles: MutableList<String> = File(localPath).list().toMutableList()
var localFileDates: MutableList<String>? = null
val iterateLocal = localFileDates?.listIterator()
for (item in localFiles) {
var date = item.takeLast(10).take(6)
if (date.matches("[0-9]+".toRegex()) and (date.length == 6) and (date != null) and (date != "null")) {
iterateLocal?.add(item.takeLast(10).take(6))
}
}
println(networkFiles) // prints correct outpu
println(localFileDates) // prints null
You need init localFileDates variable:
var localFileDates = MutableList()
var localFiles: MutableList<String> = File(localPath).list().toMutableList()
var localFileDates = MutableList<String>()
val iterateLocal = localFileDates?.listIterator()
for (item in localFiles) {
var date = item.takeLast(10).take(6)
if (date.matches("[0-9]+".toRegex()) and (date.length == 6) and (date != null) and (date != "null")) {
iterateLocal?.add(item.takeLast(10).take(6))
}
}
println(networkFiles) // prints correct outpu
println(localFileDates) // prints correct
It is better to use map{..} function to create a copy of the list with updated values.
val localFiles = File(localPath).list()?.toMutableList() ?: listOf()
val localFileDates = localFiles.mapNotNull { item ->
val date = item.takeLast(10).take(6)
if (date.matches("[0-9]{6}".toRegex()) {
date
} else {
null
}
}
println(localFiles)
println(localFileDates)
I use the mapNotNull{..} function calls the block for every element of the list and builds the new list only from non-null values.
You do not need var in your code, explicit type names can be omitted too.
The condition can be simplified - no need for the null check, the regular expression filters our the data == "null" case, the length check can be included into the regex too. The date variable can be re-used too.
A more idiomatic (and readable) way:
val localFileDates = File(localPath).list().map { it.takeLast(10).take(6) }.filter {
it.matches("[0-9]+".toRegex()) && (it.length == 6) && (it != "null")
}
Also I suggest you create a named function for takeLast(10).take(6) and the condition to make it clear what is the intent of these lines of code.

when I select many row of table and insert breakpoint dont show my list correctly?

when I trace code don't show city list correctly
how I can fix it ?
My code
public ActionResult GetCity(int idCountry)
{
TravelEnterAdminTemplate.Models.LG.MyJsonResult myresult = new Models.LG.MyJsonResult();
var citystable = db.Cities.Where(p => p.CountryId == idCountry).ToList();
if (citystable != null)
{
myresult.Result = true;
myresult.obj = citystable;
}
else
{
myresult.Result = false;
myresult.message = "داده ای یافت نشد";
}
return Json(myresult, JsonRequestBehavior.AllowGet);
}
This is not the error you are getting the data. There were 32 data in cities table . You just click on every node there the detail will visible

Sitecore change rendering datasource

Hello I would like create special page for private use for where i will be have possibility to change data source value for each rendering item.
I have created next code, but it dos't save any changes to items.
SC.Data.Database master = SC.Configuration.Factory.GetDatabase("master");
SC.Data.Items.Item itm = master.GetItem(tbPath.Text);
if (itm != null)
{
// Get the sublayout to update
//string sublayout_name = txtSublayout.Text.Trim();
//if (!string.IsNullOrEmpty(sublayout_name))
{
// Get the renderings on the item
RenderingReference[] refs = itm.Visualization.GetRenderings(SC.Context.Device, true);
if (refs.Any())
{
//var data = refs.Select(d=>d);
//refs[0].Settings.DataSource
var sb = new StringBuilder();
using (new SC.SecurityModel.SecurityDisabler())
{
itm.Editing.BeginEdit();
foreach (var d in refs)
{
if (d.Settings.DataSource.Contains("/sitecore/content/Site Configuration/"))
{
var newds = d.Settings.DataSource.Replace("/sitecore/content/Site Configuration/", "/sitecore/content/Site Configuration/" + tbLanguage.Text + "/");
// sb.AppendLine(string.Format("{0} old: {1} new: {2}<br/>", d.Placeholder, d.Settings.DataSource, newds));
d.Settings.DataSource = newds;
}
}
itm.Editing.EndEdit();
}
//lblResult.Text = sb.ToString();
}
}
}
how I can change data source ?
thanks
You're mixing up two different things in Sitecore here.
The datasource that is assigned to a rendering at run-time, when Sitecore is rendering a page
The datasource that is assigned to the presentation details of an item
The simplest approach to achieve what I think you're trying to achieve, would be this.
Item itm = database.GetItem("your item");
string presentationXml = itm["__renderings"];
itm.Editing.BeginEdit();
presentationXml.Replace("what you're looking for", "what you want to replace it with");
itm.Editing.EndEdit();
(I've not compiled and run this code, but it should pretty much work as is)
You are not saving the changes to the field.
Use the LayoutDefinitation class to parse the layout field, and foreach all the device definition, and rendering definitions.
And finaly commit the LayoutDifinition to the layout field.
SC.Data.Items.Item itm = master.GetItem(tbPath.Text);
var layoutField = itm.Fields[Sitecore.FieldIDs.LayoutField];
LayoutDefinition layout = LayoutDefinition.Parse(layoutField.Value);
for (int i = 0; i < layout.Devices.Count; i++)
{
DeviceDefinition device = layout.Devices[i] as DeviceDefinition;
for (int j = 0; j < device.Renderings.Count; j++)
{
RenderingDefinition rendering = device.Renderings[j] as RenderingDefinition;
rendering.Datasource = rendering.DataSource.Replace("/sitecore/content/Site Configuration/",
"/sitecore/content/Site Configuration/" + tbLanguage.Text + "/");
}
}
itm.Editing.BeginEdit();
var xml =layout.ToXml()
layoutField.Value = xml;
itm.Editing.EndEdit();
The code is not testet, but are changed from something i have in production to replace datasources on a copy event
If any one looking for single line Linq : (Tested)
var layoutField = item.Fields[Sitecore.FieldIDs.LayoutField];
if (layoutField != null)
{
var layout = LayoutDefinition.Parse(layoutField.Value);
if (layout != null)
{
foreach (var rendering in layout.Devices.Cast<DeviceDefinition>()
.SelectMany
(device => device.Renderings.Cast<RenderingDefinition>()
.Where
(rendering => rendering.ItemID == "RenderingYoulooking")))
{
rendering.Datasource = "IDYouWantToInsert";
}
layoutField.Value = layout.ToXml();
}
}

Showing up the image through code behind

I have a sitecore control like this: <sc:Image ID="imgLogo" runat="server" Field="Image"/>
Now the current item does not have an Image field. I am pulling the Item through droptree in CurrentItem like this (GetFieldValue is same as GetField. I am overriding the base class in Sitecore):
string countryGuid = CurrentItem.GetFieldValue("Country", null);
Item country = sitecoreDatabase.GetItem(countryGuid);
BindCountryLogo();
Now in this Item I have an image for the country logo. All I want to show up on the sc:Image.
So far I got this:
private void BindCountryLogo(Item country)
{
Fields.ImageField logoField = country.Fields["Image"];
if (logoField != null && logoField.MediaItem != null)
{
//Sitecore.Data.Items.MediaItem image = new MediaItem(logoField.MediaItem);
//string src = Sitecore.Resources.Media.MediaManager.GetMediaUrl(image);
//imgLogo.DataSource = src;
//imgLogo.ImageUrl = logoField.MediaItem.Source.;
//string src = Sitecore.StringUtil.EnsurePrefix('/', Sitecore.Resources.Media.MediaManager.GetMediaUrl(logoField));
//Sitecore.Data.Items.MediaItem img = new MediaItem(ImageField.Medi)
}
}
None of them are working.
The control has a property "Item" which you can set to the item that holds the imageField.
Your BindCountryLogo should look like this:
private void BindCountryLogo(Item country)
{
imgLogo.Item = country;
}
(Or remove the BindCountryLogo() method and set the Item in the main method ;) )

Opening Rich Text Editor in custom field of Sitecore Content Editor

I'm implementing a custom field in Sitecore for the Content Editor, and I need to be able to open the Rich Text editor and get the data from there. I'm not really sure where to look though, nor how to go about it.
Had to decompile the Sitecore.Kernel DLL in order to figure this out.
First thing is to spin off a call from the Context.ClientPage object
So, for my situation:
switch (message.Name)
{
case "richtext:edit":
Sitecore.Context.ClientPage.Start(this, "EditText");
break;
}
You will then need to have a method in your class with the same name as defined in the above Start method. Then, you either start the rich text control if the request isn't a postback, or handle the posted data
protected void EditText(ClientPipelineArgs args)
{
Assert.ArgumentNotNull(args, "args");
if (args.IsPostBack)
{
if (args.Result == null || args.Result == "undefined")
return;
var text = args.Result;
if (text == "__#!$No value$!#__")
text = string.Empty;
Value = text;
UpdateHtml(args); //Function that executes Javascript to update embedded rich text frame
}
else
{
var richTextEditorUrl = new RichTextEditorUrl
{
Conversion = RichTextEditorUrl.HtmlConversion.DoNotConvert,
Disabled = Disabled,
FieldID = FieldID,
ID = ID,
ItemID = ItemID,
Language = ItemLanguage,
Mode = string.Empty,
Source = Source,
Url = "/sitecore/shell/Controls/Rich Text Editor/EditorPage.aspx",
Value = Value,
Version = ItemVersion
};
UrlString url = richTextEditorUrl.GetUrl();
handle = richTextEditorUrl.Handle;
ID md5Hash = MainUtil.GetMD5Hash(Source + ItemLanguage);
SheerResponse.Eval("scContent.editRichText(\"" + url + "\", \"" + md5Hash.ToShortID() + "\", " +
StringUtil.EscapeJavascriptString(GetDeviceValue(CurrentDevice)) + ")");
args.WaitForPostBack();
}