SharePoint 2010 event handler - sharepoint

I am having a nightmare trying to debug the event receiver of a Sharepoint list.
This is the code of what I am doing:
//Add an event receiver to the list
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, "DatasEvent, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = 6f4db1e1fedbed57", "DatasEvent.DatasEventReceiver");
public override void ItemAdded(SPItemEventProperties properties)
{
try
{
int itemIdSql;
SPListItem item = properties.ListItem;
...
}
catch (SqlException ex)
{
Debug.WriteLine(ex.Message);
}
}
If I place a breakpoint in the event receiver it will not stop.

go to your visual studio Debug -> Attach process -> attach all available w3wp.exe in the list. Now try activating the feature in the web browser. Your breakpoint should be loaded and hit.

I personally prefer adding System.diagnostic.debug.WriteLn() messages to the event receiver code and view them using DebugView on the server. Attaching to worker processes every time is annoying.

First of all ensure that the latest assembly version gets to the GAC. The easiest way for it is to use the "Deploy" option from the context menu of the project or simply hit F5.
Then please let us know where this code is allocated
//Add an event receiver to the list
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, "DatasEvent, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = 6f4db1e1fedbed57", "DatasEvent.DatasEventReceiver");
if it is placed in a feature receiver then make sure that it is activated before you try to attach to w3wp.exe that corresponds to the app pool your target web application corresponds to.

I would try:
Debugger.Launch();
Only in a development environment, otherwise it will try to debug for every request.
I would put it before adding the event receiver.

ItemAdded handles the asynchronous event that occurs after an item is added. The execution is carried out via timer job (and not in current worker process w3wp). So you should attach to OWSTIMER process to debug it.

You should enable debugging of sharepoint in two web.config files.
You should use Debugger.Launch() to stop the runtime.
E.g. After deployment and Activation event fires (in EventReceiver's code there is a Debugger.Launch() command) and VS asks you to debug the code.
There is no need to use "attach to process", but it's another possible way to debug.
this links helped me a lot

Related

Xamarin Android: Can I not create a new object of my MainActivity?

I have a location mocking method in my main activity. Unfortunately, I cant put this method into another class (yet!). So, I need a service, to call this method from my main activity every 5 seconds. So i created a countdown in within a service that, while the app is in the background, should run the method in my MainActivity. But it doesnt.
public void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
Log.Info("2", "CountDown ausgeführt!");
var test = new MainActivity();
test.getMockLocation();
}
This is my code. As you can see, I'm installing a new object of my Main Activity and then ask for the method in within this activity. This does work. Well at least Visual Studio does not complain. If I now debug my app on my phone, nothing happens. I dont get no errors or anything.
Now, when I run this app Step by Step and it reaches this point
"var test = new MainActivity();"
I get "Frame not in Module".
So, it basically crashes as soon as I ask it to install a new object of my Main Activity.
May anybody tell me why this is?
THANKS :)
Unfortunately in Android you cannot create Activities like this, they need to be instantiated by the OS. Also, instantiate a whole Activity only for a method is not ideal, I suggest you to find the way to get that method/function out of that Activity so you can use it anywhere in your program.
Did you create your app via Xamarin Forms? If you did, you can utilize the Xamarin Forms MessagingCenter for background services and then you can call your mock location tasks.
This is the link for a very helpful walk-through and example of MessagingCenter.

Project Server 2010 Event Handler

I am working on OnPublished event handler that will update one custom field of a project based on change in another field.
I am getting an error
Event Handler for event \ProjectPublished\ of type \PS.UpdateProjectStatusChangeDate.EventHandlerUpdateField\ threw an exception: ProjectServerError(s) LastError=CICOCheckedOutToOtherUser Instructions: Pass this into PSClientError constructor to access all error information
This is the code
//loading project data from server
//Every change on this dataset will be updated on the server!
ProjectDataSet projectDs = projectClient.ReadProject(projectId, projectSvc.DataStoreEnum.WorkingStore);
foreach (projectSvc.ProjectDataSet.ProjectRow row in projectDs.Project)
{
if (row.PROJ_SESSION_UID != null)
{
sessionId = row.PROJ_SESSION_UID;
break;
}
}
//send the dataset to the server to update the database
bool validateOnly = false;
Guid jobId = Guid.NewGuid();
projectClient.QueueUpdateProject(jobId, sessionId, projectDs, validateOnly);
Unlike other answers where we are running the code when the project is in checked-in state, we are checking-out and assigning new SessionID.
But when the event handler fires, the project is already is checked-out. So how do I get the SessionID. I think that is where the code is breaking.
Logically that makes sense. While project is checked out that means that somebody can change it at any time and in any way.
So even if your idea works your update can be overwritten by the next save done from Project Pro. Because Project Pro knows nothing about your manipulations.
I don't know anything about your system so let me guess that your users work with Project Pro mostly. In this case you can add your event handler to Application.ProjectBeforePublish msdn link event and update the field from Project Pro. But please keep in mind that your users will be asked to save the project before publishing.
If the solution with Project Pro does not work for you - you can flag published projects somehow and as soon as the project is checked in - do check out, update the field, save and publish the project again.

