Sitecore Add Mutiple Language version to the same Item - sitecore

How do i create a Field value for a Particular Item in a Particular Language? I have an Excel that has all the item Names inside the RootItem .These Items exist in a en-US language Already. i need add values for a particular field for other languages.. Like en-GB, nl-NL, it-IT.
I have a List like
ItemName Language Translation
TestItem en-GB Hello
TestItem nl-NL Hallo
and so on..
The only problem is, when i do item.Add, it creates a new item rather than adding the value to the existing item. How can i handle this?
My code is as follows:
foreach (DataRow row in dt.Rows)
{
Language language = Language.Parse(languageId);
var rootItem = currentDatabase.GetItem(RootItemPath, language);
var item = rootItem.Add(itemName, templateItem);
if (item != null)
{
item.Fields.ReadAll();
item.Editing.BeginEdit();
try
{
//Add values for the fields
item.Fields["Translation"].Value = strTranslationValue;
}
catch (Exception)
{
item.Editing.EndEdit();
}
}
}

Try This:
var rootItem = currentDatabase.GetItem(RootItemPath);
foreach (DataRow row in dt.Rows)
{
Language language = Language.Parse(languageId);
var itemInCurrentLanguage = rootItem.Children.Where(i=>i.Name == itemName).FirstOrDefault();
if(itemInCurrentLanguage == null){
itemInCurrentLanguage = rootItem.Add(itemName, templateItem);
}
var itemInDestinationLanguage = currentDatabase.GetItem(itemInCurrentLanguage.ID, language );
if (itemInDestinationLanguage != null)
{
itemInDestinationLanguage.Fields.ReadAll();
itemInDestinationLanguage.Editing.BeginEdit();
try
{
//Add values for the fields
itemInDestinationLanguage.Fields["Translation"].Value = strTranslationValue;
}
catch (Exception)
{
//Log any error
}
finally
{
itemInDestinationLanguage.Editing.EndEdit();
}
}
}

You need to switch the language before you get the root item:
using (new LanguageSwitcher(language))
{
var rootItem = currentDatabase.GetItem(RootItemPath);
var item = rootItem.Add(selectedItem.Name, CommunityProjectTemplateId);
// Add new item here...
}

Related

Sitecore presentation details rendering datasource is empty

