How to create items programmatically in Sitecore - asp.net

In the content tree, the structure is as follows
Home
-England
-France
-Germany
There is a sublayout (CommentsForm.ascx), which is used in all the 3 pages. When user browses 'France' and submits a comment, the 'Comment' item should get saved under 'France' and so on..
In this scenario, the Parent item (under which the new item has to be created), is dynamic. So, how to get the parent item in such case. Is this correct?
protected void btnSubmit_Click(object sender, EventArgs e)
{
Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
Item parentItem = Sitecore.Context.Item;
string name = "Comment_" + Sitecore.DateUtil.IsoNow;
TemplateItem template = masterDb.GetTemplate("/sitecore/templates/userdefined/Comment");
using (new SecurityDisabler())
{
//how to go about here??
//newItem["Author"] = txtAuthor.text;
//newItem["CommentText"] = txtComments.Text;
//parentItem.Add("name", template);
}
}

You can use UserSwitcher safer in production, but you can also use SecurityDisabler using(newSecurityDisabler()){}
Editing and renaming must happen in an Editing.BeginEdit() transaction
Sitecore.Data.Database masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
Item parentItem = Sitecore.Context.Item;
string name = "Comment_" + Sitecore.DateUtil.IsoNow;
var template = masterDb.GetTemplate("/sitecore/templates/userdefined/Comment");
using (new Sitecore.SecurityModel.SecurityDisabler())
{
try
{
Item newItem = parentItem.Add("Name", template);
if (newItem!=null)
{
newItem.Editing.BeginEdit();
newItem["Author"] = txtAuthor.text;
newItem["CommentText"] = txtComments.Text;
newItem.Editing.EndEdit();
}
}
catch
{
newItem.Editing.CancelEdit();
}
}

Create items programmatically based on template in sitecore using the simple code
// The SecurityDisabler is required which will overrides the current security model, allowing the code
// to access the item without any security.
using (new Sitecore.SecurityModel.SecurityDisabler())
{
// Get the master database
Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master");
// Get the template for which you need to create item
Items.TemplateItem template = master.GetItem("/sitecore/templates/Sample/Sample Item");
// Get the place in the site tree where the new item must be inserted
Item parentItem = master.GetItem("/sitecore/content/home");
// Add the item to the site tree
Item newItem = parentItem.Add("NameOfNewItem", template);
// Set the new item in editing mode
// Fields can only be updated when in editing mode
// (It's like the begin transaction on a database)
newItem.Editing.BeginEdit();
try
{
// Assign values to the fields of the new item
newItem.Fields["Title"].Value = "NewValue1";
newItem.Fields["Text"].Value = "NewValue2";
// End editing will write the new values back to the Sitecore
// database (It's like commit transaction of a database)
newItem.Editing.EndEdit();
}
catch (System.Exception ex)
{
// Log the message on any failure to sitecore log
Sitecore.Diagnostics.Log.Error("Could not update item " + newItem.Paths.FullPath + ": " + ex.Message, this);
// Cancel the edit (not really needed, as Sitecore automatically aborts
// the transaction on exceptions, but it wont hurt your code)
newItem.Editing.CancelEdit();
}
}

using (new Sitecore.SecurityModel.SecurityDisabler())
{
Item newItem = parentItem.Add("Name", TemplateItem.TemplateId);
newItem.Editing.BeginEdit();
newItem.Fields[Constants.IDs.Fields.SicParent.Code].Value = row.SicCode.ToString();
newItem.Fields[Constants.IDs.Fields.SicParent.Description].Value = row.Description;
// this field is a DropList
newItem.Fields[Constants.IDs.Fields.SicParent.Grouping].SetValue(groupItem, true);
newItem.Editing.EndEdit();
}

You can add and item to parentItem and then edit the new item like this:
using (new SecurityDisabler())
{
Item newItem = parentItem.Add("name", template);
newItem.Editing.BeginEdit();
newItem["Author"] = txtAuthor.text;
newItem["CommentText"] = txtComments.Text;
newItem.Editing.EndEdit();
}

Related

Update dropdown after Items.Clear()

