When viewing a document library, if Office Web Apps are enabled, the Url for each Office document goes to the appropriate web viewer rather than the actual link to the document. For instance:


In C# if I get the Url to the document from the SPListItem.File, it gives me the direct Url to the document, not the Office Web Apps url.

How can I get the Office Web Apps version of the Url from code?


  • A quick and dirty solution would be to create that URL in C# using the SPListItem server/_layouts/WordViewer.aspx?id=/<folder>/… url>&DefaultItemOpen=1 – Ryan Erickson Mar 27 '13 at 23:00
  • I had thought about that but I run into a few issues: 1) I need to correctly identify if Office Web Apps are currently available (the product installed, services apps provisioned and online, etc.). And 2) On 2013, they don't actually run on the SharePoint servers. – John Chapman Mar 28 '13 at 13:47

I found the best programmatic solution: The SPUtility.MapToServerFileRedirect function.

In the C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\XML directory are a bunch of ServerFiles XML files that tell SharePoint how to redirect certain types of files, etc. Office Web Apps uses this to redirect links to Office files to the Office Web Apps pages.

So, I can use the following to get the Office Web Apps url (or any other specially handled redirect url):

//file is a SPFile object, GetAbsoluteUrl is an extension method we use to get the full URL for an SPListItem
var url = SPUtility.MapToServerFileRedirect(web, file.Item.GetAbsoluteUrl(), file.ProgID);


For 2013, the following method need to be used instead:


Can we do this for SharePoint List Item Attachments?

If it is possible then please suggest me the desired code. One more thing please define file.Item.GetAboluteUrl() method because it is not working for Attachments.

SPFile file = item.ParentList.ParentWeb.GetFile(item.Attachments.UrlPrefix + item.Attachments[i].ToString());

var url = SPUtility.MapToServerFileRedirect(objWeb, file.Item.Web.Url + "/" + file.Url, file.ProgID);

I am getting an exception. i.e. Object reference not set to an instance of an object.

Thanks in advance. Waiting for your response.

  • I got the solution. It is working for Attachments also. var url = SPUtility.MapToServerFileRedirect(objWeb, objWeb.Url + "/" + file.Url, file.ProgID); – Praveen Jun 23 '14 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.