PDF Icon in SharePoint

This one has been long overdue. There are ton’s of posts and manuals on how to add the PDF Icon to SharePoint. But I wanted an even simpler solution. Litterly one solution!
So here it is!
I have created a simple Farm solution. With just one Farm Feature.
When activated it will add the PDF icon to the infamous DOCICON.xml and upload the PDF Icon itself off course.

And then you will have the PDF Icon for your PDF documents.

Download the solution here.
If you want to index the contents of a PDF file with SharePoint search, don’t forget to install the Adobe PDF iFilter or the Foxit PDF iFilter ( my favorite ).
Install the solution ( from within the SP2010 Powershell window ) and as Farm Admin!

write-host -f Green "Add solution: TamTam.SP2010.PDFIcon.wsp"
Get-ChildItem TamTam.SP2010.PDFIcon.wsp | Add-SPSolution
write-host -f Green "Install solution: TamTam.SP2010.PDFIcon"
Install-SPSolution -Identity "TamTam.SP2010.PDFIcon.wsp" -GacDeployment

Here is the code from the solution to add the PDF Icon xml element to the DOCICON.xml and to all the Search Applications of the Farm. I also added the removal code in the FeatureDeactivating code in the solution itself!

public class PDFIconEventReceiver : SPFeatureReceiver {

    /// <summary>Const of the docicon FileName</summary>
    private const string dociconFile = "TEMPLATE\\XML\\DOCICON.xml";

    /// <summary>
    /// Handle the activation of the feature for the PDF Icon for SP2010,
    /// Adds an entry to the SharePoint DOCICON.xml file for the PDF Icon in this solution
    /// </summary>
    /// <param name="properties"></param>
    public override void FeatureActivated(SPFeatureReceiverProperties properties) {

        string dociconXmlFile = string.Format("{0}\\..\\..\\..\\{1}",
            properties.Feature.Definition.RootDirectory,
            dociconFile);

        XmlDocument docicon = new XmlDocument();
        docicon.Load(dociconXmlFile);

        // locate existing PDF icon
        XmlNode pdfnode = docicon.SelectSingleNode("//DocIcons/ByExtension/Mapping[@Key='pdf']");
        if (pdfnode == null) {
            // no existing PDF icon found, add to the file:
            XmlNode rootextentionNode = docicon.SelectSingleNode("//DocIcons/ByExtension");
            if (rootextentionNode != null) {
                // add new Mapping
                pdfnode = docicon.CreateElement("Mapping");

                XmlAttribute key = docicon.CreateAttribute("Key");
                // extention for this mapping
                key.Value = "pdf";
                pdfnode.Attributes.Append(key);

                XmlAttribute value = docicon.CreateAttribute("Value");
                // url of this solution's pdf icon image
                value.Value = "TamTam.SP2010.PDFIcon/pdficon_small.gif";
                pdfnode.Attributes.Append(value);

                rootextentionNode.AppendChild(pdfnode);
                // save new docicon file to disk
                docicon.Save(dociconXmlFile);
            }
        }
        // add the PDF Icon to the SearchApplications
        foreach (SearchServiceApplication service in SearchService.Service.SearchApplications) {
            // open the Content
            Content content = new Content(service);
            // register the icon as an Extention
            try { content.ExtensionList.Create("pdf"); }
            catch { }
        }
    }
}

6 Responses to “PDF Icon in SharePoint”

  1. Twitter settimana del 2010-11-14 — .mark.net il blog di Marco Trova Says:

    […] Icon in SharePoint « SharePoint Stef (@vanHooijdonk) http://stefvanhooijdonk.com/2010/08/27/pdf-icon-in-sharepoint/ […]

  2. Brad Thurber Says:

    Hi – Is the source for this project available for download? – Thank you, Brad

  3. Brad Thurber Says:

    Will this solution work when one needs to deploy to multiple WFEs (web front ends)? Microsoft seems to indicate on TechNet that a Timer Service deployment is necessary in this case.

  4. Robert Says:

    Nice article!

    You can avoid quite a bit of work by doing 2 things:

    1. Deploy your image using a mapped folder (I use \Template\IMAGES\ since it’s the default for those kind of things.

    2. Use SPUtility class (GetGenericSetupPath) to get the SharePoint path instead of working with great-great-…-grand-parents for directories.

    You can get an other good writeup about it at: http://blogs.msdn.com/b/sgoodyear/archive/2008/06/14/updating-the-docicon-xml-file-with-a-feature.aspx

    Robert / nitront.com

Leave a reply to Twitter settimana del 2010-11-14 — .mark.net il blog di Marco Trova Cancel reply