Why isn''t the ItemAdded Event fired?
The situation is like this:
I have 2 lists (picture library) in 2 different sites. For example, list 1 (called PICTURE 1) belongs to site A, and list 2 (called PICTURE 2) belongs to site B. What I want is when a user upload a picture in the list PICTURE 1, that picture is also uploaded automatically to the list PICTURE 2.
To accomplish that requirement, I developed a Console Application to register the Event Handler (ItemAdded) for the list PICTURE 1. Everything works fine, and I used the System Account to test and saw that the picture is also uploaded to the list PICTURE 2.
However, when I try to login with another account which is not in Administrator Group, it seams that the ItemAdded Event is not fired. I know that because i tried to write out some text to a Log File at beginning of the ItemAdded method. For the System Account, I got the disired text in the Log file, but for the other account, I didn't see any text.
Can anybody help me to find out the solution for this problem? I have just worked on Sharepoint for 2 months.
Thanks!!!
Tannt
[1679 byte] By [
tannt] at [2008-1-9]
Ok ... a few things to check
You mentioned a Console Application. I assume this was used to associate your custom Event Receiver class library with the List ?
Have you tried debugging your Event Receiver class and stepped through the code?
The "Item Added" event should fire every time an item is added. It doesn't make any difference who added the item or how it was added, so the user who is logged in should be irrelevant.
If you have an event receiver then it will execute under the system account, so security permissions shouldn't be a problem. However, just to eliminate anything like that, have you made sure that the "non administrator" account you are using has contributor permissions to BOTH Site A and Site B (and hence Picture 1 and Picture 2)?
regards
MKeeper
Thank you for your very quick reply.
I also created a Class override the ItemAdded method, compiled and installed it to GAC.
I used Console Application to register ItemAdded Event Handler for the List PICTURE 1.
Here is my code to register ItemAdded Event handler.
try
{
Console.WriteLine("Start to register list event receiver..."); SPSite spSrcSite = new SPSite("http://srv:2000/it/");
SPList hoiheList = spSrcSite.OpenWeb().Lists["PICTURE 1"];
hoiheList.EventReceivers.Add(SPEventReceiverType.ItemAdded, "AddListEvent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=445d7d6843983eb5", "AddListEvent.AddListEvent");
Console.WriteLine("Sucessfully registered list event receiver"); Console.ReadLine(); }
catch (Exception err) {
Console.WriteLine(err.ToString()); Console.ReadLine(); }
Logically, I also think Security Permissions isn't a problem. But when I add the that "Non Administrator" Account to Administrator group, everything works fine. It means the pictuer wa uploaded to the list PICTURE 2. That was very strange.
There is another thing. When i use the System Account to run this Console Application, It was ok. But when i used a "non administrator" account it announce that http://srv:2000/it is not found.
Thanks!!!
tannt
tannt wrote: |
| There is another thing. When i use the System Account to run this Console Application, It was ok. But when i used a "non administrator" account it announce that http://srv:2000/it is not found. | |
Administrator accounts in SharePoint are automatically made Administrators in SharePoint Central Administration.
Now, when you access your Site Collection (SPSite) in code then you need to be running as an account which has administration access to that site collection. I'm guessing that you haven't added the other account to SharePoint manually?
If the account that you are using to run your console application doesn't have admin permissions to sharepoint, then it won't be allowed to access the Site Collection. frustratingly you get a "not found" error instead of "access denied".
NOTE - you will find you get the same error message using the STSADM command line tool, if you are trying to run as an account that doesn't have Central Administration priveledges.
I'll answer your other problem in a separate post.
tannt wrote: |
| Logically, I also think Security Permissions isn't a problem. But when I add the that "Non Administrator" Account to Administrator group, everything works fine. It means the pictuer wa uploaded to the list PICTURE 2. That was very strange. | |
Follow the following steps:
-
Open up your Event Receiver Project.
-
Place a breakpoint at the first code line of the "Item Added" method.
-
Rebuild the project.*
-
Drop the assembly into the GAC (overwriting the one there).*
-
Reset IIS*
-
Refresh your SharePoint page.
-
In visual studio, go "Debug --> Attach to Process --> w3p.exe --> Attach"
This will put your project in debugging mode.
Go to your SharePoint List and try to add an item. You should find that your breakpoint hits and you can step-through your code .. exactly like any other .Net application.
Try running this under different user accounts and see what happens.
I would also recommend putting in a Try / Catch statement and looking at the Exception that gets thrown (if any?)
Let me know how you get on.
* NOTE
You may notice that your breakpoints are not active and that it says "Symbols not loaded".
This will go away when your event fires (as the assembly is loaded into memory by sharepoint).
If it doesn't then it means that either:
* your event didn't fire
or
* you have made changes to your code and need to follow steps 3 --> 7 again.
Basically .. if your code doesn't exactly match the DLL then it won't "load symbols" and you won't be able to step-through your code.
PS - "Detach" don't "Stop"
When you are attached to a process do NOT hit the stop button or "stop" the process in any way.
This will terminate the running process (in this example .. it will terminate the SharePoint Web application in the same way that IISRESET would).
Instead click "Debug --> Detach All".
This is much more graceful where Visual Studio basically excuses itself, and quietly leaves the process without taking the sharepoint application down with it.
Thank you for your very clear instruction, Mkeeper!
I followed those steps to put my project in the Debugging mode. However, I couldn't step-through the code. It says that "Symbols not loaded" like what you discribed.
According to your NOTE above, there are two posibilities ( my event didn't fire, i made changes in my code). But, I used the System Account to upload picture in List PICTURE 1, and the picture is also uploaded to the list PICTURE 2. It means that the Event fired. I didn't make change to my code as well. To make sure, I go back to the step 3 -> 7. Actually, the symbols are still not loaded.
I will paste the code I write in the Event Receiver class for you to take a look.
string sourceList; string sourceURL; string destList; string destURL;
SPSite sourceSite, destSite;
SPListItemCollection sourceListItems, destListItems;
SPFolder sourceFolder, destFolder;
SPListItem sourceItem, destItem;
SPFile sourceFile;
SPFileCollection destFiles;
StreamWriter
sw; sw =
File.AppendText("C:\\LogFile.txt"); sw.WriteLine("Write some text to see whether the Event Item Added is fired or not");
try
{
sourceURL =
http://SRV:2000/it/; //(site A) destURL = "
http://SRV:2000/"; //(site B)
sourceSite = new SPSite(sourceURL);
destSite = new SPSite(destURL);
sourceList =
"PICTURE 1"; destList =
"PICTURE 2"; sourceListItems = sourceSite.OpenWeb().Lists[sourceList].Items;
sourceItem = sourceListItems[sourceListItems.Count - 1];
sourceFolder = sourceSite.OpenWeb().Folders["PICTURE 1"].
destFolder = destSite.OpenWeb().Folders["PICTURE 2"].
sourceFile = sourceFolder.Files[sourceItem["Name"].ToString()];
destFiles = destFolder.Files;
string destFolderURL = destFolder.Url + "/" + sourceFile.Name;
byte[] binFile = sourceFile.OpenBinary();
destFiles.Add(destFolderURL, binFile);
}
catch (Exception ex)
{
sw.WriteLine(ex.ToString());
}
sw.Close();
The idea of the code above is copying the last picture in the Folder of the list PICTURE 1 to the Folder of the list PICTURE 2. I used SPFileCollection.
I think, when we used the System Account to run the Console Application to register the Event Handler for the List, the event ItemAdded will fired whenever there is new Item added into the List. Logically, this action does not depend on which account registered the event handler.
I also gave the "Contribute" permission for the "non administrator" account in two sites A and B. Therefore, I think if this acount has permission to upload new item, the ItemAdded even will be fired.
Those are my thinkings. Anybody has any idea?
Thanks!
tannt.
Ok .. one thing at first.
You don't need to go looking for the "item" that has been added in your "ItemAdded" event.
You will find it is part of the Event Receiver properties.
Code Snippet
public
override void ItemAdded(SPItemEventProperties properties) {
// this represents the item that has been added
SPListItem itemAdded = properties.ListItem;
// this represents the file that has been added
SPFile fileAdded = properties.ListItem.File;
}
Now the second bit.. you said that you are getting "symbols not loaded".
As I said before. This means one of 2 things:
1) Your Event is not firing. We now know for certain that this is NOT the case (if the files are definately being copied).
2) Your in-project code does not match the assembly in the GAC.
It seems that it must be the second case.
You need to make sure that you do not change ANYTHING in your code after you have built the project.
Then drop the dll into the GAC, do an IISRESET, refresh the web page, and then attach your process.
(in that order!).
If you STILL cannot get this working then I suggest creating a new project. Create a simple event handler (equivalent of Hello World) and get it to correctly debug and step-through the code.
I have seen many instances of MOSS associated projects that suddenly "stop working" or behave strangely. Frustratingly a new project build with a copy-paste of the code often solves those problems.
Hope this helps, and let me know how you get on!
MKeeper
Thank you very much, MKeeper!
I created new project and a simple event handler. I got to correctly debug and step-through the code. If you take a look the code I posted last time, you can see that I made a big mistake.
StreamWriter
sw; sw =
File.AppendText("C:\\LogFile.txt");
I put two lines of code at the beginning of the ItemAdded method. For the System Account, it has the permission to create and write to file. So, everything is ok. However, for the "non administrator" accounts, they don't have such permission. Therefore, I has the error right there.
This made me think that the ItemAdded event isn't fired if we use the "non administrator" account.
Again, thank you for your instruction how to debug the code in Event Handler.
tannt