in the below code the datasource is coming as null for all the renderings present in presentation details, please help me where i am going wrong
LayoutDefinition layout =
LayoutDefinition.Parse(bioItem[Sitecore.FieldIDs.LayoutField]);
foreach (DeviceDefinition device in layout.Devices) {
if (device.Renderings != null) {
for(var i =0; i < device.Renderings.Count;i++) {
RenderingDefinition rendering = (RenderingDefinition)device.Renderings[i];
var result = rendering.Datasource;
}
}
}
First of all you should know there are two layout fields:
Shared: Sitecore.FieldIDs.LayoutField
Final: Sitecore.FieldIDs.FinalLayoutField
i usually use this code snippet to read directly from layout:
var devices = dataItem.Database.Resources.Devices;
var defaultDevice = devices.GetAll().First(d => d.Name.ToLower() == ScConstants.DefaultDeviceName);
var renderings = dataItem.Visualization.GetRenderings(defaultDevice, true);
foreach (var rendering in renderings)
{
if (!string.IsNullOrWhiteSpace(rendering?.Settings.DataSource))
{
var datasource = dataItem.Database.GetItem(rendering.Settings.DataSource, dataItem.Language);
.....
There are other ways to read the Layout field, but i suggest you to using the provided API and keep the layout XML parsing as the last resort.

Passing Multiple Values

I have a visualization table that has an event listener on select.
The need: I want the user to be able to delete documents on the google drive without having to leave the webpage
The set up: I added a button so that when clicked, I get a confirm alert box that includes the value. Once I click OK, it runs the scripts from the client-side with an event handler. This works perfectly!
The problem: I can move one document at a time but if I need to move 20+ documents it gets really tedious to click rows one after the other. Is it possible to pass multiple values to the successhandler?
google.visualization.events.addListener(archiveChart.getChart(), 'select', function () {
$("#docArchive").on("click", function() {
var selection = archiveChart.getChart().getSelection();
var dt = archiveChart.getDataTable();
if (selection.length > 0) {
var item = selection[0];
var docurl = dt.getValue(item.row, 2);
var docname = dt.getValue(item.row, 1);
var folder = dt.getValue(item.row, 4);
if(confirm("Are you sure you want to archive " + docname + "?") == true) {
archiveChart.getChart().setSelection([]);
return google.script.run.withSuccessHandler(onSuccessArchive).withFailureHandler(function(err) {
alert(err);
}).archiveDoc(docurl,folder);
} else {
archiveChart.getChart().setSelection([]);
}
}});
})
I feel like I might need to add this:
for (var i = 0; i < selection.length; i++) {
var item = selection[i];
I'm struggling a little with understanding what I might need to change (still learning). Any help or guidance is appreciated!
recommend confirming once, for all documents
then loop the selection to archive each document
google.visualization.events.addListener(archiveChart.getChart(), 'select', function () {
$("#docArchive").on("click", function() {
var selection = archiveChart.getChart().getSelection();
var dt = archiveChart.getDataTable();
var docNames = selection.map(function (item) {
return dt.getValue(item.row, 1);
}).join('\n');
if (selection.length > 0) {
if(confirm("Are you sure you want to archive the following document(s)?\n" + docNames) == true) {
for (var i = 0; i < selection.length; i++) {
var item = selection[i];
var docurl = dt.getValue(item.row, 2);
var docname = dt.getValue(item.row, 1);
var folder = dt.getValue(item.row, 4);
return google.script.run.withSuccessHandler(onSuccessArchive).withFailureHandler(function(err) {
alert(err);
}).archiveDoc(docurl, folder);
}
}
archiveChart.getChart().setSelection([]);
}
});
});

UISearchbar Search in all fields in swift 3

I've got a tableview showing some data and I filter the shown data uisng UISearchbar. Each data struct consists of different values and
struct Cake {
var name = String()
var size = String()
var filling = String()
}
When a user starts typing I don't know whether he is filtering for name, size or filling. I don't want to use a scopebar. Is there a way to filter for various fields at the same time in swift 3?
This is the code I use to filter:
func updateSearchResults(for searchController: UISearchController) {
if searchController.searchBar.text! == "" {
filteredCakes = cakes
} else {
// Filter the results
filteredCakes = cakes.filter { $0.name.lowercased().contains(searchController.searchBar.text!.lowercased()) }
}
self.tableView.reloadData()
}
thanks for your help!
func updateSearchResults(for searchController: UISearchController)
{
guard let searchedText = searchController.searchBar.text?.lowercased() else {return}
filteredCakes = cakes.filter
{
$0.name.lowercased().contains(searchedText) ||
$0.size.lowercased().contains(searchedText) ||
$0.filling.lowercased().contains(searchedText)
}
self.tableView.reloadData()
}

Sitecore How to Get Control's Data Source Value

Is it possible to get __Rendering control's template field value on content item?
Especially, I'd like to get "Data Source" field value defined in control on page item, like below screenshot.
As shown in screenshot, I have some controls in page item and I'd like to get control's "Data Source" field value.
I used this code and I could list all controls using on the page item. But, I don't know how to get the control's browsed data-source information on the page.
public RenderingReference[] GetListOfSublayouts(string itemId, Item targetItem)
{
RenderingReference[] renderings = null;
if (Sitecore.Data.ID.IsID(itemId))
{
renderings = targetItem.Visualization.GetRenderings(Sitecore.Context.Device, true);
}
return renderings;
}
public List<RenderingItem> GetListOfDataSource(RenderingReference[] renderings)
{
List<RenderingItem> ListOfDataSource = new List<RenderingItem>();
foreach (RenderingReference rendering in renderings)
{
if (!String.IsNullOrEmpty(rendering.Settings.DataSource))
{
ListOfDataSource.Add(rendering.RenderingItem);
}
}
return ListOfDataSource;
}
RenderingReference[] renderings = GetListOfSublayouts(targetItem.ID.ToString(), targetItem);
List<RenderingItem> ListOfDataSource = GetListOfDataSource(renderings);
This is exactly what I wanted.
Perfectly working!!!!!!
public IEnumerable<string> GetDatasourceValue(Item targetItem)
{
List<string> uniqueDatasourceValues = new List<string>();
Sitecore.Layouts.RenderingReference[] renderings = GetListOfSublayouts(targetItem.ID.ToString(), targetItem);
foreach (var rendering in renderings)
{
if (!uniqueDatasourceValues.Contains(rendering.Settings.DataSource))
uniqueDatasourceValues.Add(rendering.Settings.DataSource);
}
return uniqueDatasourceValues;
}
}
Here is a blog post that can help: Using the Data Source Field with Sitecore Sublayouts
Here's the relevant code you can call from within a single control:
private Item _dataSource = null;
public Item DataSource
{
get
{
if (_dataSource == null)
if(Parent is Sublayout)
_dataSource = Sitecore.Context.Database.GetItem(((Sublayout)Parent).DataSource);
return _dataSource;
}
}
Accesing the DataSource property defined above will give you the item that is assigned as the Data Source from the CMS.

in slickgrid how I can delete a row from a javascript function

how I can delete a row from a javascript function from a button
for example
If you're using a DataView, use the following:
DataView.deleteItem(RowID);//RowID is the actual ID of the row and not the row number
Grid.invalidate();
Grid.render();
If you only know the row number, you can get theRowID using:
var item = DataView.getItem(RowNum);//RowNum is the number of the row
var RowID = item.id
Suppose you are using jQuery
var grid;
$(function () {
// init options, load data
...
var columns = [];
columns[0] = {
id: 'id',
name: '#',
field: 'id', // suppose you have an id column in your data model
formatter: function (r, c, id, def, datactx) {
return 'X'; }
}
// init other columns
...
grid = new Slick.Grid($('#gridDiv'), data, columns, options);
}
function RemoveClick(databaseId, gridRow) {
// remove from serverside using databaseId
...
// if removed from serverside, remove from grid using
grid.removeRow(gridRow);
}
This is how i do it (not using any data provider though):
//assume that "grid" is your SlickGrid object and "row" is the row to be removed
var data = grid.getData();
data.splice(row, 1);
grid.setData(data);
grid.render();
I use this in a live project and it runs well. Of course, if you want to remove multiple rows then a few tweaks should be made, or if you use a data provider then you'd maybe want to remove the row only from the data provider and then have SlickGrid just refresh the rows.
Hope it helps :)
function deleteRows() {
var selectedIndexes = grid.getSelectedRows().sort().reverse();
var result = confirm("Are you sure you want to delete " + grid.getSelectedRows().length + " row(s)?");
if (result) {
$.each(selectedIndexes, function (index, value) {
var item = dataView.getItem(value); //RowNum is the number of the row
if (item)
dataView.deleteItem(item.id); //RowID is the actual ID of the row and not the row number
});
grid.invalidate();
grid.render();
}
}
var rowsToDelete = grid.getSelectedRows().sort().reverse();
for (var i = 0; i < rowsToDelete.length; i++) {
data.splice(rowsToDelete[i], 1);
}
grid.invalidate();
grid.setSelectedRows([]);
yes of course, I use it this way
var selrow = grid.getSelectedRows ();
data.splice(selrow, 1);
grid.invalidateAllRows();
grid.render ();
Greetings
hi
i'm used this script for delete row of SlickGrid
function deletefila(numrow)
{
alert("delete row"+numrow);
data.splice(numrow,1);
grid.removeAllRows();
grid.render();
//grid.removeRow(5);
//grid.updateRowCount();
//and then invalidate and re-render the grid by calling grid.removeAllRows() followed by grid.render().
}