locked
how to get metadata before executing a block of code RRS feed

  • Question

  • I have some code that I am using to create a Document Set when a document is uploaded into a library. The document set is created within the library...I am currently doing this in an event receiver (ItemAdded). The problem i am having is...when the document is uploaded, I want the user to be prompted for some metadata, which will help determine which block of code to be executed and i can't seem to get this done. Is there a way to get the logic to get the metadata before executing the rest of the code?

    public override void ItemAdded(SPItemEventProperties properties)
            {
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        SPWeb currentWeb = properties.Web;  SPListItem listItem = properties.ListItem;
    						SPList myList = listItem.ParentList;
    						SPFolder mylibrary = myList.RootFolder; 
    						SPList FirstList = currentWeb.Lists["First List"];
    						SPList SecondList = currentWeb.Lists["Second List"];
    						var IsAFolder = IsFolder(listItem);
    						
                                //1. Check to see if object added is a document
                                if (IsAFolder == false)
                                {
                                    this.EventFiringEnabled = false;
    
                                    string EmployeeName = listItem["Employee Name"].ToString();
                                    string EmployeeID = listItem["Employee ID"].ToString();
                                    string DocumentSetName = EmployeeID;
                                    string DocumentType = listItem["Document Type"].ToString();
                                    SPFile fileSource = listItem.File;
                                    string EmailAddress = FirstList.Fields["Email List"].ToString();
                                    string FromEmail = SecondList.Fields["From"].ToString();
                                    string EmailSubject = SecondList.Fields["Subject"].ToString();
                                    string EmailBody = SecondList.Fields["Body"].ToString();
                                    
                                    //2. Evaluate the Document Type
                                    if (DocumentType == "Assessment")
                                    {
    									//execute some block of code
                                    }
                                    else if (DocumentType == "Certification")
                                    {
                                        //execute some other block of code
                                    }
                                    else
                                    {
                                        //bla bla bla.
                                    }                
    
                                
    
    
                            }
                        }
                    });
                }
                catch (Exception ex)
                {
                    SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("My Category", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
                }
                finally
                {
    
                }
            }

    Part of the metadata that is entered by the user is the "Document Type", and I want specific blocks to be executed depending on what the document type entered is. Right now, i can get this to work the way it is designed cause the metadata input screen is popping up at the very end. What am I missing? 

    Thanks for the help


    • Edited by Spawn10 Wednesday, December 4, 2013 9:37 PM
    Wednesday, December 4, 2013 9:33 PM

Answers

  • OK got the problem.

    So the process of uploading a document to a document library is the following:

    - Click the "Add document"-link

    - Choose the file

    - Item is moved to the document library, ItemAdded-Event is firing

    - Dialog for editing metadata opens

    - Item gets updated, ItemUpdated-Event is firing

    So it is absolutely correct that you don't get the metadata in ItemAdded-Event. Maybe you can try to work with the ItemUpdated-Event?

    • Marked as answer by Patrick_Liang Sunday, December 29, 2013 9:17 AM
    Friday, December 6, 2013 10:33 AM

All replies

  • Hey,

    I don't really understand your problem:

    " Is there a way to get the logic to get the metadata before executing the rest of the code?"

    Which metadata do you mean? The list item fields like "Document Type"?

    And what do you mean with "the rest of the code"? The hole event receiver?

    With the code you pasted, you get the metadata before executing the event receiver so I don't get the problem.

    Thursday, December 5, 2013 3:39 PM
  • Thanks for your response Keulovic...I don't think I did a good job explaining my issue. Let me give it another shot...

    So what I am trying to accomplish is this. 1) user uploads a document into a document library 2) the user gets a pop up form and is asked to enter in some metadata, like the "Document Type" 3) then my code determines which type of document was uploaded (the information entered in by the user), and then carries out an action.

    Right now, this is how the code is executing...1) I upload a document, 2) the code then tries to execute (the event receiver code), 3) I finally get the pop up form, asking me to enter in the metadata. 

    So right now, #'s 2 and 3 are not happening in the right order. So currently when i upload a document, it goes through the event receiver code without determining what the "Document Type" is. So no action is actually carried out cause I had not entered in the metadata yet. I want to be able to ask the user for the metadata as the document is being uploaded. Presently it seems like that information is being asked after the it has gone through the document upload. Right now even if i don't put in the metadata, the document is currently still uploaded...I am getting the metadata entry pop up at the end of the operation, and I want to get this pop up as I select the document to upload.

    I hope this helps to explain my issue...please let me know if I can clarify anything, as I know am not good at explaining things sometimes :)

    Thanks for the help.





    • Edited by Spawn10 Thursday, December 5, 2013 4:06 PM
    Thursday, December 5, 2013 3:58 PM
  • OK got the problem.

    So the process of uploading a document to a document library is the following:

    - Click the "Add document"-link

    - Choose the file

    - Item is moved to the document library, ItemAdded-Event is firing

    - Dialog for editing metadata opens

    - Item gets updated, ItemUpdated-Event is firing

    So it is absolutely correct that you don't get the metadata in ItemAdded-Event. Maybe you can try to work with the ItemUpdated-Event?

    • Marked as answer by Patrick_Liang Sunday, December 29, 2013 9:17 AM
    Friday, December 6, 2013 10:33 AM