Item Adding in event receiver sharepoint 2010 - c#

public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
SPItem itemBeingAdded = properties.ListItem;
var startTime = itemBeingAdded["Start Time"];
// Some code goes here.
}
I am firing this event while adding an item in a calender list. It is getting fired. No problem. But I am not getting any value from properties. In the above code startTime gives me nothing. Actually, I want to access the column field of the item (properties in my case) is being added.
When the user will click in save button, How can I get the column value in code behind (Inside the ItemAdding(SPItemEventProperties properties) method). Let say, I need Start Time and End Time to compare them with some other values. Problem is in the ItemAdding method. ItemUpdating is working fine.
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
SPItem itemBeingAdded = properties.ListItem;
var startTime = itemBeingAdded["Start Time"];
}

Finally, I got my answer from here. The way would be like following
string message = properties.AfterProperties["Description"].ToString();
Another thing is you should use internal name. For my case, display name didn't work.

Related

How to get current item from Spinner without listener - c# android xamarin

I am trying to get the item that is currently in the spinner, but not by Selected or OnChange events.
public string GetCurrentSport()
{
string currentSport = spnSports.GetItemAtPosition(0).ToString();
return currentSport;
}
the above code throws and error, I am guessing it is the wrong code to get the Spinner value, this is getting passed to another class from a button click so i can't use the OnSelected Events.
If you need more code, please ask.
A Spinner via its subclassed AdapterView has three methods to obtain the "item" that is currently selected, SelectedItem, SelectedItemId, SelectedItemPosition:
// The data corresponding to the currently selected item, or null if there is nothing selected.
var javaObj = spinner.SelectedItem; // getSelectedItem
//The id corresponding to the currently selected item, or INVALID_ROW_ID if nothing is selected.
var id = spinner.SelectedItemId; // getSelectedItemId
// Return the position of the currently selected item within the adapter's data set
var postion = spinner.SelectedItemPosition; // getSelectedItemPosition
re: https://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItem()

TFS workitem field cannot be validated in Eval

I have a TFS work item field that seems to exist for most work items, but sometimes doesn't.
I'm attempting to bind these workitems in a listview using a WorkItemCollection, and everything works except when I attempt binding that field.
The field itself is specified in the query select and gives no error. However the only way to get the field without throwing an error on some workitems is as so (in the .cs)::
foreach(WorkItem w in queryResults)
{
if (w.Fields.Contains("Symptom"))
{
w.Fields["Symptom"].Value.ToString();//show
}
}
as these workitems are in readonly mode, I can't just force write the value etc.. (unless I return a Dictionnary with the value paired with the workitem, but I don't think that would be the best solution...)
What I'm attempting to do is in the Listview ItemTemplate, something like this::
<div class="details">
<%# Server.HtmlEncode(Eval("Fields.Contains(\"Symptom\")? Fields[\"Symptom\"].Value : \"\";").ToString())%>
</div>
But I get the 'Contains' is not a valid 'Fields' property (because its a collection method) - I'm stuck with the eval
How can I properly evaluate and display my Symptom field?
If you use TFS 2015 or VSTS, you can use REST API to get Symptom field. The API look like:
GET http(s)://{instance}/DefaultCollection/_apis/wit/workitems?ids=xx&fields=Microsoft.VSTS.CMMI.Symptom&api-version=1.0
Or with .net Api:
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Client;
using System;
namespace TestCaseProject
{
class Program
{
static void Main(string[] args)
{
var tfs =
TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
new Uri("http://tfsserver:8080/tfs/CollectionName"));
var service = tfs.GetService<WorkItemStore>();
var wi = service.GetWorkItem(id);
foreach (Field field in wi.Fields)
{
Console.WriteLine("{0}: {1}", field.Name, field.Value);
}
}
}
}
Use OnItemDataBound
Nothing seemed to work directly on the page so I moved away from that approach and went with attempting to solve my problem in the codebehind, since I was already capable of sorting it out on that front.
I removed my sourceObject from my page and instead had the codebehind programmatically databind my listview. (this step is not necessary)
TFSListView.DataSource = SearchHandler.SearchTFS(searchstring);
TFSListView.DataBind();
Then I used OnItemDatabound event to be able to manage each item being bound, I check if that annoying symptom field exist for the dataitem being bound and fill up the appropriate control if it does (all this replaces one eval line, so I'm still a bit annoyed but it didn't seem to be possible directly in the aspx page)
protected void TFSListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem myitem = (ListViewDataItem)e.Item;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Control divDetails = e.Item.FindControl("divLsvDetails");
WorkItem myWI = myitem.DataItem as WorkItem;
if (myWI != null && divDetails != null)
{
if (myWI.Fields.Contains("Symptom"))
{
((HtmlGenericControl)divDetails).InnerHtml = myWI["Symptom"].ToString();
}
}
}
}

