Set content type based on file extension after uploading into document library

when there are multiple content types associated with a document library, say, report builder report, and excel documents, after you drag and drop a document into the document library, content type will not be detected based on the file extension. Suppose
your default content type is document, and you had added the report build report content type, and you uploaded a .RDL file, the content type for the file will be set to the default document content type.
I get a requirement to update the content type automatically based on the file extension, what follows is my code:
public class setCNTByFileExtItemAdded : SPItemEventReceiver
/// <summary>
/// An item was added.
/// </summary>
public override void ItemAdded(SPItemEventProperties properties)
if (properties.Web.GetFile(properties.AfterUrl).Exists)
SPListItem CurrentListItem = properties.Web.GetFile(properties.AfterUrl).Item;
String FileName = CurrentListItem.Name;
if (FileName.EndsWith(".rdl",StringComparison.OrdinalIgnoreCase))
CurrentListItem.Properties["ContentTypeId"] = "0x010100C3676CDFA2F24E1D949A8BF2B06F6B8B";
CurrentListItem.SystemUpdate(false);
base.ItemAdded(properties);
What follows are references that helped me:
http://stackoverflow.com/questions/7984300/properties-listitem-is-null-in-itemadded-event-while-uploading-documents-using
http://williamvanstrien.blogspot.in/2011/10/read-content-of-uploaded-file-within.html
http://blogs.msdn.com/b/manuj/archive/2009/09/22/itemadded-event-on-document-library-the-file-has-been-modified-by-on-error.aspx
http://support.microsoft.com/kb/2647429

user may have further requirement to detect whether the uploaded .xlsx file contains powerpivot data model, i tried the following code, the caveat is that .xlsx is actually a zip file, i just test whether the uploaded stream contains an entry xl/model/item.data.
public partial class ApplicationPage2 : LayoutsPageBase
protected void Page_Load(object sender, EventArgs e)
if (this.IsPostBack)
if (FileUpload1.PostedFile == null)
return;
string destWeb = DestinationUrl.Url;
string destUrl = destWeb + "/" + FileUpload1.FileName;
using (SPSite site = new SPSite(destWeb))
using (SPWeb web = site.OpenWeb())
SPFile f = web.Files.Add(destUrl, FileUpload1.PostedFile.InputStream);
if (FileUpload1.FileName.EndsWith(".xlsx") )
Stream fs = f.OpenBinaryStream() as Stream;
using (ZipArchive zfs = new ZipArchive(fs, ZipArchiveMode.Read))
if (zfs.GetEntry("xl/model/item.data") == null)
Debug.WriteLine("Does not contain data model");
else
Debug.WriteLine("Contain data model");
The code i posted here is part of an application page in layout folder, the reason why i use application page is described in
http://social.msdn.microsoft.com/Forums/sharepoint/en-US/065d010a-d37e-4da4-9873-172ab2c50d6d/custom-list-new-application-page-with-listfielditerator-and-savebutton?forum=sharepointdevelopment#288a686e-bfbe-41aa-83d0-58a80f0cec07