I populate a dropdown with several items. When the user chooses one of these items, a second dropdown gets populated.
When the user clicks on the "x" button on the first dropdown, the two dropdown must be cleared. The first dropdown gets cleared automatically, and i clear the second dropdown by using "dropdown.Items.Clear()".
It happens that when i load again the data for the first dropdown, the second dropdown does not update.
This is the code:
protected void DropDownDiagStati_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = this.DropDownDiagStati.SelectedIndex;
PopulateDDLStates(selectedIndex);
}
private void PopulateDDLStates(int selectedIndex)
{
// Ottengo i diagrammi di stato in sessione
ArrayList diagrammiStato = Session["stateDiagrams"] as ArrayList;
if(selectedIndex > 0)
{
// Ottengo il diagramma di stato selezionato
DocsPaWR.DiagrammaStato currDiagStato = (DocsPaWR.DiagrammaStato)diagrammiStato[selectedIndex - 1];
// Ottengo gli stati del diagramma di stato selezionato
Stato[] stati = currDiagStato.STATI;
for(int i = 0; i < stati.Length; i++)
{
ListItem item = new ListItem();
item.Value = Convert.ToString(stati[i].SYSTEM_ID);
item.Text = stati[i].DESCRIZIONE;
this.DropDownStati.Items.Add(item);
this.UpPanelStatiDdl.Update();
}
}
else
{
this.DropDownStati.Items.Clear();
this.UpPanelStatiDdl.Update();
}
}
I see only the old value in the second dropdown and i cannot select it.
You need to remove update pane or you can set data source to drop-down.
ddl1.datasource = DataSource;
ddl1.DataTextField = "name";
ddl1.DataValueField = "id";
ddl1.DataBind();
The problem was that the clear function removes also the default empty Item. I solved by creating a private method that removes all the items but the first one.

Sitecore: Show input option after using menu context item

I've added a menu context item to the TreelistEx. This menu item sends a messages that I later catch in a HandleMessage method.
In this method i create a new item ( template type and parent item are given in the source of the treelist field ).
All i need now is a way to ask the user for a name. But i haven't been able to find a simple way to do this.
class MyTreeListEx : TreelistEx, IMessageHandler
{
void IMessageHandler.HandleMessage(Message message)
{
if (message == null)
{ return; }
if (message["id"] == null)
{ return; }
if (!message["id"].Equals(ID))
{ return; }
switch (message.Name)
{
case "treelist:edit":
// call default treelist code
case "mytreelistex:add":
// my own code to create a new item
}
}
}
Does anyone have any suggestions on how to achieve this ?
Edit: added image & code + i'm using Sitecore 8 Update 1
I don't know which version of Sitecore you use but what you can try is SheerResponse.Input method.
You can use it like this:
using Sitecore.Configuration;
using Sitecore.Globalization;
using Sitecore.Shell.Applications.ContentEditor.FieldTypes;
using Sitecore.Web.UI.Sheer;
void IMessageHandler.HandleMessage(Message message)
{
...
case "mytreelistex:add":
Sitecore.Context.ClientPage.Start(this, "AddItem");
break;
}
protected static void AddItem(ClientPipelineArgs args)
{
if (args.IsPostBack)
{
if (!args.HasResult)
return;
string newItemName = args.Result;
// create new item here
// if you need refresh the page:
//SheerResponse.Eval("scForm.browser.getParentWindow(scForm.browser.getFrameElement(window).ownerDocument).location.reload(true)");
}
else
{
SheerResponse.Input("Enter the name of the new item:", "New Item Default Name", Settings.ItemNameValidation,
Translate.Text("'$Input' is not a valid name."), Settings.MaxItemNameLength);
args.WaitForPostBack();
}
}
This code will even validate your new item name for incorrect characters and length.

How to persist search criteria and results of a c# .NET application page

