Trigger Sitecore event programmatically - c#

My sitecore website subscribes to the OnSave event of the content change. When I log into the content manager and change any of the fields, then the event is successfully triggered on the website. Now, I'm trying to do the same thing, except trigger this event from a standalone console app.
The code below changes the content and I'm able to see the change on the website, but the OnSave event is not triggered. The edit context is not silent, so it should trigger the event but it isn't.
I also checked the event quote database and the new event is being created but its just not being triggered on the web. Does anyone have a clue on why the event is not being triggered?
Pipeline.Start("initialize", new PipelineArgs(), true);
// get database
string domainUser = #"sitecore\admin";
if (Sitecore.Security.Accounts.User.Exists(domainUser))
{
Sitecore.Security.Accounts.User user =
Sitecore.Security.Accounts.User.FromName(domainUser, false);
using (new Sitecore.Security.Accounts.UserSwitcher(user))
{
var db = Factory.GetDatabase("master");
Item ii = db.SelectSingleItem("/sitecore/content/Home");
using (new EditContext(ii, true, false))
{
String current = ii.Fields["Breadcrumb Title"].Value;
Console.WriteLine("Current value: " + current);
current += " e";
Console.WriteLine("Changing to: " + current);
ii.Fields["Breadcrumb Title"].Value = current;
}
}
}

The use of EditContext is deprecated. The correct and supported way to edit items is to use Item.Editing.BeginEdit() and Item.Editing.EndEdit(). First thing to try is to switch to those, and see if it makes a difference.
Meanwhile; you say the event is not being triggered "on the web". Are you in a multi-server setup? If so, you need to consider the remote events, as "item:saved" would only trigger on the actual server where the event was triggered.

Related

Creating Dynamic Controller Names in C# with WinForms for TableLayoutPanel

While working on a small app that pulls test cases, runs, and results from an SQL Server Database, I encountered a dilemma in my methodology for attempting to create dynamic controller names in a TableLayoutPanel in WinForms. I am creating the rows dynamically when the user chooses the particular test case, and from there the TableLayoutPanel will open another window with the test steps preloaded and two radio buttons to indicate whether or not the test passed. My issue is that when I select one of the radio buttons on the right of the step, I get the same console read every single time. I need to be able to determine which exact radio button the user has pressed so I can therefore determine what row it's in and subsequently what test either passed or failed. My main code is as follows:
FormManualTest.cs (section when adding to the TableLayoutPanel)
private void addRowToolStripMenuItem_Click(object sender, EventArgs anotherEvent)
{
tableLayoutTest.RowStyles.Clear(); // Clear row styles to ensure a clean start when adding to the TableLayoutPanel
List<RadioButton> listOfRadioControls = new List<RadioButton>(); // Create array of radio buttons
List<UserCustomStep> listOfStepControls = new List<UserCustomStep>(); // Create array of custom controls
for (int i = 0; i < 5; i++)
{
UserCustomStep step = new UserCustomStep(Counter, "Step: " + i + " Push the button to elicit a response."); // Creates new user custom step control instance
RadioButton pass = new RadioButton();
pass.Text = "Pass";
pass.AutoSize = true;
RadioButton fail = new RadioButton();
fail.Text = "Fail";
fail.AutoSize = true;
fail.Margin = new Padding(3,3,20,3); // Needed to see the fail button without having to scroll over
listOfStepControls.Add(step); // Add step to UserCustomStep array
listOfRadioControls.Add(pass); // Add radio buttons to the RadioButton array
listOfRadioControls.Add(fail);
listOfRadioControls[i * 2].CheckedChanged += (s, e) => // Subscribes the pass radio button to listen for when a user has clicked on it
{
Console.WriteLine("Pass " + i + " was clicked");
};
listOfRadioControls[(i * 2) + 1].CheckedChanged += (s, e) => // Subscribes the fail radio button to listen for when a user has clicked on it
{
Console.WriteLine("Fail " + i + " was clicked");
};
tableLayoutTest.Controls.Add(listOfStepControls[i], 0, i); // Adds CustomStep to first column
tableLayoutTest.Controls.Add(listOfRadioControls[i*2], 1, i); // Adds Pass Radio Button to second column
tableLayoutTest.Controls.Add(listOfRadioControls[(i * 2) + 1], 2, i); // Add Fail Raido Button to third column
Counter++; // Increment couter to add subsequent steps underneath the previous ones.
}
}
Screenshots of App with Console Readout:
After Test Case Has Been Clicked and Radio Button Has Been Pressed
(From clicking this I would expect the console to read "Pass 1 was clicked")
Console Read:
Click Fail Button:
(I know from this image below that since the Pass button doesn't remain clicked I'm somehow using the same controller for all 5 of them)
Console Read
So from all of these issues that I've been presented with, I know that I'm somehow using the same controller for all 5 instances regardless of the fact that I'm storing everything in a controller array and grabbing from there. The for loop will have to be converted to a for each loop later, but that still doesn't solve my issue. I believe that if I could say something like:
RadioButton (pass+id) = new RadioButton();
or something similar while looping through to dynamically create the name for the controls, then each one would be a completely separate control and I could go from there. Any help would be greatly appreciated! I come from a heavy web background so my normal skills to remedy this in JS land aren't coming in handy as of right now. Thanks again for the assistance.
The Name property is optional, you don't need to specify it and it doesn't need to be unique. You can use property Tag for your own purpose (you can assign there ID or event instance of some object).
However you can also create your own control/usercontrol which encapsulate the whole row, and you can declare your own properties exactly for your purpose.

