Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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:

http://server/_layouts/WordViewer.aspx?id=/Shared%20Documents/Document.docx&Source=http%3A%2F%2Fserver%2FShared%2520Documents%2FForms%2FAllItems%2Easpx&DefaultItemOpen=1&DefaultItemOpen=1

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?

Thanks!

share|improve this question
    
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
add comment

1 Answer

up vote 2 down vote accepted

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);

EDIT

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

 SPUtility.GetServerFileRedirectForLinking
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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