Receiver ListUrl is not working in Event Receivers for particular list? - sharepoint

I trying to debug EventReceiver is not working. It use to work properly. Only thing I changed is added Intraner AAM.
http:// spfoundation/dept/it/Lists/App%20Change%20Request/AllItems.aspx
Above url is the default AAM and list url.
<Receivers ListUrl="Lists/App%20Change%20Request">
<Receiver>
<Name>AppChangeEventReceiverItemAdded</Name>
<Type>ItemAdded</Type>
<Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
<Class>AppChangeRequest.AppChangeEventReceiver.AppChangeEventReceiver</Class>
<SequenceNumber>10000</SequenceNumber>
</Receiver>
public class AppChangeEventReceiver : SPItemEventReceiver
{
/// <summary>
/// An item was added.
/// </summary>
public override void ItemAdded(SPItemEventProperties properties)
{
// base.ItemAdded(properties);
using (SPWeb web = properties.OpenWeb())
{
try
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["Project/Task Status Details"];
......
......
web.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
throw ex;
}
}
}
In visual studio 2012, I was able to debug properly. Now I am not. Anything wrong I did here? Breakpoint not reaching even first line of it. "No symbols have been loaded..."

There are a few things you can try
1- Delete the dll of your project in GAC (C:\Windows\Microsoft.NET\assembly\GAC_MSIL)
2- Deploy project again
3- Control the dll if its the last deployed one.(control the date)
4- From the vs2012
-debug and attach to process (w3wp.exe and OWSTIMER.exe)
if this does not work, restart vs2012 and try steps again.
I hope it will help you!!

It may be a late answer but still. I killed half of a day figuring out very similar issue in my environment. It is really possible that if AAM is set up incorrectly, event receivers won't be fired (although the site is loaded and everithing works well... well, almost everything). In that case you'll probably find an error like this in the event log:
Event receiver threw an exception: System.IO.FileNotFoundException: The Web application at http://xxx.yyy.zzz/sites/aaa could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

Related

sharepoint event receiver never fire

I tried to follow a few examples on how to create sharepoint event receiver. Most examples are fairly straightforward in instructions. So I was able to create a sharepoint event receiver project (for example: item adding or site deleting) in visual studio 2010 and deployed to server and site. I checked the feature and it is activated. I ran some test like deleting a test site or even uploading a file. But the test error messages i put in the code never run. I cannot figure out why the events are never fired.
The following is an example of my code:
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
namespace DeletingSite.EventReceiver1
{
/// <summary>
/// Web Events
/// </summary>
public class EventReceiver1 : SPWebEventReceiver
{
/// <summary>
/// A site is being deleted.
/// </summary>
public override void WebDeleting(SPWebEventProperties properties)
{
base.WebDeleting(properties);
properties.Cancel = true;
properties.ErrorMessage = "You cannot ";
}
}
}
Under Event Receiver item, there would be element.xml file. This file defines how and where your event receiver is attached. Please check if everything is correct within that file.

Force Application Start on Azure Web Role

