ItemUpdating called twice after ItemAdded in event receiver - sharepoint-2010

I've created an event receiver to handle the ItemAdded and ItemUpdating events on a document library in SharePoint 2010.
I've encountered a problem where when I add a document to the library (e.g. by saving it back from Word) the ItemAdded method is correctly called however this is then followed by two calls to ItemUpdating. I have removed all code from my handlers to ensure that it's not something I'm doing inside that is causing the problem. They literally look like:
public override void ItemUpdating(SPItemEventProperties properties)
{
}
public override void ItemAdded(SPItemEventProperties properties)
{
}
Does anyone have a solution to this issue?
Here is my elements.xml file for the event receiver:
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="101">
<Receiver>
<Name>DocumentsEventReceiverItemUpdating</Name>
<Type>ItemUpdating</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>My.Namespace.DocumentsEventReceiver</Class>
<SequenceNumber>10000</SequenceNumber>
<Synchronization>Synchronous</Synchronization>
</Receiver>
<Receiver>
<Name>DocumentsEventReceiverItemAdded</Name>
<Type>ItemAdded</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>My.Namespace.DocumentsEventReceiver</Class>
<SequenceNumber>10000</SequenceNumber>
<Synchronization>Synchronous</Synchronization>
</Receiver>
</Receivers>
</Elements>

Problem is that In Document library event handlers during Item Updating also checking that Document is it in Check In mode or Check Out. That's why it is called twice.
You should put your code in
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
if (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null && properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] != null)
{
//do stuff
}
}
For further details Here is good article for describe whole situation of the Document's Events.

Related

Disable copy/paste on Xamarin forms input field i.e. Entry

I am working on disabling copy/paste option menus on xamarin forms Entry, I am able to disable copy option using IsPassword=true attribute but this attribute also converts the normal input field to password field, which is not a requirement.
<Entry IsPassword="true" Placeholder="Password" TextColor="Green" BackgroundColor="#2c3e50" />
Thanks in advance.
This has to do with how Forms functions. Using iOS as the example here, the CanPerform override referred to in the other answer's Bugzilla issue is using the UIMenuController as the withSender and not the UITextField itself that might otherwise be expected. This is because the EntryRenderer class is a ViewRenderer<TView, TNativeView> type and subsequently is using whatever TNativeView (in this case, the UITextView) has in its CanPerform. Because nothing is going to be overridden by default, one still sees all of the cut/copy/paste options in the UIMenuController.
As a result, there would be a couple options. You could first make the modification where if you don't want copy/paste but are fine with getting rid of everything else, you can use UIMenuController.SharedMenuController.SetMenuVisible(false, false) in a custom renderer inheriting from EntryRenderer. If you look around on SO, there are similar questions where this is a possible route.
Alternatively, you can create a "true" custom renderer inheriting from ViewRenderer<TView, TNativeView> as ViewRenderer<Entry, YourNoCopyPasteUITextFieldClassName>. The class inheriting from UITextField can then override CanPerform as something like follows:
public override bool CanPerform(Selector action, NSObject withSender)
{
if(action.Name == "paste:" || action.Name == "copy:" || action.Name == "cut:")
return false;
return base.CanPerform(action, withSender);
}
This will require more effort because the custom renderer will not have the same behavior as the EntryRenderer, but as Xamarin.Forms is now open source, you could look to it for some ideas as to how the EntryRenderer functions normally. Something similar would likely have to be done for Android.
Edit: For Android, you can probably use this SO answer as a starting point: How to disable copy/paste from/to EditText
Another custom renderer, this time inheriting from ViewRenderer<Entry, EditText>, and create a class inside of it like this (in the most basic form):
class Callback : Java.Lang.Object, ActionMode.ICallback
{
public bool OnActionItemClicked(ActionMode mode, IMenuItem item)
{
return false;
}
public bool OnCreateActionMode(ActionMode mode, IMenu menu)
{
return false;
}
public void OnDestroyActionMode(ActionMode mode)
{
}
public bool OnPrepareActionMode(ActionMode mode, IMenu menu)
{
return false;
}
}
Then, in your OnElementChanged method, you can set the native control and the CustomSelectionActionModeCallback value:
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.CustomSelectionActionModeCallback = new Callback();
}
}
Doing something like the following appears to disable all of the copy/paste/cut functionality on the custom entry as far as the toolbar goes. However, you can still long click to show the paste button, to which I've poked around a bit hadn't found an answer yet beyond setting LongClickable to false. If I do find anything else in that regard, I'd make sure to update this.