Scenario:
User submits search criteria and selects an item from search results on the same page, which navigates to a new page of details for the selected item.
When the User returns to the search screen, the search criteria & results (including selected page and sort-order) should be preserved from their last visit.
Related information:
All form submissions are POSTs.
Navigation back to the search screen may not be available from last browser history (e.g. more than one details screen may be encountered, or the user may navigate directly to the search screen from an alternative menu.)
Search results are provided using Telerik RadGrid control.
I'm looking for a generic solution that will be able to be applied to different search screens.
In some instances, the item may be DELETED from within the details screen, and should therefore not appear in the search results when the screen is next encountered.
Thoughts:
I've read a lot of suggested methods for addressing various parts of this scenario, but I'm still confused; no comprehensively "correct" solution jumps to the forefront.
I guess I'm asking for recommendations/approach rather than a whole solution spelled out for me (although that would be nice! ;-)
The .NET VIEWSTATE would seem to do exactly what I'm after (with the exception of #5) - Is there some way of leveraging off this so that viewstate can be used between pages, and not just between postbacks to the same page? (e.g. can I store/restore viewstate to/from a session variable or something? I haven't seen this suggested anywhere and I'm wondering if there's a reason why.)
Thanks in advance.
Thanks for all the advice.
For the benefit of others, here is a solution to this issue (no doubt there's room for improvement, but this works satisfactorily for the moment).
4 functions...
StoreSearchCookie - Persist the state/values of a panel of search criteria and a results grid in a cookie with a specified UID.
RestoreSearchCookie_Criteria - Read the cookie and re-populate the search criteira
RestoreSearchCookie_Results - Read the cookie and restore the grid state.
FindFormControls - Helper method to recursively find form-input controls in a container (pinched & modified from elsewhere on Stack Overflow)
NB...
I haven't addressed the multiple-tabs issue because our application doesn't allow them anyway.
RestoreSearchResults utilises GridSettingsPersister.cs available from Telerik website, (but I had to modify it to store the page number as well)
Usage is as follows...
protected void Page_PreRender(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
// Store the state of the page
StoreSearchCookie("SomeSearchPage", pnlSearchCriteria, gridResults);
}
else
{
// Restore search criteria
RestoreSearchCookie_Criteria("SomeSearchPage");
// Re-invoke the search here
DoSearch(); // (for example)
// Restore the grid state
RestoreSearchCookie_Results("SomeSearchPage");
}
}
Code follows...
protected void StoreSearchCookie(string cookieName, Panel SearchPanel, RadGrid ResultsGrid)
{
try
{
HttpCookie cookieCriteria = new HttpCookie("StoredSearchCriteria_" + cookieName);
// 1. Store the search criteria
//
List<Control> controls = new List<Control>();
FindFormControls(controls, SearchPanel);
foreach (Control control in controls)
{
string id = control.ID;
string parentId = control.Parent.ID;
string uid = string.Format("{0}>{1}", parentId, id);
string value = "";
Type type = control.GetType();
bool isValidType = true; // Optimistic!
if (type == typeof(TextBox))
{
value = ((TextBox)control).Text;
}
else if (type == typeof(DropDownList))
{
value = ((DropDownList)control).SelectedValue;
}
else if (type == typeof(HiddenField))
{
value = ((HiddenField)control).Value;
}
else if (type == typeof(RadioButton))
{
value = ((RadioButton)control).Checked.ToString();
}
else if (type == typeof(CheckBox))
{
value = ((CheckBox)control).Checked.ToString();
}
else
{
isValidType = false;
}
if (isValidType)
{
cookieCriteria.Values[uid] = value;
}
}
cookieCriteria.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(cookieCriteria);
// 2. Persist the grid settings
//
GridSettingsPersister SavePersister = new GridSettingsPersister(ResultsGrid);
HttpCookie cookieResults = new HttpCookie("StoredSearchResults_" + cookieName);
cookieResults.Values["GridId"] = ResultsGrid.ID;
cookieResults.Values["GridSettings"] = SavePersister.SaveSettings();
cookieResults.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(cookieResults);
}
catch (Exception exception)
{
Logger.Write(exception);
}
}
protected void RestoreSearchCookie_Criteria(string cookieName)
{
try
{
HttpCookie cookieCriteria = Request.Cookies["StoredSearchCriteria_" + cookieName];
if (cookieCriteria != null)
{
foreach (string key in cookieCriteria.Values.AllKeys)
{
string value = cookieCriteria[key];
string[] ids = key.Split('>');
string parentId = ids[0];
string id = ids[1];
Control control = FindControl(parentId).FindControl(id);
Type type = control.GetType();
if (type == typeof(TextBox))
{
((TextBox)control).Text = value;
}
else if (type == typeof(DropDownList))
{
((DropDownList)control).SelectByValue(value);
}
else if (type == typeof(HiddenField))
{
((HiddenField)control).Value = value;
}
else if (type == typeof(RadioButton))
{
((RadioButton)control).Checked = Boolean.Parse(value);
}
else if (type == typeof(CheckBox))
{
((CheckBox)control).Checked = Boolean.Parse(value);
}
}
}
}
catch (Exception exception)
{
Logger.Write(exception);
}
}
protected void RestoreSearchCookie_Results(string cookieName)
{
try
{
HttpCookie cookieResults = Request.Cookies["StoredSearchResults_" + cookieName];
if (cookieResults != null)
{
string gridId = cookieResults.Values["GridId"];
string settings = cookieResults.Values["GridSettings"];
RadGrid grid = (RadGrid)FindControl(gridId);
GridSettingsPersister LoadPersister = new GridSettingsPersister(grid);
LoadPersister.LoadSettings(settings);
grid.Rebind();
}
}
catch (Exception exception)
{
Logger.Write(exception);
}
}
private void FindFormControls(List<Control> foundSofar, Control parent)
{
List<Type> types = new List<Type> { typeof(TextBox), typeof(DropDownList), typeof(RadioButton), typeof(CheckBox), typeof(HiddenField) };
foreach (Control control in parent.Controls)
{
if (types.Any(item => item == control.GetType()))
{
foundSofar.Add(control);
}
if (control.Controls.Count > 0)
{
this.FindFormControls(foundSofar, control); // Use recursion to find all descendants.
}
}
}

