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?