feature deactivation through command line not firing the SPFeature event reciever

I am deactivating a feature through stsadm tool. I have an event reciever enabled for this action. If i deactivate the feature manually from the site the event is getting triggered but not when running stsadm command.
Please help...
Here is the code Sylvain:
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
Logging.Log(_componentName, "The document library feature has deactivated successfully.");
}
In the code of your event receiver, do you use SPContext.Current (or other Web-Context objects)... ?
If so, your code works on the site, but not out of web-context (like stsadm or command line) because SPContext.Current is null.
Check this point, but there's a good probability that's the problem.
As Sylvain Reverdy pointing out it is more likley your feature receiver to fail than not beeing called. Check out SharePoint logs - all feature activation/deactivation/installation steps are traced there (Location - c:\Program Files\Common Files\Microsoft Shared\web server extensions\14\LOGS, see Where is the default log location for SharePoint/MOSS?).
Make sure you are not using "-force" option of StsAdm command - it makes stsadm to ignore failures during deactivation.

SharePoint and deployment of global.asax code

I want to start logging some custom messages into the ULS from my custom SharePoint code. My code is running inside list item receivers attached to some lists. I'd like to configure this logging mechanism within the application start event handler in global.asax. What's the best-practices way to deploy a SharePoint solution package that modifies global.asax?
I don't know about "best practice", but I would be quite keen on making the edits via code in the feature reciever.
With a line that backs up the file for later restoration.
For logging, we have used Scott hilliers code here to create a trace provider to log with.
Works a treat.
I should clarify that we use a static readonly wrapper for the trace provider
static readonly Log instance = new Log();
that registers itself with the code
SPFarm farm = SPFarm.Local;
Guid traceGuid = farm.TraceSessionGuid;
unit result = NativeMethods.RegisterTraceGuids(ControlCallback, null, ref traceGuid, 0, IntPrt.Zero, null, null, out hTraceReg);
System.Diagnostics.Debug.Assert(result==NativeMethods.ERROR_SUCCESS, "TraceRegister result = " + result.ToString());
Gah!
This then gets instanciated only once and we use the destructor to unregister it.

Registering an event handler for a single list

I have a sharepoint event handler which I want to activate for a single list, not all the lists in the site. How do I go about this?
Got the answer. We need to run this code, maybe in a console app. I still didn't get how to remove the event handler once it has been added though...
string siteUrl = Console.ReadLine();
SPSite site = new SPSite(siteUrl);
SPWeb web = site.OpenWeb();
string listName = Console.ReadLine();
SPList list = web.Lists[listName];
string assemblyName = "Issue.EventHandler, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=89fde668234f6b1d";
string className = "Issue.EventHandler.IssueEventHandler";
list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assemblyName, className);
Just that list or that list in each site ?
I have been testing the code that run when the event happens and I have used a nice little tool from u2u, which allows me to add or remove event handlers per list.
This MSDN article is a nice primer.
Another alternative is the "SharePoint Events Manager".
Events Manager is a Feature for SharePoint that allows administrators to manage events attached to their site's lists and document libraries directly using their browser.
This simple feature enables management of events on lists and document libraries through a new item on list settings menu.
You can view, add and delete events, and even find automatically interesting classes and events from an assembly name.
You can download this feature here, and install it using "stsadm -o addsolution -filename GatWeb.SharePoint.EventsManager.wsp".
This feature is localized in french and english.
I recently gave a talk at our Sharepoint SIG about this very problem. The slides and tools are available here.
You can
write a console app to do this
write a features that uses the code in your console app to deploy to the proper list
use PowerShell
use Brian Wilson's admin tool
You can use this code for removing event handlers:
for (int i = 0; i < olist.EventReceivers.Count; i++) {
olist.EventReceivers[i].Delete();
}
Take a look at the code that comes with the tool from u2u that I posted earlier. It is a convenient tool when you are working with event handlers.

Resources