Weird SharePoint ItemUpdating Behavior

I have a SharePoint list where I register a custom ItemUpdating event receiver but I am seeing some really strange behavior in this solution. This behavior occurs if I add any code to the event receiver other than base.ItemUpdating.
What happens is if I debug the event receiver I can see that properties.AfterProperties has all the values entered on the field and properties.ListItem has the original item. But once the ER finishes running and the page reloads nothing is saved and it just returns to what it was before I changed values. Even more weird, if I go and manually set the after properties similar to below it works and the updates are saved correctly. So basically the event receiver is making me responsible to do any changes to the item but this is not normal behavior for ItemUpdating. Does anyone have any idea what might cause this?
public override void ItemUpdating(SPItemEventProperties properties)
{
var recurringBefore = properties.ListItem.TryGetValue<bool>(Constants.CommonFields.Recurring_STATIC);
var recurringAfter = Convert.ToBoolean(properties.AfterProperties[Constants.CommonFields.Recurring_STATIC]);
//This loop is the horrible fix I have done to manually update the relevant fields but this shouldn't be necessary
var item = properties.ListItem;
foreach (SPField key in item.Fields)
{
if (item[key.InternalName] != properties.AfterProperties[key.InternalName] && key.CanBeDisplayedInEditForm && properties.AfterProperties[key.InternalName] != null)
{
//looping through and setting the AfterProperties to what they already are makes them save? If I don't do this nothing saves
properties.AfterProperties[key.InternalName] = properties.AfterProperties[key.InternalName].ToString();
}
}
if (!recurringBefore && recurringAfter &&
currWfStatus == Constants.WorkflowStatus.Processed)
{
//do some stuff
}
base.ItemUpdating(properties);
}
Is it because you are not saving current item at all, something like this:
item.update();

ObjectListView editing doesn't work

