Answered How to update a document field using Taxonomy term programatically

  • 3 ธันวาคม 2553 5:20
     
     

    Hi,

    I have created a custom column name "Region" (Managed Metada type and associated with Term - Region which has following terms: NA, EU, Asia, Africa)

    The column Region is associated with two custom type: Folder custom type and Document Custom Type.

    When a folder is created, the user select the Region filed using Manage Metadata.

    When a document is created/imported/updated in the folder we need to update the region field automatically based on the folder data.

    I am planning to create Event Receiver to handle such request.  how to get the value from folder type for Region field and populate the same value to the document type. 

    thanks

     

     

ตอบทั้งหมด

  • 3 ธันวาคม 2553 5:58
     
     

    SPItem item = properties.ListItem; // get the list item associated with the added/updated item (use the ED events and not ING event receivers to be able to access the list item)

    SPFile file = item.file; // the file associated with the SPListItem

    SPFolder folder = file.ParentFolder; // the parent folder of the file associated with the SPListItem

     SPItem folderItem = folder.Item; //the list item associated with the folder

    TaxonomyFieldValue managedFieldValueFolder = folderItem[fieldName] as TaxonomyFieldValue;

    document[fieldName] = managedFieldValueFolder; // or just do document[fieldName] =  folderItem[fieldName];

    PS: use itemAdded/Updated and not adding/updating, if not you won't have a created list item


    Florin DUCA
    MCSE 2003 +Sec,MCTS conf/dev WSS3/MOSS, MCITP/MCPD SP 2010, MCPD ASP.Net 3.5, MCTS ISA 2006
    Logica Business Consulting, France
  • 3 ธันวาคม 2553 15:36
     
      มีโค้ด

    Hi thanks for the help

    I applied the code but I am getting the following error at TaxonomyFieldValue managedFieldValueFolder = folderItem["Application Type"] as TaxonomyFieldValue;

    Type 'Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue' in Assembly 'Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' is not marked as serializable.

     

    My code is as below:

    using System;
    using System.Security.Permissions;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Security;
    using Microsoft.SharePoint.Utilities;
    using Microsoft.SharePoint.Workflow;
    using Microsoft.SharePoint.Taxonomy;
    
    namespace EventReceiverProjectVC.EventReceiver1
    {
     /// <summary>
     /// List Item Events
     /// </summary>
     public class EventReceiver1 : SPItemEventReceiver
     {
      /// <summary>
      /// An item was added.
      /// </summary>
      /// 
      public override void ItemAdded(SPItemEventProperties properties)
      {
       //base.ItemAdded(properties);
    
       SPItem item = properties.ListItem; 
       
        SPFile file = properties.ListItem.File; // the file associated with the SPListItem
    
       SPFolder folder = file.ParentFolder; // the parent folder of the file associated with the SPListItem
    
       SPItem folderItem = folder.Item; //the list item associated with the folder
    
       TaxonomyFieldValue managedFieldValueFolder = folderItem["Application Type"] as TaxonomyFieldValue;
       System.Diagnostics.Trace.WriteLine("Debug Application type " + managedFieldValueFolder.ToString());
    
      }
     }
    }
  • 3 ธันวาคม 2553 15:43
     
     

    Did you also try : document[fieldName] =  folderItem[fieldName];

    I did not test the code, tell me if the previous line corrects the issue if not I'll do a small test.


    Florin DUCA
    MCSE 2003 +Sec,MCTS conf/dev WSS3/MOSS, MCITP/MCPD SP 2010, MCPD ASP.Net 3.5, MCTS ISA 2006
    Logica Business Consulting, France
  • 3 ธันวาคม 2553 15:48
     
      มีโค้ด

    Hi Florin,

    I tried and get the same error : " Type 'Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue' in Assembly 'Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' is not marked as serializable."

    at line :    file.Item["Application Type"] = folderItem["Application Type"];

    Code:

          SPItem item = properties.ListItem; 
          
           SPFile file = properties.ListItem.File; // the file associated with the SPListItem
    
          SPFolder folder = file.ParentFolder; // the parent folder of the file associated with the SPListItem
    
          SPItem folderItem = folder.Item; //the list item associated with the folder
          file.Item["Application Type"] = folderItem["Application Type"];
         //  TaxonomyFieldValue managedFieldValueFolder = folderItem["Application Type"] as TaxonomyFieldValue;
         // System.Diagnostics.Trace.WriteLine("Debug Application type " + managedFieldValueFolder.ToString());
  • 3 ธันวาคม 2553 15:55
     
      มีโค้ด

    Hi Florin,

    I have following Setting for my taxonomy:

    Group: Submission
    TermSet: Regulatory
    	Term: Application Type
           	Term:Application Type 1
           	Term:Application Type 2
    
    Thanks
  • 3 ธันวาคม 2553 17:02
     
     

    I did a test and both:

    folderItem["Application Type"] as TaxonomyFieldValue

    and

    document["Application Type"] =  folderItem["Application Type"];

    worked correctly.

    Can you go in debug and see the data types for folderItem["Application Type"] and item["Application Type"].

    PS: recover the listItem only once (SPItem item = properties.ListItem;) and then use it (don't use properties.ListItem again) and you will need to do an update at the end if you want to save the changes.


    Florin DUCA
    MCSE 2003 +Sec,MCTS conf/dev WSS3/MOSS, MCITP/MCPD SP 2010, MCPD ASP.Net 3.5, MCTS ISA 2006
    Logica Business Consulting, France
  • 3 ธันวาคม 2553 17:43
     
      มีโค้ด

    Hi Florin,

    Thanks for taking time and helping me out.  I really appriciate your effort.

    the following is my code which give me an error on SPFile as below:

    Error 1 'Microsoft.SharePoint.SPItem' does not contain a definition for 'File' and no extension method 'File' accepting a first argument of type 'Microsoft.SharePoint.SPItem' could be found (are you missing a using directive or an assembly reference?) C:\Users\shah.b.2\Documents\Visual Studio 2010\Projects\EventReceiverProjectVC\EventReceiverProjectVC\EventReceiver1\EventReceiver1.cs 29 33 EventReceiverProjectVC

     //base.ItemAdded(properties);
       
          
          
         
    
          SPItem item = properties.ListItem;
          SPFile file1 = item.File;
    
        //  SPFile file = properties.ListItem.File; // the file associated with the SPListItem
    
          SPFolder folder = file1.ParentFolder; // the parent folder of the file associated with the SPListItem
    
          SPItem folderItem = folder.Item; //the list item associated with the folder
          TaxonomyFieldValue managedFieldValueFolder = folderItem["Application Type"] as TaxonomyFieldValue;
          

    So I tried the following code:

     //base.ItemAdded(properties);
       
          
          
         
    
          SPItem item = properties.ListItem;
         // SPFile file1 = item.File;
    
         SPFile file = properties.ListItem.File; // the file associated with the SPListItem
    
          SPFolder folder = file.ParentFolder; // the parent folder of the file associated with the SPListItem
    
               

     

     

    SPItem folderItem = folder.Item; //the list item associated with the folder

    System.Diagnostics.Trace.WriteLine("Debug type " + folderItem.Fields["Application Type"].Type.ToString());

    //The above give me "Invalid" as output

    System.Diagnostics.

     

    Trace.WriteLine("Debug type " + folderItem["Application Type"].GetType().ToString());

     

    to give you further detail:

    I am using custom content type for my folder (name: Application Folder - Parent: Folder)

    The "Application Type" is a Term Lablel  - Does it make any difference or it should be acutal Term name - I dont know how to get that

    The "Application Type" - is a custom column created as Managed Metadata type and it is associated with Application Folder type.

    The Custom Column and Custom Type are created at site collection level and I am testing in one of the subsite of the site collection

     

    Thanks

     

  • 3 ธันวาคม 2553 22:53
     
     
    I write the code in my first post directly as text and I made a small mistake. It's SPListItem and not SPItem.
    Florin DUCA
    MCSE 2003 +Sec,MCTS conf/dev WSS3/MOSS, MCITP/MCPD SP 2010, MCPD ASP.Net 3.5, MCTS ISA 2006
    Logica Business Consulting, France
  • 6 ธันวาคม 2553 0:10
     
     

    Hi Florin,

    Hi I change the first line to SPListItem item = properties.Listitem

    still i am getting the following error:

    Type 'Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue' in Assembly 'Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' is not marked as serializable.

    what does serializable has to do with Taxonomy?

    Did i do something wrong while creating taxonomy?

     

  • 6 ธันวาคม 2553 6:09
     
     คำตอบ มีโค้ด

    There are no problems with the taxonomy field, it was created correctly.

    You must also change to SPListItem this line: SPItem folderItem = folder.Item;

    If it's a multichoice managed metadata you should use TaxonomyFieldValueCollection instead of TaxonomyFieldValue (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.taxonomyfieldvaluecollection.aspx).

    Here is what I did test in an event receiver (ItemAdded of type synchronous, deployed using a farm solution):

    public override void ItemAdded(SPItemEventProperties properties)
    {
    base.ItemAdded(properties);
            
      SPListItem item = properties.ListItem;

      SPFile file = item.File; // the file associated with the SPListItem

      SPFolder folder = file.ParentFolder; // the parent folder of the file associated with the SPListItem

      SPListItem folderItem = folder.Item; //the list item associated with the folder

      item["Application Type"] = folderItem["Application Type"];

      this.EventFiringEnabled = false;
      item.Update();
      this.EventFiringEnabled = true;
    }

    Are you using sandbox solution or farm solution?

    Is it a single choice taxonomy field or multi choice taxonoly field?

    Could you try the previous code?

     


    Florin DUCA
    MCSE 2003 +Sec,MCTS conf/dev WSS3/MOSS, MCITP/MCPD SP 2010, MCPD ASP.Net 3.5, MCTS ISA 2006
    Logica Business Consulting, France
    • ทำเครื่องหมายเป็นคำตอบโดย SharePointNewbie 6 ธันวาคม 2553 18:17
    •  
  • 6 ธันวาคม 2553 18:14
     
     

    Hi Florin,

    I created as a Sandbox solution.  The Taxonomy filed is single choice. - I was getting the error i mentioned

    so I created another Farm Solution with following:

    While creating the project I selected following option:

    What type of event receiver do you want:  List Item Events

    What item should be the event source: Document Library

    Handle the following events: An item was added

    Same code applied as u have mentioned - it worked fine...I think as I was using Taxonomy created in Farm I had to deply the solution as farm level solution

    Thanks for all your help