I have a web role on azure and I would like to force a Application_Start without waiting for the first request.
I managed to set the "Start Automatically" property to true on my site
AutoStart a WCF on Azure WebRole
But the Application_Start is not called until the first request comes.
I don't know exactly if I am missing something important here. The server is a W2008 R2 and the IIS version is 7.5
Thanks!
SOLUTION
I put the solution code here. I hope will help someone. I just added a WebRole.cs and just put that code to perform a ping every 30 seconds. Please netice I'm browsing Service.svc because this is my endpoint, your endpoint could be another one. Notice I'm asking for "Endpoint1". If you have more than one endpoints, you should review that line.
public class WebRole : RoleEntryPoint
{
public override void Run()
{
var localuri = new Uri( string.Format( "http://{0}/Service.svc", RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint ) );
while (true)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(localuri);
request.Method = "GET";
var response = request.GetResponse();
}
catch { }
System.Threading.Thread.Sleep(30000);
}
}
public override bool OnStart()
{
return base.OnStart();
}
}
The IIS will only start when the first request arrives. The workaround is to send an HTTP request to the same VM from within OnStart or your RoleEntryPoint descendant - that's easy using WebRequest or equivalent class.
Jordi, I've recently experienced the same issue.
Based on my test Application_Start() is called ONLY when the 1st request ISS for the WebApp. (if you try to start VS in Debug without it open any page (see options in proj/debug), you will see that Application_Start() won't be called also if you don't run the WebApp in Azure)
I suppose that you need doing somethings when the WebRole start, well put your code in the WebRole.cs ;)
Here you can override OnStart() and OnStop() and put your code that wiil be execuded when the WebRole will start.
I've used this way to run a BakgroundWorker that do some scheduled tasks, independently from IIS.
I hope this help.
Davide.
Note:
1 - if you dont'have a WebRole.cs create it in the root of project and write inside:
public class WebRole : RoleEntryPoint
{
public override bool OnStart()
{
...your code...
return base.OnStart();
}
}
2 - If you need to debug the code mind that you need to run VS in debug with the Azure project that refer to WebApp as a "Run Project", otherwise the WebRole will not be called
You could try putting some code in your WebRole.cs to request some URLs from your website. I've tried that, and it seems to work somewhat. But it's a pain to debug, so I never got it really nailed down.
Another option would be to use IIS Application Initialization. You can't use it on IIS 7.5, but you can get IIS 8 if you upgrade your roles to Windows 2012 (set osFamily="3" in your .cscfg).

Sharepoint 2010 - web.config modification

We have a farm with 2 servers.
I have applied some changes to the web.config on both servers. (I have a specific web application i.e. I have applied the changes in the web.config of my sharepoint web application and NOT the default Sharepoint site or the central admin site)
But it seems that Sharepoint does not take into consideration these changes!
For example I added an assembly reference .... but sharepoint was still throwing an assembly reference exception. i had to add the assembly reference to each and every control.
I have also increased the execution timeout but it still gives timeouts.
This happens in the production environment only.
In the test (where there is a single server) I update the web.config and all changes work.
Is it because I should not be updating the web config. I have noticed there is an SPWebConfigModification class. Do I have to use this ? won't it do the same changes as I would have done manually.
Update ....
I have now used the SPWebConfigModification and also did an iisreset ... but the changes were simply ignored!
Update 2...
Some more details on my web.config updates
I had added an assembly reference under assemmblies sectuion, somethign like this:
This did not work as Sharepoint was still throwing an exception that the assembly cannot be found.
This problem only happens in the production environment. In dev and test, I was also receiving the exception but when I added the assembly reference above, the error disappeared.
Another thing which did not work is the executionTimeout. I have added this to the production environment but sharepoint is still giving timeouts an a long request wich we have. Again, this entry solved the problem in the test and development environments.
I wrote this for this for Sharepoint 2007, I think you need to modify the impersonation (RunWithElevatedPrivileges) but the rest should work:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate() {
try
{
Trace.WriteLine("Try to modify web.config");
SPWebApplication myWebApp = ((SPWeb)properties.Feature.Parent).Site.WebApplication;
WebConfigModifier mod = new WebConfigModifier(myWebApp, OwnerString);
mod.AddModification(
"SafeControl[#Assembly='Elsni.WorldsBestProgram, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e7639c2c71f2f003']",
"configuration/SharePoint/SafeControls",
"<SafeControl Assembly='Elsni.WorldsBestProgram, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e7639c2c71f2f003' Namespace='GFA.UniversalListSyncSolution' TypeName='*' Safe='True' />"
);
mod.AddModification(
"add[#key='PropertiesSiteUrl']",
"configuration/appSettings",
"<add key=\"PropertiesSiteUrl\" value=\"http://iei-developersy/sites/gfaadmin/\" />"
);
mod.Update();
Trace.WriteLine("Done.");
}
catch (Exception ex)
{
Trace.WriteLine("ERROR while activating feature: " + ex.Message);
}
}
});
}
As you have pointed out, you need to make these changes using the SPWebConfigModification class. It is possible to make the changes manually, however, this usually results in random issues caused by replication issues etc ....
The SPWebConfigModification class will ensure the change is stored in the SP database and amend the web.config files on your behalf.

