Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create edit link to document in sharepoint

I have a document library in sharepoint storing a word document.

If I click on the link to the document I get a dialog box with "you want to open this file in readonly or editmode etc" and can open it in edit mode, change it, save it directly in word an the changes are saved in the document library.

The link to the file in the document library looks like this:

<a onfocus="OnLink(this)" 
   href="/test/DocLib2/wordtest.docx" 
   onmousedown="return VerifyHref(this,event,'1','SharePoint.OpenDocuments','')"     
   onclick="return DispEx(this,event,'TRUE','FALSE','FALSE',
            'SharePoint.OpenDocuments.3','1', 'SharePoint.OpenDocuments',
            '','','','1','0','0','0x7fffffffffffffff','','')"
>wordtest</a>

How do I create this link in my own web part where I have the name of the file and document library? Without just copying the above code, that wouldn't be a good idea...

Is there some "official" method to achieve this?

like image 831
Marc Avatar asked Aug 16 '12 10:08

Marc


People also ask

How do I edit a document link in SharePoint?

If you're in thumbnail view, click the top right corner of the file, folder or link to select it. to open the information pane. Click the value of the property you wish to edit under the property name, type the new value, and then press Enter on your keyboard. Your change will automatically save.

How do I make a SharePoint file editable by multiple users?

Update Document Library Permissions From the Library Tools menu in the ribbon, select “Library.” On the Document Library Settings page, click “Permission for this document library” under the Permissions and Management heading. Review permissions to ensure that collaborators have editing rights in the document library.


3 Answers

Unfortunately it doesn't seem like there is a better option. But at least you can sort of figure out what the function definition is. The DispEx function is defined in the core.js file (but it's easier to read in the core.debug.js). Both are in 14\Templates\Layouts\1033 directory.

Here is the function definition:

function DispEx(ele, objEvent, fTransformServiceOn, fShouldTransformExtension,
    fTransformHandleUrl, strHtmlTrProgId, iDefaultItemOpen, strProgId, strHtmlType, 
    strServerFileRedirect, strCheckoutUser, strCurrentUser, strRequireCheckout, 
    strCheckedoutTolocal, strPermmask)

Here is my guess on what they mean. Please feel free to add comments to correct any mistakes or omissions:

  • ele - [obj] the element
  • objEvent - [obj] the event object
  • fTransformServiceOn - [bool] (unknown functionality) defaults to True
  • fShouldTransformExtension - [bool] (unknown functionality) defaults to False
  • fTransformHandleUrl - [bool] (unknown functionality) defaults to False
  • strHtmlTrProgId - [string] name of the ActiveXControl to try to load defaults to SharePoint.OpenDocuments.3
  • iDefaultItemOpen - [int] indicator of default to Edit or Read defaults to 1
  • strProgId - [string] name of the ActiveX Control
  • strHtmlType [string] (unknown functionality) defaults to empty
  • strServerFileRedirect - [string] (unknown functionality)
  • strCheckoutUser [string] the ID of the user who has checked out the document
  • strCurrentUser - [string] the ID of the current user
  • strRequireCheckout - [string] indicator whether to force a checkout
  • strCheckedoutTolocal - [string] indicator of whether to use the Local Drafts folder
  • strPermmask - [string] permissions mask for the current user defaults to 0x7fffffffffffffff

There are clearly some inconsistencies in terms of using strings and integers to represent boolean values. It's also strange that your code has 17 parameters but I can only find a function definition with 15 parameters, so I'm not sure what those last two empty strings are for. Some of that is the nature of JavaScript, but it also just looks sloppy on the part of Microsoft.

This doesn't really answer the question, hopefully it helps you or someone else.

like image 94
Peter Jacoby Avatar answered Oct 30 '22 16:10

Peter Jacoby


Chad Schroeder made a blog post on how to construct the javascript function call in C#. Taking into account a couple of settings, like force checkout and open in browser or client for instance.

private string GetFileViewScript(SPFile file)
    {
        string text = SPUtility.MapToControl(SPContext.Current.Web, file.Name, string.Empty);
        string text2 = (file.Item.ParentList.DefaultItemOpen == DefaultItemOpen.Browser) ? "1" : "0";
        SPFieldLookupValue sPFieldLookupValue = file.Item["CheckedOutUserId"] as SPFieldLookupValue;
        string scriptLiteralToEncode = (sPFieldLookupValue == null) ? string.Empty : sPFieldLookupValue.LookupValue;
        string text3 = (SPContext.Current.Web.CurrentUser != null) ? SPContext.Current.Web.CurrentUser.ID.ToString(CultureInfo.InvariantCulture) : string.Empty;
        string text4 = file.Item.ParentList.ForceCheckout ? "1" : "0";

        return string.Format(CultureInfo.InvariantCulture, "return DispEx(this,event,'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')", new object[]
        {
            "TRUE",
            "FALSE",
            "FALSE",
            text,
            text2,
            text,
            string.Empty,
            string.Empty,
            SPHttpUtility.EcmaScriptStringLiteralEncode(scriptLiteralToEncode),
            text3,
            text4,
            (string)file.Item["IsCheckedoutToLocal"],
            (string)file.Item["PermMask"]
        });
    }

Using DispEx in a link to a SharePoint document

like image 23
Steven Avatar answered Oct 30 '22 16:10

Steven


I end up with adding this code

return DispEx(this,event,'TRUE','FALSE','FALSE', 
'SharePoint.OpenDocuments.3','1', 'SharePoint.OpenDocuments','','','',
'1','0','0','0x7fffffffffffffff','','')

to my link tag because I wasn't able to find a better solution.

If there is any, please let me know.

like image 21
Marc Avatar answered Oct 30 '22 14:10

Marc