with resource delta object, identify file delete or file create

I am using Resource Change Listener to track the changes done to my project. This listener is invoked if I delete, or create or save any changes to a file, in the project. I have the ResourceDelta object. With this, how can I find if the file is being created or is going to be deleted.
Below is my code:
In the activator class of my plugin I have:
IResourceChangeListener listener = new MyResourceChangeListener();
this.workspace.addResourceChangeListener(this.listener);
In the MyResourceChangeListener class I have:
System.out.println(event.getBuildKind());
System.out.println(event.getSource());
System.out.println(event.getType());
if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
System.out.println("this is post change event");
final IResourceDelta delta = event.getDelta();
System.out.println(delta.getFlags());
System.out.println(delta.getKind());
System.out.println(delta.getFlags());
if (delta.getKind() == IResourceDelta.ADDED) {
System.out.println("this is ADD event");
}
if (delta.getKind() == IResourceDelta.CHANGED) {
System.out.println("this is CHANGED event");
}
if ((delta.getFlags() & IResourceDelta.CONTENT) == 0) {
System.out.println("this is CONTENT event");
}
}
Output is always as below, either i create a class, delete a class, or make changes and save a class :
0
org.eclipse.core.internal.resources.Workspace#5f9f1f42
1
this is post change event
0
4
0
this is CHANGED event
this is CONTENT event
How can I differentiate between save, delete or create events.
Test the bitmap returned by IResourceChangeEvent.getType() for PRE_DELETE bit. Register the listener specifically for that event type with IWorkspace.addResourceChangeListener(IResourceChangeListener, int)
This article might be useful, too.

is it possible to call an event receiver from an event receiver with different properties?

I have an event receiver that runs when metadata on a folder (docset) is updated. In the same event receiver, I want to run kick off an event receiver for each document in the folder. What I want to know is: is it possible to call an event receiver using a different SPitemEventProperties than the one given.
e.g.
public override void ItemUpdated (SPItemEventProperties properties) {
// when properties is/contains a folder:
// on each document in the folder
// run ItemUpdated where properties is a/contains a document
}
Is it possible to do this? If so, any ideas?
Short answers: no. There is possibly a really hacky way to do it but no obvious solution.
For my particular situation, it was easier to setup the environment so the document inherits the changing piece of metadata so it's event receiver fires when this column changes.
Hope it helps someone else in the future, or for my own records if no one else reads this.
You can call SPListItem.Update() on each document folder ItemUpdatedER. This run document EventReceiver.
public override void ItemUpdated (SPItemEventProperties properties) {
query all sub items/documents
on each document change your data:
item["customField"] = "update value";
item.Update() //call recursively ItemUpdating/ItemUpdate
}

Unsubscribe from IObservableElementEnumerable.EnumerableChanged doesn't work?

