Hi All,
Today i found one very interesting thing. I thought it may help you a lot in many cases.
I was developing webpart which functions like this.
I have document library and in that Document Library i have one column which specially identified a ContactID from other List. When i add that contact i also take the attachment and put it in that document library, so first when i get that ContactID (ID of the contact listItem), i insert a row in that document library with Contact ID in the ContactID column in that document library.
Because requirement was such that i needed to develop one webpart in which i can filter through the contact and see his/her related documents.
This post is not regarding this, but would be one of the scenario to explain what i am now about to tell you.
I also needed the icon with each respective documents. If the document is Docx, i needed to show the docx icon which sharepoint puts in Type column in Document Library. same goes for any files. Doc,Docx,XML,Xls,Xlsx,ppt etc.
so i wonder how sharepoint manages this, i tried to take the ListItem of the document library and tried to see that any of the Field gives the icon path or not. But no, none of the fields returns me the Icon of the path, or even none of the Field tells us that it uses which icon.
I search a bit, then i found that everything is in one Field which is called "DocIcon".
So assume that you have document library called "Shared Documents".
SPDocumentLibrary objDocLib =
(SPDocumentLibrary)SPContext.Current.Web.Lists["Shared Documents"];
Now, Lets say i am taking the First Document,
SPListItem objItem = objDocLib.GetItemById(1); // Make sure you have at least one doc, otherwise this will give Error
Now, Check for this condition,
if (objItem["DocIcon"] == null)
Because if sharepoint images does not have supporting type icon for that document type, lets say any file without any extesnion, this field will return you null. so first check this. now proceed.
string strFileType= Convert.ToString(objItem["DocIcon"]);
This returns you the type (it returns doc,docx,ppt,xls,xlsx,xml etc). So from here you can come to know what is the type of the document.
Now, interesting part comes. you must be wondering that OK, that is fine, but where is the icon path?
Here is a trick that i found,
Check the Images Folder inside C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\IMAGES.
Now Check you will find all the icons which sharepoint uses have .gif extension and all these icons have IC prefix with the extension and plus .gif at last. So have you got any hint? No??? no, problem, here we go. Check this out.
if (objItem["DocIcon"] == null)
{
picture.ImageUrl = @"_layouts\Images\" + "ICgen.gif";
}
else
{
picture.ImageUrl = @"_layouts\Images\" + "IC" + objItem["DocIcon"].ToString() + ".gif";
}
Consider picture as asp Image control, If you have noticed that if sharepoint does not have icon matching with the document type then it puts one white paper kind of icon and the name of that icon is ICgen.gif. so we have done same thing, if sharepoint does not have any supported icon for that document type, it will give objItem["DocIcon"] null. so we have also checked that and places the proper URL, and if we found that then we make combination of "IC" + objItem["DocIcon"].ToString() which is extension + ".gif" which is default extension of any images of sharepoint.
That's it. your job is done.