Similar Messages

  • Setting Content-Type in .jspx file

    Hello,
    My application is not rendered by an HTML Browser.
    I need to set the contentType to application/x-ywidget+xml in my JSPX file.
    The content type returned is always text/xml !!!
    I have tried:
    1. To create a phaseListener and overloading before or after phase in ANY_PHASE
    FacesContext context = FacesContext.getCurrentInstance();
    ExternalContext extContext = context.getExternalContext();
    HttpServletResponse response =
    (HttpServletResponse)extContext.getResponse();
    String contentType = "application/x-ywidget+xml";
    response.setContentType(contentType);
    2. To add a new MimeType extension in web.xml and create my file under this extension
    <servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.xygo</url-pattern>
    </servlet-mapping>
    <mime-mapping>
    <extension>xygo</extension>
    <mime-type>application/x-ywidget+xml</mime-type>
    </mime-mapping>

    Frank,
    Thanks you for you help.
    What i would like to do is to use a JSF library components to build YahooGO pages (Yahoo mobile widgets).
    For this my server needs to return an xml document with the application/x-ywidget+xml content type.
    Regarding you question i don't think that the target device understand the HTML.. on my mobile, it's J2ME application...
    Perhaps you can provide me some advices.
    Regards,
    Fred

  • Urgent: How to set content type on upload depending on target folder?

    Hi,
    i have to set the content type of a file uploaded via SMB depending on the folder the file is placed in. All files placed below a certain root directory have to be of my custom content type. All others should be stored as documents of type "Document". Is there an easy way to accomplish this task? I can't assume any specific file extensions and can therefore not register my custom content type to that extension.
    Any hints are welcome.
    Thanks.

    Hi,
    i have to set the content type of a file uploaded via SMB depending on the folder the file is placed in.All files placed below a certain root directory have to be of my custom content type.
    +++ Use an agent to verify the name and extension using the IfsEvent class and EVENTTYPE_CREATEINSTANCE
    All others should be stored as documents of type "Document". Is there an easy way to accomplish this task?
    +++ Those that aren't your custom content type move to a different folder using the agent.
    I can't assume any specific file extensions and can therefore not register my custom content type to that extension.
    +++I've found that registering content type only really affects things like how a browser knows what kind of file it's downloading/looking at (mime type) or how a renderer knows how to render it.
    Any hints are welcome.
    Thanks.

  • Created column not showing in the Document Library View for a Custom Content Type based on Document

    We have a custom content type based of Document Content Type. The OOTB "Created" column does not show up in the view as well as in the Site Settings -> Columns.
    But it shows up in the Display Form and Edit Form of the Item at the bottom "Created at " by " " and "Modified by" at by ""
    Would anyone know how to make this column appear as part of the view ?
    thanks,
    Harsh

    Hi,
    They should by default be possible to add to a view. The only reason they would disappear from the UI if someone has changed the attribute of the Field to hidden = true. You can verify that with PowerShell and if so you can use PowerShell to revert the setting
    to false.
    $w = Get-SPWeb http://dev13$f = $w.Fields.GetFieldByInternalName("Created")$f.Hidden$f.Hidden = $false$f.Update()

  • External content type based on OData is not crawlable

    Hi all,
    I followed instructions from this msdn article
    How to: Create an external content type from an OData source in SharePoint 2013. I've created an external content type based on OData. I imported created bdcm file to BCS store in central administration. So the ECT is scoped to the farm. I could create
    an External List and the data from OData service was displayed. But I couldn't get the ECT crawlable. When I try to add a new content source in Search Service Application and select Line of Business Data it isn't possible to select External Data Source. I
    tried to add IDEnumerator method and RootFinder property to the ECT in order to get it crawlable without any luck.
    External Content Types based on MS SQL are crawlable fine.
    Dmitry
    Lightning Tools Check
    out our SharePoint tools and web parts |
    Lightning Tools Blog | Мой Блог

    Hi,
    In order to use the ECT for search you have to manually edit it as it's not complete for use with search. The two most important ones to add are:
    ShowInSearchUI
    RootFinder
    I experienced the same issue when creating an OData BCS model for my session at the SPC2012 conference and I think those were the two properties I was missing (in addition to cleaning up some other stuff).
    Take a look at
    MSDN for more information on BDC model properties, and take a look at Search Connector Framework Troubleshooting for SP2010 which shows some samples.
    Let me know if you need more help and I can provide a copy of my OData model (which is against an SP2010 site)
    Thanks,
    Mikael Svenson
    Search Enthusiast - SharePoint MVP/MCT/MCPD - If you find an answer useful, please up-vote it.
    http://techmikael.blogspot.com/
    Author of Working with FAST Search Server 2010 for SharePoint

  • I download itext  for convert jsp to PDF. How to set content type for PDF.

    I download itext for convert jsp to PDF. How to set content type for PDF. I try
    <%@ page contentType = "application/pdf;charset=TIS-620" %>
    , but the page does not PDF.
    Thank.

    PDF files are usually binary files, JSPs are not well-suited for binary content.
    (If you download the result of your JSP you'll see that it is not a valid PDF file; it will have probably a lot of whitespace and linefeeds, that will choke your PDF reader.). The first few characters must be
    "%PDF-" without whitespace.
    You can try using PDF files encoded as text - check if you can use text-encoded PDFs in iText.
    Try using a Servlet instead.

  • Setting content-type response header in BlazeDS

    Per this JIRA ticket, http://bugs.adobe.com/jira/browse/BLZ-428 , there is a suggested fix that includes setting the response content-type header to text/xml.
    The JIRA ticket name is
    pinging endpoint Returns a HTTP: Status 200 in ie8
    and the suggested fix is:
    The problem appears to be completely worked around by setting the following headers in our HTTP responses to Flash:
    Content-Type: text/xml; charset=UTF-8
    Cache-Control: no-cache
    What is the best way to do this is if using BlazeDS?
    I have tried to set the content-type within my data service desitnation class, by accessing the HttpServletResponse from the FlexContext class, however this does not work.  The content-type is still "application/x-amf" in the response.    I am assuming that BlazeDS sets the content-type sometime after my data service desination code is invoked.
    I have also tried to create a servlet filter and set the content-type header both before and after (in a finally block) my data service code is invoked, however neither of these approaches successfully ended up with a content-type of "text/xml" in the response.  My sense is that setting the content-type prior to the data service destination code is too early in the chain and setting in the finally block is too late (per the servlet spec you cannot set the content-type after getWriter() has been called).
    Any ideas are appreciated.
    Also any other ideas on how to solve the general IE8 issue described in the bug above are appreciated.
    I am using...
    BlazeDS version: 3.2.0.3978
    Flex SDK version: 3.4
    App Server: JBoss 4.5
    Java: 1.5_16
    Thanks,
    Kevin

    Hi,
    in the meantime I found a "known issue" that seems to be related to my problem but it is said to be fixed with 10.0 MP1 (and we use 10.3):
    ChangeRequest number: CR316761, CR318708
    Description. The servlet container used to append charset=ISO-8859-1 to the HTTP Header contentType in the response for non-JSP pages with any charset contents. This would result in improper display of multibyte characters.
    This problem has been resolved.
    Maybe the information is wrong. I will examine the CR and may apply them on our 10.3 environment but I would be glad if some oracle/bea engineer would intervene here.
    Cheers
    Thorsten

  • Document Set Content Type Workflow

    I have a document library in Share Point 2013 that is using Document Sets.  I need to set up a workflow under the document content type that would send an e-mail if documents have not been uploaded by a specific due date in the document set.  Any
    Help will be greatly appreciated.

    Hi,
    Based on your description, my understanding is that you want to send an email if there is no document uploaded to the document set by a specific due date in the document set.
    I recommend to create retention policy on the document set content type in the library to start the workflow at the due date.
    The workflow can be created as the image below:
    Firstly, get the max id in the library for using in the loop condition in the workflow:
    You can follow the link below for details:
    http://social.technet.microsoft.com/Forums/en-US/a8a890b5-f5fb-447f-b96f-9f3abdb7e178/forum-faq-sharepoint-2013-extracting-values-from-a-multivalue-enabled-lookup-column-and-merge?forum=sharepointgeneral
    Secondly, we need to loop through the items in the library and then check if there are documents(including folders)
     existed inside the document set which is called text for example.
    Please change the string “/sites/victoria/lib set/” in the Replace Substring in String step to be the relative URL of your library and make sure that there is a space before the document set name in the if condition in the workflow as the image below shows:
    Best regards.
    Thanks
    Victoria Xia
    TechNet Community Support

  • Custom Content type based on "Link to a Document" does not open new folder in a dialog

    Hi,
       We have created a custom Content Type  based on "Link to a Document" (0x01010A) into a document library. When I use the New Document button on the ribbon, the "Link to a document" form appears on the same window, not as a dialog. If we
    use the "link to a document" content type it works fine.
      This issue occurs when creating the content type from the SharePoint UI as well as via XML (provisioning the content type).
       Is it a SharePoint bug or are we missing something?
    Thanks!
    Sergio

    Hi S3rgiones,
    I can reproduce your issue, when create a custom content type based on “Link to a Document” content type, create item using the content type, it will open the NewLink.aspx page in the same window not the modal dialog.
    In this situation, you can try to custom the content type and customize the form page for it, specify it in the content type customization, if this doesn’t work, you may need to custom the content type not inherit from the default link to
    a document, but customize it yourself.
    Thanks,
    Qiao Wei
    TechNet Community Support

  • Content type of .key files

    Dear team ,
    please tell me the "contenttype" of the .pages file format....
    example: .doc....is having the content type as "application/ms-word"
    similarly can u tell me content type of .pages
    Regards,
    spradeepkumar

    hi balusC thanks, it works very well.
    your site http://balusc.xs4all.nl/srv/dev-jep-pdf.html helped me a lot.
    But one point. There is another method : URLConnection.guessContentTypeFromStream(InputStream)but it don't work and i receive as result a null.
    Is there another classes/methods which helps getting the content type of a file? not necessarily from sun. thanks
    bye

  • How to set content type while sending an email?

    Hello,
    I want to set content type text/html while send an email using org.apache.commons.httpclient api. I am using MultipartPostMethod method. and i am set using object of multipartpostmethod like post.
    and post.setRequestHeader("Content-Type","text/html").
    but it wont work.
    so please give me a proper solution.
    example::
    MultipartPostMethod post = new MultipartPostMethod("url");
    post.addParameter("msgbody","html message");
    post.setRequestHeader("Content-Type","text/html");
    Regards,
    Bhavesh Kharwa

    Actually, I'm writing a "HashMap" from servlet to applet. I have used your suggested content type "application/octet-stream" and another "appication/x-java-serialized-object" to try it. But in applet side, when I use (HashMap)in.readObject(), it always gives me a exception as:"java.util.HashMap; IllegalAccessException". Why?
    Thankd.

  • Filter/Redirect based on file extension and path

    Posted this earlier on the NSAPI list, didn't get a response. Can some
    one help?
    Thanks
    Immanuel
    I am attempting to write a filter/redirector based on file extension
    and
    the path of the file - some thing like:
    http:\\www.wombat.com\filetpye1\file1.abc
    gets redirected to server http:\\foo.com\file1.abc
    http:\\www.wombat.com\filetpye2\file2.xyz
    gets redirected to server http:\\bar.com\file2.xyz
    Can some one tell me
    a. Is this possible using NSAPI and conf file changes
    b. Pointer (and source if available) to implment this
    Regards
    Immanuel

    You need to use Client tages and redirect and other SAFs. More info in :
    http://docs.sun.com/source/817-1834-10/crobjsaf.html#wp18074
    http://docs.sun.com/app/docs/doc/820-1643/abvaq?l=en&a=view&q=Client+tags

  • Set content type in respone

    Hi,
    I am using velocity model and to set the content type of response as csv report i am setting content type of response as
    $response.setContentType("application/vnd.ms-excel")
    This stmt opens up open-save download dialog box. But the problem is a new internet browser window also opens up. I want to hide this newly opened browser till i click on open button.
    How i can hide or disable this IE window.

    Ritu,
    U specified only content type , try to specifiy attributes like attachment where u can rid of the IE explorer.
    if u have any other doubts u r most welcome giving me a mail [email protected]
    Thanks.
    Sravan

  • How to Load the data from excel file(Extension is .CSV) into the temp.table

    Hi
    How to Load the data from excel file(Extension is .CSV) into the temporary table of oracle in Forms11g.
    My Forms Version is - Forms [64 Bit] Version 11.1.2.0.0 (Production)
    Kindly Suggest the Solution.
    Regards,
    Sachin

    Hello Sachin,
    You can use the following metalink note:How to Read Data from an EXCEL Spreadsheet into a Form Using Webutil Client_OLE2 (Doc ID 813535.1) and modify it a little bit.
    Instead of copy values into forms you can save them in your temporary table.
    Kind regards,
    Alex
    If someone's helpful or correct please mark it accordingly.

  • Merge/append the contents of a pdf file to an MS Word document in JAVA

    Hi experts,
    I have a requirement to merge/append the contents of a pdf file to an ms word document(Not to be done by including pdf file objects in word). Please suggest some java libraries or ways to do it using java. Any pointers would be appreciated.
    Regards,
    Subin

    Odonnells52 you will still need to install Adobe Acrobat Professional prior to being able to make edits to PDF or utilize Acrobat.   By default the installation files have been saved to your download folder.
    If you continue to face difficulties with the Adobe Download Assistant then please see Troubleshoot Adobe Download Assistant.
    You can also download the installation files directly from Download Acrobat products | Standard, Pro | XI, X if you are unable to locate the downloaded installation files.

Maybe you are looking for

  • SQL Server Express 2008 R2 - Connect to Server Error

    I have recently downloaded and installed SQL Server 2008 R2 on a new laptop (Windows 7).  The install completed successfully and I encountered no issues.  However, when I start SQL Server Management Studio, I am unable to connect to my server instanc

  • Update rules mapping display

    I need to check the update rules and its mapping for objects in production. I clicked the next arrow button to see the mapping but not able to  . It is good enought for me to just see it in display mode. How can i see the mappings in display mode in

  • Open in Default Mode

    This should be easy. I'm opening Breeze presentations in New windows, and they keep defaulting to the Slide Only mode, when I want users to see the video. Anybody know how to make them always open with the video showing? Yep, in the Theme Editor, I'v

  • Not able to login BPC Excel

    Experts, Today I have installed BPC front end Excel and not able to connect with my user ID. In my BI 7.0 system can see BPC 7.0 componenet and Iused BI 7 login details to access BPC excel with BI 7 IP address. Could you please suggest me do I need m

  • When I answer my vonage voip phone I loose my Airport signal.

    Since I've installed Snow Leopard OS, everytime I answer my home phone which is Vonage Voip, I loose my Airport signal on my MacBook. It has to be related to the OS, because my other laptop which is PC works fine with the phone in use and the same Ai