Parts of our UI uses IObservableElementEnumerable.EnumerableChanged in order to update if the user e.g. deletes a domain object from a folder.
When the UI is disposed, we unsubscribe from the event... or so we thought. It turns out that the unsubscribe doesn't have any effect, and our event handler is still called. This caused a number of odd bugs, but also leads to memory leaks.
The only time unsubscription works, is if we store the IObservableElementEnumerable reference instead of calling IObservableElementEnumerableFactory.GetEnumerable(obj) again. But this, in turn, is likely to keep a live reference to the folder object, which will break if the folder itself is deleted by the user.
This is particularly puzzling as the GetEnumerable() documentation clearly states: "It is expected that subsequent calls with the same domain object will yield the same instance of IObservableElementEnumerable." Is this not to be interpreted as a guarantee?
Should there be any reason for unsubscription not working?
The following code replicates the issue on Petrel 2011 (add to a simple plugin with a menu extension, or get the full solution here (DropBox)):
using System;
using System.Linq;
using System.Windows.Forms;
using Slb.Ocean.Core;
using Slb.Ocean.Petrel;
using Slb.Ocean.Petrel.Basics;
using Slb.Ocean.Petrel.UI;
namespace ObservableElementEnumerable
{
public class OEEForm : Form
{
private Droid _droid;
private bool _disposed;
public OEEForm()
{
IInput input = PetrelProject.Inputs;
IIdentifiable selected = input.GetSelected<object>().FirstOrDefault() as IIdentifiable;
if (selected == null)
{
PetrelLogger.InfoOutputWindow("Select a folder first");
return;
}
_droid = selected.Droid;
GetEnumerable().EnumerableChanged += enumerable_EnumerableChanged;
PetrelLogger.InfoOutputWindow("Enumerable subscribed");
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing && !_disposed)
{
GetEnumerable().EnumerableChanged -= enumerable_EnumerableChanged;
PetrelLogger.InfoOutputWindow("Enumerable unsubscribed (?)");
_droid = null;
_disposed = true;
}
}
IObservableElementEnumerable GetEnumerable()
{
if (_disposed)
throw new ObjectDisposedException("OEEForm");
object obj = DataManager.Resolve(_droid);
IObservableElementEnumerableFactory factory = CoreSystem.GetService<IObservableElementEnumerableFactory>(obj);
IObservableElementEnumerable enumerable = factory.GetEnumerable(obj);
return enumerable;
}
void enumerable_EnumerableChanged(object sender, ElementEnumerableChangeEventArgs e)
{
PetrelLogger.InfoOutputWindow("Enumerable changed");
if (_disposed)
PetrelLogger.InfoOutputWindow("... but I am disposed and unsubscribed!");
}
}
public static class Menu1
{
public static void OEEBegin1_ToolClick(object sender, System.EventArgs e)
{
OEEForm f = new OEEForm();
f.Show();
}
}
}
To replicate:
Run Petrel with the plugin
Load a project with a folder with objects
Select the folder
Activate the plugin menu item
With the popup open, delete an object in the folder
Close the Form popping up
Delete an object in the folder
The message log should clearly show that the event handler is still called after the form is disposed.
You already keep a reference to the underlying enumerable by connecting the event. Events are references as well. Just keep a reference to the enumerable and unsubscribe from the same instance as the one you subscribe to.
To deal with the issue of objects that are deleted by the user you need to listen to the delete event.

flex 4: swfloader as2 game, can i catch a customevent that was sent using mx.events.EventDispatcher?

I'm building a flex 4 container for action script 2 flash applications.
I use <mx:SWFLoader> component to load the game.
I know that i can catch events or even custom events from an action script 3 application.
working example for action script 3 (not 2):
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955"
minHeight="600" creationComplete="init()">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import Red5Event;
private function handleRed5Event(e:Red5Event):void {
Alert.show("yay");
}
private function init():void {
this.fileSwf.content.addEventListener(Red5Event.CONTROL_TYPE
,handleRed5Event);
}
]]>
</fx:Script>
<mx:SWFLoader id="fileSwf" source="file.swf" />
</s:Application>
then in the flash application i extend the event class, adding the proper control type and setting bubbles to true, so whenever i dispatch an event, it's probably being catched by the flex application.
I understood that using as2 i can dispatch custom events using the following example:
import mx.events.EventDispatcher;
class Sender {
// these three lines are needed to use EventDispatcher
public var addEventListener:Function;
public var removeEventListener:Function;
public var dispatchEvent:Function;
public function Sender() {
// this line must be in the constructor of the class
EventDispatcher.initialize(this);
// dispatch an event once per second
}
public function sendEvent():Void {
dispatchEvent({type:"xpoControl"});
trace("event sent!");
}
}
can i somehow dispatch an event in action script 2 flash application that the flex 4 container will be able to catch?
thanks!
after a lot of reading.. what i
request may not be possible.
Actually, there are work-arounds. You are correct in that the way you're trying to accomplish this isn't really possible because of security restrictions. However, you could build what I like to call a marshaller-adapter via the LocalConnection class. You will need to have a method in AS2 that uses a localConnection to communicate with the flex 4 side. You will have to use simple types and pass the properties of your Red5Event more generically, but you should be able to accomplish what you need with your custom 'marshaller-adapter'.
Best of luck,
Jeremy