I'm trying to create a simple listbox with ObjectListView (WinForm, C#). The goal is to have a single value (a double) and a check box.
I want to be able to edit the double value by Single Click, so here are the relevant lines of code from my MyWindow.Designer.cs file (i've left out the default values for efficiency):
this.olvDepths = new BrightIdeasSoftware.ObjectListView();
this.olvColumn1 = ((BrightIdeasSoftware.OLVColumn)(new BrightIdeasSoftware.OLVColumn()));
...
this.olvDepths.CellEditActivation = BrightIdeasSoftware.ObjectListView.CellEditActivateMode.SingleClick;
this.olvDepths.CheckBoxes = true;
this.olvDepths.CheckedAspectName = "IsDefault";
this.olvDepths.FullRowSelect = true;
//
// olvColumn1
//
this.olvColumn1.AspectName = "Depth";
this.olvColumn1.Text = "";
this.olvColumn1.IsEditable = true;
I then create a list of my class (ShieldingEntry) and use the olvDepths.SetObjects() with the list. My ShieldingEntry class looks like this:
public class ShieldingEntry
{
public double Depth { get; set; }
public bool IsDefault { get; set; }
}
However, when I click the field, it doesn't go into edit mode. I've also tried the DoubleClick, SingleClickAlways, and F2Only modes and they don't work either.
The Checkbox works fine.
************** I have additional information *********************
I've pulled and build the ObjectListView source, so I could step through it.
I put a breakpoint in the OLV StartCellEdit method and it gets called and appears to setup and select the control appropriately. It just never appears...
As I noted in the comments on the answer below, I've got this control on a tabbed dialog, and if I switch to another tab, then back, the control works fine.
What am I missing?
I've used ObjectListView before, and here is what I had to do:
Handle the CellEditStarting event. This event is raised when the cell goes into edit mode. Since OLV doesn't really have built-in editors, you have to make your own. Then handle the CellEditFinishing event to validate the data before putting it back into your model.
So first, handling the CellEditStarting event:
private void objlv_CellEditStarting(object sender, CellEditEventArgs e)
{
//e.Column.AspectName gives the model column name of the editing column
if (e.Column.AspectName == "DoubleValue")
{
NumericUpDown nud = new NumericUpDown();
nud.MinValue = 0.0;
nud.MaxValue = 1000.0;
nud.Value = (double)e.Value;
e.Control = nud;
}
}
This creates your editing control. If you want to make sure the size is right, you can set the size of the control (in this case a NumericUpDown) to the cell bounds using e.CellBounds from the event object.
This will show the editor when you click in the cell. Then you can handle the editor finished event to validate the data:
private void objlv_CellEditFinishing(object sender, CellEditEventArgs e)
{
if (e.Column.AspectName == "DoubleValue")
{
//Here you can verify data, if the data is wrong, call
if ((double)e.NewValue > 10000.0)
e.Cancel = true;
}
}
I don't think handling it is required, but its good practice to validate data from the user.
The editing control in the CellEditStarting event can be any control, even a user defined one. I've used a lot of user defined controls (like textboxes with browse buttons) in the cell editor.
[Edit]
I uploaded an example here dropbox link that seems to work. Might not be in the exact view as needed, but seems to do the job.
For anyone else with this problem. I had it specifically when trying to edit a 'null' value in a decimal? on the OLV on a tab page. Solution for me was to set UseCustomSelectionColors to 'False'. I didn't look elsewhere to see if it was reported as a bug. Seems like a bug.

silverlight, ListBox navigation to new page with object possible?

Hi all i have a listbox MainListBox where i add items to dynamically.
Now i want to navigate to DetialsPage.xaml.cs when i choose an item in the listbox.
where i can then display my info about the selected item.
private void SetListBox()
{
foreach (ToDoItem todo in itemList)
{
MainListBox.Items.Add(todo.ToDoName);
}
}
MainListBox_SelectionChanged ("Generated by visual studio 2010 silverlight for windows 7 phone)
// Handle selection changed on ListBox
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (MainListBox.SelectedIndex == -1)
return;
// Navigate to the new page
NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));
// Reset selected index to -1 (no selection)
MainListBox.SelectedIndex = -1;
}
in DetailsPage.xaml.cs is the next method. ("Generated by visual studio 2010 silverlight for windows 7 phone)
I'm aware that the below method does not do what i try.
// When page is navigated to set data context to selected item in list
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string selectedIndex = "";
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
int index = int.Parse(selectedIndex);
DataContext = App.ViewModel.Items[index];
}
}
I would like to access the selectedIndex and call my methods of my object that is in the MainListbox
so Basicly:
Mainlistbox => select item => send that item to details page => details page access the item and call methods on the item (object)
I'm sure this is a basic question tough it seems hard to find any specifics on it. i would like to add that this is my first windows phone 7 app.
There are many ways you can pass an object from page to page:
serialize and deserialize like Dennis said, but this, although feasable, is not practical, unless you want to save the object in isolated storage and retrieve it later.
Place an object in the App.cs class, which is accessible to all pages. Set your object in the master page, retrieve it from the Details page.
Code to put in App.cs: MyObject selectedObject;
Code to put in MasterPage.cs: application.selectedObject = MainListBox.selectedItem;
Code to put in DetailsPage.cs: MyObject selectedObject = application.seletedObject;
You can set the Object in the DataContext of your LayoutRoot, but i don't have the code for that on top of my head.
The answer here is simple - you cannot directly pass an object to another page. You can serialize it to JSON or XML and then deserialize it on the target page, but the serialized item will still have to be passed as a parameter.
Instead of sending the selectedindex as a query string parameter you could send the ID for the object or similar, something that uniquely can identify the object.
Then in the details page you could fetch the correct object from the same datasource that the main list box get its data from (in your case "itemList" which could come from e.g. IsolatedStorage).
If itemList is instantiated and kept only within the main page then you won't be able to fetch the item by ID from the details page. So in that case you'd need to move the itemList to some static or app level storage.
HTH

Categories

Resources