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

2 Answers 2

up vote 3 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

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.

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

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.