locked
Does anyone know why properties.ListItem is NULL in ItemAdded Event Handler when i add a document in Windows Explorer

    Question

  •  

    hey everyone,

    When i upload a document in a document library normally, then when i run the ItemAdded Event Handler:

     

            public override void ItemAdded(SPItemEventProperties properties)

     

    And then when I debug my code, properties.ListItem has a value. But when i add documents in Windows Explorer, properties.ListItem was NULL when i debuged it in Visual Studio. 

     

    does anyone know why properties.ListItem is NULL when i copy and paste existing document in windows Explorer View?

    Wednesday, April 30, 2008 1:30 AM

Answers

  • Well, i've been having the same problem but finally, i found a workaround :)

    Actually, the problem can be found when the user tries to do one of the following...

    1. Copying and pasting existing document in SharePoint windows explorer.
    2. Creating new folder in SharePoint windows explorer.
    3. Copying a folder from the pc to the SharePoint using the SharePoint windows explorer.
    To solve the problem of the first case, the user can use the following code...

                SPSite CurrentSite = new SPSite(properties.SiteId);

                SPWeb CurrentWeb = CurrentSite.OpenWeb(properties.RelativeWebUrl);

                SPList CurrentList = CurrentWeb.Lists[properties.ListId];

                SPListItem CurrentListItem;

                CurrentListItem = CurrentList.Items[CurrentList.Items.Count - 1];   // the added file is the last item in items collection 

    To solve the problem of the second and the third case

                CurrentListItem = CurrentList.Folders[CurrentList.Folders.Count - 1];   // the added folder is the last item in folders collection

    hope this helps :)

    Ahmed Amin

    Thursday, October 09, 2008 5:49 PM
  • Hi Alanzhoupsd,

    You can use the following code:-


            CurrentListItem = CurrentList.Items[CurrentList.Items.Count - 1];

            if(properties.AfterUrl != CurrentListItem.Url)
            {
                  CurrentListItem = CurrentList.Folders[CurrentList.Folders.Count - 1];  
            }

    Eventually, we have found another way to get the items and it's much better.


        if (CurrentWeb.GetFile(properties.AfterUrl).Exists)
        {
            
    CurrentListItem = CurrentWeb.GetFile(properties.AfterUrl).Item;
        }

        else if (CurrentWeb.GetFolder(properties.AfterUrl).Exists)
        {    
            
    CurrentListItem = CurrentWeb.GetFolder(properties.AfterUrl).Item;
        }


    GoodLuck :)


                                                                                                                                                   Ahmed Amin

    Wednesday, December 03, 2008 10:27 AM

All replies

  • This is a known issue. No ETA on a fix yet.

    You should be able to work around it by using the ListItemId and spList.GetItemByUniqueId.

     

     

    • Unmarked as answer by Mike Walsh FIN Thursday, July 28, 2011 7:33 AM
    Monday, May 05, 2008 4:02 PM
  • I've been having the same problem!
    I tried with SPList.GetItemById(properties.ListItemId) and that doesn't work either, ListItemId is 0...
    The GetItemByUniqueId requires the GUID of the item.... but where would I get the item GUID?

    Thanks

    Yohan
    Monday, September 29, 2008 5:57 PM
  • Well, i've been having the same problem but finally, i found a workaround :)

    Actually, the problem can be found when the user tries to do one of the following...

    1. Copying and pasting existing document in SharePoint windows explorer.
    2. Creating new folder in SharePoint windows explorer.
    3. Copying a folder from the pc to the SharePoint using the SharePoint windows explorer.
    To solve the problem of the first case, the user can use the following code...

                SPSite CurrentSite = new SPSite(properties.SiteId);

                SPWeb CurrentWeb = CurrentSite.OpenWeb(properties.RelativeWebUrl);

                SPList CurrentList = CurrentWeb.Lists[properties.ListId];

                SPListItem CurrentListItem;

                CurrentListItem = CurrentList.Items[CurrentList.Items.Count - 1];   // the added file is the last item in items collection 

    To solve the problem of the second and the third case

                CurrentListItem = CurrentList.Folders[CurrentList.Folders.Count - 1];   // the added folder is the last item in folders collection

    hope this helps :)

    Ahmed Amin

    Thursday, October 09, 2008 5:49 PM
  • Hi Ahmed!

    Thanks for the suggestion!  Funny thing is, I had found the exact same workaround!  Forgot to post it so thanks!!

    Indeed, hope this helps more than a few people. ;)

    Yohan
    Friday, October 17, 2008 2:28 AM
  • Hi Ahmed,

    But in the method public override void ItemAdded(SPItemEventProperties properties),  how can I know which code snip below should be used.
    Users may copy/ paste a folder or a file.

    CurrentListItem = CurrentList.Items[CurrentList.Items.Count - 1];   // the added file is the last item in items collection 
    CurrentListItem = CurrentList.Folders[CurrentList.Folders.Count - 1];   // the added folder is the last item in folders collection
    Wednesday, December 03, 2008 5:51 AM
  • Hi Alanzhoupsd,

    You can use the following code:-


            CurrentListItem = CurrentList.Items[CurrentList.Items.Count - 1];

            if(properties.AfterUrl != CurrentListItem.Url)
            {
                  CurrentListItem = CurrentList.Folders[CurrentList.Folders.Count - 1];  
            }

    Eventually, we have found another way to get the items and it's much better.


        if (CurrentWeb.GetFile(properties.AfterUrl).Exists)
        {
            
    CurrentListItem = CurrentWeb.GetFile(properties.AfterUrl).Item;
        }

        else if (CurrentWeb.GetFolder(properties.AfterUrl).Exists)
        {    
            
    CurrentListItem = CurrentWeb.GetFolder(properties.AfterUrl).Item;
        }


    GoodLuck :)


                                                                                                                                                   Ahmed Amin

    Wednesday, December 03, 2008 10:27 AM