Get Selected Text asp.net custom server control

I need to get the latest text set in the custom control by javascript. When i tried to get the selected text from server control, it is always returning the default text & not the modified text. How to retain the latest value set by the javascript in servercontrol? Below is the complete code for your reference..
ServerControl1.cs
[assembly: WebResource("ServerControl1.Scripts.JScript1.js", "text/javascript")]
namespace ServerControl1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
public class ServerControl1 : WebControl
{
public List<string> ListItems
{
get
{
return ViewState["items"] as List<string>;
}
set
{
ViewState["items"] = value;
}
}
public string Text
{
get
{
return (FindControl("middleDiv").FindControl("anchorID") as HtmlAnchor).InnerText;
}
set
{
((FindControl("middleDiv").FindControl("anchorID") as HtmlAnchor)).InnerText = value;
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
HtmlGenericControl selectedTextContainer = new HtmlGenericControl("div");
selectedTextContainer.ClientIDMode = System.Web.UI.ClientIDMode.Static;
selectedTextContainer.ID = "middleDiv";
HtmlAnchor selectedTextAnchor = new HtmlAnchor();
selectedTextAnchor.ClientIDMode = System.Web.UI.ClientIDMode.Static;
selectedTextAnchor.ID = "anchorID";
selectedTextAnchor.HRef = "";
selectedTextContainer.Controls.Add(selectedTextAnchor);
HtmlGenericControl unList = new HtmlGenericControl("ul");
foreach (string item in ListItems)
{
HtmlGenericControl li = new HtmlGenericControl("li");
HtmlAnchor anchor = new HtmlAnchor();
anchor.HRef = "";
anchor.Attributes.Add("onclick", "updateData()");
anchor.InnerText = item;
li.Controls.Add(anchor);
unList.Controls.Add(li);
}
selectedTextContainer.Controls.Add(unList);
Controls.Add(selectedTextContainer);
ChildControlsCreated = true;
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string resourceName = "ServerControl1.Scripts.JScript1.js";
ClientScriptManager cs = this.Page.ClientScript;
cs.RegisterClientScriptResource(typeof(ServerControl1), resourceName);
}
}
}
JScript1.js
function updateData() {
var evt = window.event || arguments.callee.caller.arguments[0];
var target = evt.target || evt.srcElement;
var anchor = document.getElementById("anchorID");
anchor.innerText = target.innerText;
return false;
}
TestPage Codebehind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<string> items = GetDataSource();
ServerControl1.ListItems = items;
ServerControl1.Text = "Select ..";
}
}
protected void ClientButton_Click(object sender, EventArgs e)
{
string selectedText = ServerControl1.Text;
}
The server won't get your client changes unless you POST the changes to him. Your HtmlAnchors are being rendered in HTML as <a> controls, and these type of controls won't POST anything to the server.
You're going to need an <input> control to input the changes into the server (that's why they're called input controls after all). I suggest an <input type=hidden> to hold the value of the anchor.innerText and keeps its state.
Your Javascript function needs to be modified so it updates the anchor.innerText AND updates the hidden input value as well. This way when the page gets posted back to the server you can retrieve the updated and client-modified value from the hidden field.
First you need to define as private fields your selectedTextAnchor and the hiddenField you are going to insert. This is because you need to access them in your CreateChildControls method as well as in the getter and setter of yout Text property. Much in the way the partial designer classes define the controls you want to have available in code-behind.
ServerControl.cs
private HtmlAnchor selectedTextAnchor;
private HtmlInputHidden hiddenField;
In the CreateChildControls method you need to insert the hidden field.
You'll notice I removed the use of ClientIDMode.Static. Using that mode would make your client controls to have the same fixed IDs and Javascript might get confused when you have multiple copies of your ServerControl in a page, and thus losing the reusable purpose of a custom control.
Instead, you need to provide your Javascript function with the ClientID's of the controls it needs to modify. The key here is that you need to attach your controls to the Control's hierarchy BEFORE you try to get their ClientID's.
As soon as you do this.Controls.Add(dummyControl), you're making dummyControl to become a part of the Page and its dummyControl.ClientID will be suddenly changed to reflect the hierarchy of the page you're attaching it into.
I changed the order at which your controls are attached to the Control's collection so we can grab their ClientID's at the time we build the onclick attribute and pass the parameters so your Javascript function knows which anchor and hiddenField to affect.
ServerControl.cs
protected override void CreateChildControls()
{
base.CreateChildControls();
// Instantiate the hidden input field to include
hiddenField = new HtmlInputHidden();
hiddenField.ID = "ANCHORSTATE";
// Insert the hiddenfield into the Control's Collection hierarchy
// to ensure that hiddenField.ClientID contains all parent's NamingContainers
Controls.Add(hiddenField);
HtmlGenericControl selectedTextContainer = new HtmlGenericControl("div");
// REMOVED: selectedTextContainer.ClientIDMode = System.Web.UI.ClientIDMode.Static;
selectedTextContainer.ID = "middleDiv";
selectedTextAnchor = new HtmlAnchor();
// REMOVED: selectedTextAnchor.ClientIDMode = System.Web.UI.ClientIDMode.Static;
selectedTextAnchor.ID = "anchorID";
selectedTextAnchor.HRef = "";
selectedTextContainer.Controls.Add(selectedTextAnchor);
// Insert the selectedTextContainer (and its already attached selectedTextAnchor child)
// into the Control's Collection hierarchy
// to ensure that selectedTextAnchor.ClientID contains all parent's NamingContainers
Controls.Add(selectedTextContainer);
HtmlGenericControl unList = new HtmlGenericControl("ul");
foreach (string item in ListItems)
{
HtmlGenericControl li = new HtmlGenericControl("li");
HtmlAnchor anchor = new HtmlAnchor();
anchor.HRef = "";
// The updateData function is provided with parameters that will help
// to know who's triggering and to find the anchor and the hidden field.
// ClientID's are now all set and resolved at this point.
anchor.Attributes.Add("onclick", "updateData(this, '" + selectedTextAnchor.ClientID + "', '" + hiddenField.ClientID + "')");
anchor.InnerText = item;
li.Controls.Add(anchor);
unList.Controls.Add(li);
}
selectedTextContainer.Controls.Add(unList);
}
Note the use of the keyword this in the updateData function, it'll help us to grab the object that is triggering the action. Also note that both Id's are passed as strings (with single quotes)
The Javascript function would need to be modified so it updates the anchor and the hidden input field.
JScript1.js
function updateData(sender, anchorId, hidFieldId) {
// Update the anchor
var anchor = document.getElementById(anchorId);
anchor.innerText = sender.innerText;
// Update the hidden Input Field
var hidField = document.getElementById(hidFieldId);
hidField.value = sender.innerText;
return false;
}
The last thing to do is change the way you are setting and getting your Text property.
When you GET the property you need to check if it's a Postback, and if it is, then you want to check if among all the info that comes from the browser there is your HiddenInputField. You can grab all the info coming from the client right at the Request object, more specifically, in the Request.Form.
All enabled input controls on your page will be part of the Request.Form collection, and you can get their values by using Request.Form[anyInputControl.UniqueID]. Note that the key used for this object is the UniqueID, NOT ClientID.
Once you get your client-modified value from the hidden input, you assign its value to the selectedTextAnchor, otherwise it'll go back to the original "Select..." text.
When you SET the property, you just need to assign it to the selectedTextAnchor.
In both GET and SET you need to call EnsureChildControls(), which will actually call your CreateChildControls() to make sure that your selectedTextAnchor and hiddenField controls are instantiated before you try to get some of their properties. Pretty much the same way that it's done in Composite Controls.
ServerControl.cs
public string Text
{
get
{
EnsureChildControls();
if (this.Page.IsPostBack)
{
string HiddenFieldPostedValue = Context.Request.Form[hiddenField.UniqueID];
// Assign the value recovered from hidden field to the Anchor
selectedTextAnchor.InnerText = HiddenFieldPostedValue;
return HiddenFieldPostedValue;
}
else
{
return selectedTextAnchor.InnerText;
}
}
set
{
EnsureChildControls();
selectedTextAnchor.InnerText = value;
}
}
This way you can have a control that recognizes the changes made in client. Remember that server won't know any change in client unless you notice him.
Another approach would be to notice the server everytime you click a link through an ajax request, but this would require a whole new different code.
Good luck!

