1

I have a field in Sharepoint 2013 that consists of choices. The choices are a variety of document types like

  • Word
  • Excel
  • Powerpoint

and so forth. The main idea is that on the DIP (Document information panel), when I upload a document I want to check the file extention of that file type and automatically select a suitable value from this choice field. I have an event receiver connected to the "item added" event on a document library and all I need now is to actually choose the value.

  private void UpdateDocumentType(SPListItem item)
    {
        //get document type field by internalname            
        var fields = item.Fields;
        SPFieldChoice docTypeDropDownList = fields.GetFieldByInternalName("DocumentType") as SPFieldChoice;  

        if (docTypeDropDownList != null)
        {

        //get file extension
        var docFileExt = Path.GetExtension(item.Url);            

        if (docFileExt != null && docFileExt.IndexOf("doc", System.StringComparison.Ordinal) >= 0)
          {
             var docType = docTypeDropDownList.Choices.IndexOf("Word");
             //SELECT SUITABLE CHOICE HERE
          }
         }
     }

I am a novice regarding C#, but it seems that whatever I try I am unable to select a value from the choice field. The code above is tested and it connects with the item-added event, finds the file extension and gets the field just fine. Can anybody help me with this? Also - is there a more elegant way to do this other than what I have attempted? Seems like maybe there is a better way to do it instead of an if for each file extention?

2 Answers 2

1

The below code should work.

   item["DocumentType"]="Word";
    item.Update();

But better solution will be to do this operation in a Designer workflow rather than writing an event receiver. In workflows , it will be easy to make changes and saves deployment time and effort. enter image description here

0
1

I would recommend you to not hard code the mapping "file extension" to "application". If you want a solution to easily update the mapping I would create an other list with two fields. On field containing the extension and one field containing the application name. This approach allows easily and fast updating of the mapping on UI.

With this approach you just have to query your mapping list for the extension of the current file and set the choice field value to the value of the application specified for this extension (so you don't need to switch through all possible extensions).

If a little code example is needed i could provide one tomorrow. If needed please let me know (through a comment)

1
  • Thank you for this idea - I made a dictionary with "file extension" on the left side and "application" on the right side. It made the code much more manageable. Commented Nov 4, 2014 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.