Drag&Drop File Attribute is ReadOnly

I created an Attached Property which registers the drag and drop events and requests the operations: move, link, copy.
UIElement dragablecontrol = d as UIElement;
if (dragablecontrol != null)
{
dragablecontrol.AllowDrop = true;
dragablecontrol.DragEnter += Dragablecontrol_DragEnter;
dragablecontrol.DragStarting += Dragablecontrol_DragStarting;//does not get fired
dragablecontrol.DragOver += Dragablecontrol_DragOver; //e.AcceptedOperation got move, link, copy
dragablecontrol.Drop += Dragablecontrol_Drop; //e.DataView.RequestedOperation is set none
}
Anyway the starting drag event is not fired and my RequestedOperation are ignored because e.Data is null in drag enter.
Therefore (I guess) the event argument parameter e.DataView.RequestedOperation is set to None in the drop event. The file attributes I get with var filesAndFolders = await e.DataView.GetStorageItemsAsync(); are set to ReadOnly.
What can I do about that. I need to rename the dragged files. I created a demo project on GitHub.

ItemChangeEvent fired multiple times

I work on Outlook addin that is connected to an Owncloud (Caldav, Cardav).
I use eventhandlers to detect when a user delete, create or update a contact item.
In this case this will notify the server and do an update on his side.
storage.Aitems[storage.Aitems.Count - 1].ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(contact_item_add);
storage.Aitems[storage.Aitems.Count - 1].ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(contact_item_change);
((MAPIFolderEvents_12_Event)storage.Afolders[storage.Afolders.Count - 1]).BeforeItemMove += new MAPIFolderEvents_12_BeforeItemMoveEventHandler(contact_item_before_move);
When i find new contacts items from the server i add them right the addressbook folder. Outlook detect a new item or a update and then fire the eventshandlers. This is good in case there is only one call (I can say this from the server dont notify again the server or this is from the user notify the server) but i can't because the events are fired multiple times.
static void contact_item_change(object Item) {
Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)Item;
System.Diagnostics.Debug.WriteLine("[Modification contact]" + contact.FullName);
// Need to know if item was created by code (server) or user
Main.SyncContact(contact);
}
Is it possible to know if an item is created trough the GUI or in my code ?
I can't set a variable to know if it was created by user or by my code because of the multiple calls of events.
BTW i already succeed to fire add and delete only one time :
static void contact_item_add(object Item) {
Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)Item;
if (Contact.GetCreatedByUserProperty(contact, "USER")) {
if (storage.PreviousAddedContactId != contact.EntryID)
{
System.Diagnostics.Debug.WriteLine("[Ajout contact]" + contact.FullName);
Main.SyncContact(contact);
storage.PreviousAddedContactId = contact.EntryID;
}
}
}
static void contact_item_before_move(object item, MAPIFolder destinationFolder, ref bool cancel) {
if ((destinationFolder == null) || (IsDeletedItemsFolder(destinationFolder))) {
ContactItem contact = (item as ContactItem);
if (storage.PreviousDeletedContactId != contact.EntryID)
{
System.Diagnostics.Debug.WriteLine("[Suppression du contact]" + contact.FullName);
Main.DeleteContact(contact);
storage.PreviousDeletedContactId = contact.EntryID;
}
}
}
Thank you !
You can read MailItem.LastModificationTime immediately after calling MailItem.Save. When ItemChange event fires, you can compare the new modification time with what you cached and check if it is greater than some delta (1 second?).
In case of Exchange store, you can also retrieve PR_CHANGE_KEY (DASL name http://schemas.microsoft.com/mapi/proptag/0x65E20102) property - it will change after each modification.
Is it possible to know if an item is created trough the GUI or in my code ?
No, there is no property or method which can help in identifying the way which was used to create an Outlook item. But you may add a user property which may indicate that the item was created by your code programmatically. User created item will not have this property set.
I followed the answer of Dmitry Streblechenko
In fact ContactItem have a LastModificationTime property. I don't know if i'm wrong but i guess outlook never change this Datetime when he makes changes. So it's easy to check if user have touch the contact. That allow me to notify the server and it works very well ! Thank you !
static void contact_item_change(object Item) {
Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)Item;
// Need to know if item was created by code (server) or user
var seconds = (DateTime.Now - contact.LastModificationTime).TotalSeconds;
if (seconds < 2) {
System.Diagnostics.Debug.WriteLine("[Modification contact]" + contact.FullName);
Main.SyncContact(contact);
}
}
You only need to focus on properties you want on that Item. When ItemChange event
fired, you can compare properties of that Item with your server Item, if they are difference => do Main.SyncContact(contact);

