Often when building an application that links to files stored in SharePoint you run into the problem of getting the file (doc, xls, ppt, etc…) to open in edit mode. By default when you create a hyperlink to a document SharePoint will just get the file and stream it’s contents through the browser to your machine. The file will open in the application associated with the file type/format on your pc. In the case of the Office formats (Word, Excel, PowerPoint, etc…) the application will open the file in a read only mode. To get the file to open in edit mode we have to make sure that we use the same Javascript to ActiveX calls that SharePoint uses to invoke the correct Office application. There are various ways to do this but in a recent project we achieved the desired result by doing the following:
- Creating a web part page page in SharePoint (we were using SharePoint Foundation 2010)
- Adding an HTML form web part to the main zone of the web part page
- Placing the following javascript functions into the Source property of the HTML form web part
- Creating hyperlinks in our existing application that now point to this ASPX page and include a query string argument called OpenDoc
- OpenDoc is set to the URL of the document that you want to open in edit mode
Some things of interest in creating this functionality:
- You need to be able to filter the valid file types (doc, xls, etc…) for opening in edit mode from the invalid file types (pdf, mp3, etc…)
- This utilizes some existing functions in the core.js javascript
- Be careful when debugging as it is easy to trap yourself into a javascript window opening loop
- This code works well in IE 8 (Strangely it works in Firefox on my machine because of Office 2010 see this link for more info)
- This code should work in SharePoint 2007 & 2010 (I have tested only in SPF 2010)
Below is some sample code to get you started on a similar solution….
<html>
<head>
<title></title>
</head>
<script type="text/javascript" src="/_layouts/1033/core.js"></script>
<script type="text/javascript">
//This function reads the query string of the current page
//If the OpenDoc query string argument exists it takes the value (a document url) and opens that file
// using the SharePoint.OpenDocuments function
//If the LinkDoc query string exists it renders a link to re-open the file and to the source library
function openfileineditmode() {
var qs = new Querystring();
var keyField = "OpenDoc";
var keyField2 = "LinkDoc";
var keyValue = qs.get(keyField, '');
var keyValue2 = qs.get(keyField2, '');
var fileExtension = "";
if (keyValue != '') {
var reloadUrl = RemoveUrlKeyValue(keyField, window.location.href);
reloadUrl = reloadUrl + "&" + keyField2 + "=" + keyValue;
fileExtension = keyValue.substring(keyValue.length - 3, keyValue.length);
if (IsEditableType(fileExtension) == true) {
g_varSkipRefreshOnFocus = true;
editDocumentWithProgID2(keyValue, '', 'SharePoint.OpenDocuments', '0', 'http://portal.company.com/home', '0');
}
else {
//Go directly to the file
window.open(keyValue);
}
window.location.href = reloadUrl;
}
else {
if (keyValue2 != '') {
var folderLink = document.getElementById('LibraryLink');
var folderURL = keyValue2.substring(0, keyValue2.lastIndexOf("/") + 1);
folderLink.href = folderURL;
var reloadLink = document.getElementById('OpenDocLink');
reloadLink.href = window.location.href + "&" + keyField + "=" + keyValue2;
}
}
}
// helper function to create a query string array
function Querystring() {
var querystring = location.search.substring(1, location.search.length);
var args = querystring.split('&');
for (var i = 0; i < args.length; i++) {
var pair = args[i].split('=');
temp = unescape(pair[0]).split('+');
name = temp.join(' ');
temp = unescape(pair[1]).split('+');
value = temp.join(' ');
this[name] = value;
}
this.get = Querystring_get;
}
// helper function to get a given query string value from the array
function Querystring_get(strKey, strDefault) {
var value = this[strKey];
if (value == null) {
value = strDefault;
}
return value;
}
// function to evaluate the document type for the correct opening method
function IsEditableType(extension) {
//logic to figure out extension...
}
return result;
}
window.onload = openfileineditmode;
</script>
<body>
<div>
<h2> Launching your file in Edit mode.... </h2>
<p> </p>
<h3>
Source Library: <a id="LibraryLink" href=http://portal.company.com
>Source Library</a> |
<a id="OpenDocLink" href=http://portal.company.com/>Open In Edit Mode Again</a>
</h3>
<br/>
</div>
</body>
</html>
Hi Kyle,
By html form web form, can the CEWP be used instead?
And if so, where in your code do I exactly point to the doc to be opened in Edit mode?
Best regards,
Mark
Mark,
Depending on the version of SharePoint you are using the CEWP can be used. But the HTML Form web part is always safe. What has happened is that Microsoft has restricted the CEWP in some cases (SharePoint 2010 Web Publishing Pages) and as a result it will not accept javascript functions. From what I can tell the HTML Form web part never has these restrictions.
As for your question on where to point to the document I think you are a little confused on how this works. The web part page you create simply acts as a middle-man utility page. Anytime someone accesses this utility page it checks to see if there is a query string value in the URL. If it finds one exists it takes the value (the address of the file) and calls the javascript function needed to open the file in Edit Mode.
So to answer your question you can create HTML links from any page that as long as they point to your web part page and include the OpenDoc query string argument they will then use the middle man utility script I provided to open the file in Edit Mode.
There are many Intranets solutions on the market, however if you are looking for a solid SharePoint based Intranet solution, check out SharePoint Implementeds’ product. I think it’s the best solution out for the price. They seem to have put a lot of thought into usability and filling in gaps that you would not know exsist in sharepoint until you start your implementation.
They offer a turnkey solution which provides a custom Home Site, Department Sites and Project Sites, installation, configuration and training all under $5,000 and even have a source code option.
One thing that I would love to see that they dont have now is a hosted solution
You can get more details at
http://sharepointimplemented.com/AwesomeIntranetGorilla.html#/
Thanks for this. I was unraveling the mystery of how to handle these links, but having you put it all one in place is very helpful.
I can’t be the only one that’s surprised there isn’t a proper, built-in way of handling links like these this late in SharePoint’s lifecycle.
Hi Kyle
I’m a newby and have two questions:
1. question:
Is the Content Editor Web Part in 2007 analog to the HTMLWeb Part in 2010?
2. question:
My situation is following. I have a Content Editor Web Part showing a link to open an Excel file. Now I have added another Content Editor Web Part and put your code into the source. Then I changed the two links to our website.
How would I have to build the link in my Content editor Web Part to open the Excel file in Edit mode ?
Many thanks
Michael