2

Is it possible to create a SharePoint list item that uses the Link to Document content type? I would like to be able to add new links with REST, but JSOM could work too. I was thinking if I could just set the Name and URL fields with REST that would work but I see that the URL field is nested which may make that difficult.

Edit: From what I have seen it looks like I need to create an aspx page that redirects to the correct link besides just setting the name and url fields. Is that possible with REST/JSOM?

Edit2 I have found several blogs about doing this now such as this one https://erroreimprevisto.wordpress.com/2014/05/10/sharepoint-2013-programmatically-add-link-to-a-document-file-in-a-document-library/ but they all use C#. Does anyone know if javascript can be used instead?

2 Answers 2

4

I eventually found out a way to do this. Uploading an aspx file that redirects to the correct document does the trick. I include the aspx file in the JavaScript code because this makes it easy to modify the url to point to different files. The aspx file automatically populates the url and description fields so making another call to set those is not necessary. Here is the code:

function AddLink(){
    var fileName = "test.docx";

    var vardata = '<%@ Page language="C#" %>'+
    "<%@ Assembly Name='Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral,   PublicKeyToken=71e9bce111e9429c' %>"+
    "<%@ Register TagPrefix='SharePoint' Namespace='Microsoft.SharePoint.WebControls' Assembly='Microsoft.SharePoint' %>"+
    "<%@ Import Namespace='System.IO' %>"+
    "<%@ Import Namespace='Microsoft.SharePoint' %>"+
    "<%@ Import Namespace='Microsoft.SharePoint.Utilities' %>"+
    "<%@ Import Namespace='Microsoft.SharePoint.WebControls' %>"+
    '<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">'+
    '<head>'+
    "<meta name='WebPartPageExpansion' content='full' /> <meta name='progid' content='SharePoint.Link' />"+ 
    '<!--[if gte mso 9]><SharePoint:CTFieldRefs runat=server Prefix="mso:" FieldList="FileLeafRef,URL,IconOverlay"><xml>'+
    '<mso:CustomDocumentProperties>'+
    '<mso:ContentTypeId msdt:dt="string">0x01010A00EDD388FD5D86CB41BC0CFDC058F11820</mso:ContentTypeId>'+
    '<mso:IconOverlay msdt:dt="string">|docx?d=wb030a1c46dee4fd6ac9e319218f7b63b|linkoverlay.gif</mso:IconOverlay>'+
    '<mso:URL msdt:dt="string">https://sharepointsite.com/sites/TeamSite/Shared%20Documents/'+fileName+', https://sharepointsite.com/sites/TeamSite/Shared%20Documents/'+fileName+'</mso:URL>'+
    '</mso:CustomDocumentProperties>'+
    '</xml></SharePoint:CTFieldRefs><![endif]-->'+
    '</head>'+
    '<body>'+
    "<form id='Form1' runat='server'>"+
    "<SharePoint:UrlRedirector id='Redirector1' runat='server' />"+
    '</form>'+
    '</body>'+
    '</html>';

    var uspURL = "https://sharepointsite.com/sites/TeamSite/_api/web/getfolderbyserverrelativeurl('/sites//CustomTasks')/files" +
                        "/add(overwrite=true, url='Testing.aspx')";

    $.ajax({
        url: uspURL,
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: vardata,
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            var oitemid = data.d.ID
            alert('Sucess');
        },
        error: function (jqxr, errorCode, errorThrown) {
            alert(jqxr.responseText);
        }
    });
}

1

You can update/add link value using REST as shown below, here 'XYZ' column is of HyperLink type.

var itemType = "SP.Data.URListListItem";

var vardata = {
            '__metadata': { 'type': itemType },
'XYZ': {
                '__metadata': { 'type': 'SP.FieldUrlValue' },
                'Description': 'Google',
                'Url': 'http://www.google.com'
       }
              };

var uspURL = "http://yoursiteurl/_api/web/lists/getbytitle('URList')/items";

        $.ajax({
            url: uspURL,
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(vardata),
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: function (data) {
                var oitemid = data.d.ID
               alert('Sucess');
            },
            error: function (jqxr, errorCode, errorThrown) {

                alert(jqxr.responseText);
            }
        });

Happy Coding

1
  • 1
    I am sure this would work well in a regular SharePoint list. However, the links to documents are in a Document Library so when I ran this code to try to add the link I got the error "To add an item to a document library, use SPFileCollection.Add()". I think this is because I'm not actually creating an aspx page like the out of the box link to document does.
    – James11
    Commented Nov 17, 2015 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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