ListBox SelectionChanged WP7 to navigate with parameters

So, in the Windows Phone 7 app I'm making, I use a ListBox with a SelectionChanged event handler to navigate a user to a new webpage, showing additional information. The MainPage.xaml shows a ListBox populated with information from a JSON file, which works correctly. However, if a user wants to read more about the news, he/she will have to click on the news in the ListBox, which fires the SelectionChanged event, which looks like this:
private void NewsList_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
int index = NewsList.SelectedIndex;
fetchNewsContent newsContentGetSet = new fetchNewsContent();
newsContentGetSet.newsID = newslistJson.ElementAt(index).news_id;
newsContentGetSet.newsTitle = newslistJson.ElementAt(index).news_title;
newsContentGetSet.newsAbstract = newslistJson.ElementAt(index).news_abstract;
newsContentGetSet.newsContent = newslistJson.ElementAt(index).news_content;
newsContentGetSet.newsAuthor = newslistJson.ElementAt(index).news_author;
newsContentGetSet.newsDatePublished = newslistJson.ElementAt(index).news_date_published_no;
//object[] someobject = { newsContentGetSet.newsID, newsContentGetSet.newsTitle, newsContentGetSet.newsAbstract, newsContentGetSet.newsContent, newsContentGetSet.newsAuthor, newsContentGetSet.newsDatePublished };
NavigationService.Navigate(new Uri("/NewsPage.xaml?obj=" + index, UriKind.Relative));
}
This simply uses a class (newsContentGetSet.cs) with getters and setters for each of the strings (newsID, newsTitle, etc.), but when the SelectionChanged is fired, it the .cs file doesn't set the newly given newslistJson values! Why?
I also tried sending only text parameters in the NavigationService, but the newsContent string was too long (whole news story), so it returned an "shell page uri too long" error.
Right now, this sends simply the index int to the NewsPage page, which tries to capture the values, but fails since the newsContentGetSet doesn't actually set anything (doesn't debug into it when I try). Aaany ideas, really?
Instead of passing the data on parameter. You should save the data to variable into App class and then retrieve them from there when you have navigated to next page.
App.xaml.cs
public static fetchNewsContent newsContentGetSet;
Accessing it
var fetchedNewsContent = App.fetchNewsContent;
You can store/retrieve the data from any page. Note that if the application is closed the data is gone.

Dynamically Added Event Handler Not Firing

Here is a quick code snippet, that doesn't seem to work at all for me. I'm reading from a file to create a list of radio buttons. The problem is that when one of the radio buttons is clicked the Event Handler I have set up in the code doesn't fire. I have tested it over and over in debug mode with line breaks... all with no luck. Am I missing something obvious here????
strLine = strLine.Trim();
System.Diagnostics.Debug.WriteLine("[3-a] ship by date - date: " + strLine);
try{ shipByDate = (Convert.ToDateTime(strLine)); }
catch (Exception e) { shipByDate = new DateTime(); }
shipByDesc = sr.ReadLine().Trim();
System.Diagnostics.Debug.WriteLine("[3-b] ship by date - desc: " + shipByDesc);
RadioButton button = new RadioButton();
button.Text = shipByDesc + " - " + shipByDate.ToString("MM/dd/yyyy");
button.Checked = false;
button.GroupName = "shipByOptions";
button.ID = "shipByRadio" + count;
//button.EnableViewState = true;
button.AutoPostBack = true;
button.CheckedChanged += new EventHandler(shipBy_CheckedChanged); // <-- doesn't work!!!
//form1.Controls.Add(button);
shipByPlaceHolder.Controls.Add(button);
You need to add the button on every postback before events attached to it will fire.
If you think about it for a moment, it will make sense - if the button has not been created (on the postback), then there are no button events that can fire. The button must exist before events attached to it can be fired.
The OnInit page event is the most suitable place to add dynamic controls to a page.
Read about the asp.net page life cycle.

Categories