If I create an item in sitecore from code behind using following code as shown in example under the heading Creating items:
//Now we can add the new item as a child to the parent
parentItem.Add("NewItemName", template);
And then in order to get the this newly added item from sitecore database what I should do? Because I don't know the ID.
The new item will be returned by the add method.
Eg.
Item newItem = parentItem.Add("NewItemName", template);
Related
I´ve a Homescreen with a list of items (List<book> listOfBooks). In a child widget I've a gridView creating Items of the widget BookItem().
My problem is when I'm passing BookItem() the parameter book, which is book itself and changing it in the widget BookItem it doesn't change the values of book in listOfBooks.
I hope that wasn´t to confusing. My question is: Is there being created a new instance not related to the listOfBooks or have I made any other mistakes?
If you take the book of listBooks like that:
listBooks.map((book){
return itemBook(book);
}).toList();
And into itemBook try to change the book, you need to update the item into listBook. This update the list correctly:
listBooks[indexBookSelected] = bookUpdated;
This doesnt work:
book = bookUpdated;
Of course after change the book need to call setState to update the view.
I have a list of items listed using iron-list.
The items in the iron-list are expandable, for every item there is an edition form.
There is also the possibility of creating a new item by pressing a general 'Add' button. Then, a new item will be added to the list using the method this.push('items', newItem).
The push of the new item works fine, the list is updated.
I would like that after pushing a new item the form corresponding to the item to expand immediately. So when the user want's to create a new item to see the creation form right away.
In order to expand the form for an item I simply grab the index of the iron-list from the DOM (item 0 index 0, item 1, index 1...), then I add a css class that will expand it.
The problem that I have is that between the pushing of the item and the calling of the method that will expand the item, the DOM isn't rendered yet so there is no index in the iron-list corresponding to the newly created item.
Is there a part of the element's lifecycle that I missed?
After research I didn't found methods for forcing the rendering of the iron-list or the refresh...
Could anyone give me an idea about how to expand the form immediately after pushing the item into the array?
This code expands whenever the the expanded variable equals the ID, requires unique IDs, could be name or whatever(I'd advise using whatever your database provides). Automatically expands the object by setting expanded equal to the ID of the recently created object.
<dom-module id="my-list">
<template>
<iron-list items="{{myItems}}">
<my-item
on-tap="select"
expaned="[[areEqual(item.id, expanded]]">
[[item.name]]
</my-item>
</iron-list>
.... add stuff ....
</template>
</dom-module>
<script>
(function() {
Polymer({
is: 'my-list',
properties: {
expanded: {
type: string,
value: ""
}
},
onAdd: function(adding){
... adding code...
this.expanded = this.adding.id;
},
areEqual: function(a, b){
return a === b;
},
select: function(e){
this.expanded = e.model.id;
}
})
}())
</scipt>
In sitecore, let's say I have an Item parentItem under it I want to create an item childItem which is based on Template templateItem, only when the childItem does not exist.
So the following code should only execute if the item not exist as child already.
parentItem.Add("Child Item", templateItem);
What do you mean saying item exists? Item with the same name? In Sitecore it is possible to have multiple items with the same item name. Those items in fact are not the same and have different IDs.
So in order to avoid creating another child item of the same template with the same name under the same parent, you need to upgrade your construction to the following:
string itemName = "Child item";
if(!dataItem.Children.Any(i=>i.Name == itemName && i.TemplateID == templateItem.ID))
{
dataItem.Add(itemName, templateItem);
}
Of course, it is not a good idea to have that clause from above everywhere, so you might want to automate that on every item creation. In order to do that, create your custom item:creating event handler with your custom one, like that:
<event name="item:creating">
<handler type="Your.Type, Your.Assembly.Name" method="OnItemCreating" />
And within OnItemCreating method, implement the same snippet from above.
Hope this helps!
I've created a custom field for sitecore Item, this custom field just creates a new Item with name specified under some specific folder. I do it on save (appropriate condition in onLoad method for custom field control):
if (!Sitecore.Context.ClientPage.IsEvent)
{
//blah-blah-blah
}
else
{
TemplateItem template = db.GetTemplate(TemplatePath);
Item parentItem = db.Items[ItemsRootPath];
var newItem = parentItem.Add("item name", template);
}
Item is creating successfully, but content editor automatically opens this item when I save a parent item (item that contains this custom field).
The question is: how I can avoid it and create this item in background, without displaying it to user?
Thanks,
A
You can disable Notifications which will prevent the new item being loaded like so:
Client.Site.Notifications.Disabled = true;
TemplateItem template = db.GetTemplate(TemplatePath);
Item parentItem = db.Items[ItemsRootPath];
var newItem = parentItem.Add("item name", template);
Client.Site.Notifications.Disabled = false;
I tested this by adding a child to the item that I was saving and resulted in the tree not being updated as well. I fixed that by adding this line after the notification enable.
Sitecore.Context.ClientPage.SendMessage(this, string.Format("item:refreshchildren(id={0})", ItemID));
How to enable the multilist to be control in content editor?
for example I have a list of item, item1 to item10. In the standard template value, I defined item1,2,3. After I have deploy the solution, how am I going to enable users in content editor mode or page editor mode to select item7,8,9 and 10?
And also, after I tested/rendered the multilist, only RAW VALUES are being rendered, is there any possible to render the item name such as item1? Do I need to customize the multilist?
The multilist control should be directly visible to the user in the Content Editor, you do not need to do anything else. Since you defined some items in standard values then those will be "pre-selected" when that item is first created. The user can then add the additional items as required.
To allow users to select values from the Page Editor you can Use Sitecore EditFrame in PageEdit
The reason the item is being rendered as the raw value is because you need to get the item and then iterate over the target id's. There is an example of this here here
//Get a multilist field from the current item
Sitecore.Data.Fields.MultilistField multilistField = Sitecore.Context.Item.Fields["myMultilistField"];
if (multilistField != null)
{
//Iterate over all the selected items by using the property TargetIDs
foreach (ID id in multilistField.TargetIDs)
{
Item targetItem = Sitecore.Context.Database.Items[id];
litItemTitle = targetItem.DisplayName;
// Do something with the target items
// ...
}
}
You can use the following instead for the datasource of a repeater
Sitecore.Data.Fields.MultilistField multilistField = Sitecore.Context.Item.Fields["myMultilistField"];
Sitecore.Data.Items.Item[] items = multilistField.GetItems();