How do I Immediately Validate a Newly Inserted Row in a Silverlight 3 Datagrid?

I have a Silverlight 3 tools library with a custom DataGrid user control. This grid has no direct access to the WCF RIA Services entity types so I'm using reflection to add a new item when the user clicks on the grid when it's empty:
private void InsertEmptyRecord()
{
if (this._dataGrid.ItemsSource == null)
return;
Type[] typeParameters = this._dataGrid.ItemsSource.GetType().GetGenericArguments();
if (typeParameters.Count() > 0)
{
Type itemType = typeParameters[0];
object newItem = System.Activator.CreateInstance(itemType);
Type sourceType = typeof(System.Windows.Ria.EntityCollection<>);
Type genericType = sourceType.MakeGenericType(itemType);
System.Reflection.MethodInfo addMethod = genericType.GetMethod("Add");
addMethod.Invoke(this._dataGrid.ItemsSource, new object[] { newItem });
// == Validate data here ==
}
}
This works, but I need it to also validate after the new item is added. There are two ways I can see to do this:
Force the user to enter edit mode
for the first cell of the new row in
the grid. (This would force
validation if they click anywhere
else on the page.)
Force validations
to run immediately when the new row
is added (or when the grid looses
focus.)
I haven't been able to get either of these to work. Tried this but it only selects the row, doesn't force the validations to run:
this._dataGrid.SelectedItem = newItem;
System.ComponentModel.IEditableObject editableItem = newItem as System.ComponentModel.IEditableObject;
if (editableItem != null)
editableItem.BeginEdit();
Any suggestions?
Just got this working thanks to some help from this question.
I added the following to the "== Validate data here ==" section in the code from above:
DataGridRow newRow = this._dataGrid.ChildrenOfType<DataGridRow>().FirstOrDefault();
if (newRow != null)
{
newRow.Loaded += (sender, e) =>
{
this._dataGrid.CurrentItem = newItem;
this._dataGrid.BeginEdit();
};
}
This forces the first cell to immediately go into edit mode.

Resources