SharePoint 2010 event handler

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

SPSite constructor problem "Operation aborted"

Warning: I'm an asp.net developer taking my first steps in SharePoint.
So, i'm writing a console application that connects to a SharePoint Server 2007 site on the same machine, but it seems that something goes wrong during the call to SPSite() constructor. Here's the simplified code:
using (SPSite siteCollection = new SPSite("http://server/AwesomeSite"))
{
//when i set a breakpoint on this line and inspect the siteCollection object,
//most of it's properties (for example siteCollection.AllWebs.Names) throw an
//exception, which leads me to the conclusion that something went wrong during
//the constructor above.
SPWebCollection site = siteCollection.AllWebs;
SPWeb web = site[""];
SPList list = web.Lists["AwesomeList"]; //exception occurs here
}
The SPException text:
Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
I followed the advice of Sharepoint SPSite and checked that:
The user is a server farm administrator.
The user has Read and Write permissions on the content database.
The user is a site collection administrator.
The user has permissions to access the Windows SharePoint Services site or the SharePoint Server 2007 site through which the code iterates.
And they are all correct. What else could be causing this to happen?
In my experience, the SPSite() constructor is highly dependent on the Alternate Access Mappings configuration of your site. Make sure that the URL you are using in the constructor appears in the list of mappings (e.g., http vs. https, machine vs. FQDN).
You need to get more debug information.
Using Visual Studio
Try setting the debugger to break on all exceptions. Go to Debug, Exceptions and tick Common Language Runtime Exceptions. Then go to Tools, Options, Debugging and untick Enable Just My Code. Finally attach to w3wp.exe. Try running your console application now and you should find that it triggers an exception in w3wp.exe. Check the stack trace and see if that gives you more information.
Using dump files
You could also try working from a crash dump. This is admittedly significantly more hard-core but should give you the detail you are otherwise lacking. The tool ProcDump will can be attached to w3wp.exe (provided the -s switch isn't used) and will create a dump if an unhandled exception occurs. If you have trouble with ProcDump, try ADPlus which does something similar.
From the created dump file, use this KB article to set up WinDbg and get started. There is an example case of how to use WinDbg on Tess Ferrandez's blog (Strategy #2).
Have you tried to run the code with elevated privileges?
Does the IIs have some kind of funky settings regarding authentication? (Try Windows auth. only)
Unfortunately, there are hundreds of ways to generate this error. Just ask Google.
You might consider using SPTraceView to get a better description of the error. Here's a description of the tool and an example working an issue with it.
Good luck!
I have similar (not equals) problem. I've solved it with this piece of code:
using( SPSite site = ConnectToSharepointAsSystem() ) {
// now should be all ok
}
// somewhere in helper class ->
public static SPUserToken GetSystemToken(SPSite site) {
SPUserToken token = null;
bool tempCADE = site.CatchAccessDeniedException;
try {
site.CatchAccessDeniedException = false;
token = site.SystemAccount.UserToken;
}
catch (UnauthorizedAccessException) {
SPSecurity.RunWithElevatedPrivileges(() => {
using (SPSite elevSite = new SPSite(site.ID))
token = elevSite.SystemAccount.UserToken;
});
}
finally {
site.CatchAccessDeniedException = tempCADE;
}
return token;
}
public static Microsoft.SharePoint.SPSite ConnectToSharepoint() {
string urlSharepointSite;
var ret = ConnectToSharepoint(out urlSharepointSite);
return ret;
}
public static Microsoft.SharePoint.SPSite ConnectToSharepoint(out string urlSharepointSite) {
urlSharepointSite = "http://www.domain.org";
var site = new Microsoft.SharePoint.SPSite( urlSharepointSite );
return site;
}
public static Microsoft.SharePoint.SPSite ConnectToSharepointAsSystem() {
string urlSharepointSite;
Microsoft.SharePoint.SPUserToken userToken = null;
using (var tmpSite = CSharepointNastroje.PripojitNaSharepoint( out urlSharepointSite )) {
userToken = GetSystemToken(tmpSite);
}
var site = new Microsoft.SharePoint.SPSite(urlSharepointSite, userToken);
return site;
}

Resources