Display DOC ICON in SharePoint custom page


I m going to explain how to take DOC icons from files in SharePoint library. Display the Doc icons in the custom page when you bind the listitems in the page as grid or table.


Here is the steps to fetch and display DOC icom from files in SharePoint library from files in SharePoint library.

Display DOC ICON in SharePoint custom page

1. Get folder of the file

2. Loadfiles into client object.

3. Load Properties like -> DOC Icon, FileURL,File ID

Folder request = currentWeb.Lists.GetById(new Guid(listID)).GetItemById(requestID).Folder;

clientContext.Load(request, x => x.Files, x => x.Files.Include(p => p.ListItemAllFields[“DocIcon”]), x => x.Files.Include(p => p.ListItemAllFields[“EncodedAbsUrl”]), x => x.Files.Include(p => p.ListItemAllFields.Id));

clientContext.ExecuteQuery();

Get all files from folder location .

foreach (File file in request.Files)

{

//Add file Icon

Image dociconImage = new Image();

var imageUrl = clientContext.Web.MapToIcon(f.Name, String.Empty, Microsoft.SharePoint.Client.Utilities.IconSize.Size16);

clientContext.ExecuteQuery();

dociconImage.ImageUrl = string.Format(“{1}/_layouts/images/{0}”, imageUrl.Value, SecurityHelper.getQueryString(Request, “SPHostUrl”, “AuthorizedSPFarm”, true));

//Add file name with hyperlink

HyperLink docLink = new HyperLink();

docLink.Text = String.Format(“{0}”, f.Name);

docLink.NavigateUrl = f.ListItemAllFields[“EncodedAbsUrl”].ToString();

}

Display DOC ICON in SharePoint custom page

1. Get folder of the file

2. Loadfiles into client object.

3. Load Properties like -> DOC Icon, FileURL,File ID

Folder request = currentWeb.Lists.GetById(new Guid(listID)).GetItemById(requestID).Folder;

clientContext.Load(request, x => x.Files, x => x.Files.Include(p => p.ListItemAllFields[“DocIcon”]), x => x.Files.Include(p => p.ListItemAllFields[“EncodedAbsUrl”]), x => x.Files.Include(p => p.ListItemAllFields.Id));

clientContext.ExecuteQuery();

Get all files from folder location .

foreach (File file in request.Files)

{

//Add file Icon

Image dociconImage = new Image();

var imageUrl = clientContext.Web.MapToIcon(f.Name, String.Empty, Microsoft.SharePoint.Client.Utilities.IconSize.Size16);

clientContext.ExecuteQuery();

dociconImage.ImageUrl = string.Format(“{1}/_layouts/images/{0}”, imageUrl.Value, SecurityHelper.getQueryString(Request, “SPHostUrl”, “AuthorizedSPFarm”, true));

//Add file name with hyperlink

HyperLink docLink = new HyperLink();

docLink.Text = String.Format(“{0}”, f.Name);

docLink.NavigateUrl = f.ListItemAllFields[“EncodedAbsUrl”].ToString();

}

Read SharePoint 2013 tutorials:

Hope this code will help you.

Thanks&Regards,



Jayabharathi

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About jayabharathi

The turning point in the process of growing up is when you discover the core of strength within you that survives all hurt.

View all posts by jayabharathi →