Get content type of a file

I have a few document libraries that have word documents in them, I have to find out the content types of those documents so that I can server them to a web browser that requests them. How do I do this from the SPListItem that I have in memory

I really need to solve this issue quickly, any help is greatly appreciated!


Answer this question

Get content type of a file

  • ElliotHC

    Does anyone else out there have an ideas about this I may have worded my question wrong from the start, I need the MIME type for a document in one of my document repositories, how do I get it

    Do I use the SPContentType class

    Thanks

  • Grimal

    There's theese fields in the document library that you can look at, I got them from the CAML viewer on the document library.

    <FieldRef Name="File_x0020_Type" StaticName="File_x0020_Type" DisplayName="File Type" Type="Text" />

    <FieldRef Name="HTML_x0020_File_x0020_Type" StaticName="HTML_x0020_File_x0020_Type" DisplayName="HTML File Type" Type="Text" /


  • Jo&amp;#227;o da N&amp;#243;brega

    Thanks for the tips, but that particular property seems to be giving me the file extension of the file, do you know how to retrieve the content type of the file

    Thanks!!

  • omarmhd

    Go to this url:
    http://www.codeproject.com/dotnet/ContentType.asp

    Look under the section title "The Registry to the Rescue- Again" it has code for a method that looks in the registry using the file extension to retrieve the content type.

  • Doga Oztuzun

    can you please give me more information about this How did you get that data and can I get that through the Object Model

    Thanks for the help


  • Jegant

    Also, one more thing, I did loop through all the properties of a document in the Library, and I outputted all of the keys and values, here is what I got (the words key: and value: are what I put in for formatting)

    key: _Comments value:
    key: Keywords value:
    key: Subject value:
    key: _Author value: Mark Macumber
    key: ContentTypeId value: 0x0101009798DADD327F5C45AD5B47B267973C76
    key: vti_approvallevel value:
    key: _Category value:
    key: ContentType value: Document
    key: vti_title value: MICM
    key: vti_parserversion value: 12.0.0.4518
    key: pubic view value: 1
    key: vti_cachedtitle value: MICM
    key: vti_categories value:
    key: vti_cachedcustomprops value: vti_approvallevel vti_categories Subject pubic\ view vti_assignedto Keywords vti_title _Author _Category ContentType _Comments
    key: vti_author value: MARKV\administrator
    key: vti_assignedto value:
    key: vti_modifiedby value: MARKV\administrator


    How can I use this data to server this file (which is a .doc file) to an end user
    Note: these are properties of a SPListItem object, not a SPFile object.

  • DanielN305517

    Sadly, no, I dont think ContentType gives me what I want, but I havent tried the second suggestion yet, Ill let you know how that one goes!

  • LAE2

    Hey thanks, that looks like a great place to find out the info I need.

    Do you know if those registry entries are installed with Windows (if so, which version of windows, etc...) I would hate to write all this code and find out it only works with XP (for example) then install the site on a server 2003 machine.

    Thanks again!

  • gress

    That is an excellent question to which I do not know the answer. If you find out please post...

  • dafle

    There are a few tools out that will give you that data. One is the Sharepoint Solution Generator (an MS product).

    To get the values of these fields from the object model use the syntax:
    spListItemVar["File_x0020_Type"] as string

  • Ingenious

    SPListItem item = ....

    Does item.ContentType give you what you want If not that, then maybe item.File.Item.ContentType

    // Richard


  • Get content type of a file