0

I'm trying to create a custom "Attach File" button in my SharePoint 2013 New Item form. So far this is what I've done:

HTML

 <div onclick="javascript:openpageindialog();" class="btn" style="float: left;">Attach File</div>

JS:

function openpageindialog() {
var options = SP.UI.$create_DialogOptions();
options.title = "Attach File";
options.url = '/_layouts/Attachfile.aspx?ListId={My-ListId}&ItemId=4';
options.autoSize = true;
options.showClose = true;
options.showClose = true;
options.allowMaximize = false;
// options.dialogReturnValueCallback = callback;
SP.UI.ModalDialog.showModalDialog(options);
}

This works just fine, but this the issue. As you notice I addded "ItemId=4" for testing purposes, but this a New Item form; therefore there is not ItemId yet. Is there any work around this? I would hate to create a New Item form and then add the Attach File button to an Edit Form after the Id has been created. Just FYI, unfortunately, I can't use any server side code Thanks!

3
  • SharePoint handles attachments on the New Item form already.
    – hamboy
    Commented Jan 8, 2015 at 1:04
  • I encountered the same problem also, do you have any idea now?
    – user38509
    Commented Jan 9, 2015 at 5:37
  • Can you customize the form using Info-Path forms, that will provide the custom attach file button. Commented Jan 9, 2015 at 6:13

2 Answers 2

2

Create a Custom "New Form" Using SharePoint Designer. You will be able to see all the code for all fields, Now add a new attachment field there and bind it using xslt. Bewlow is the code.

<SharePoint:AttachmentButton runat="server" id="attachmentnew" ControlMode="New" __designer:bind="{ddwrt:DataBind('i','attachmentnew','Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Attachments')}"></SharePoint:AttachmentButton>
5
  • I already created a custom New Form, that's one of the reasons why I'm having the issue.I'll give a try to your suggestion next Monday, and I'll let you know how it goes. Thanks!
    – cubanGuy
    Commented Jan 9, 2015 at 16:16
  • Hey Did you got through your problem ? Commented Jan 12, 2015 at 7:05
  • It doesn't seem to be working. I'm getting: "Uncaught TypeError: Cannot read property 'insertRow' of null" error on the console. I'll keep playing around to see what's going on. Thanks for following up.
    – cubanGuy
    Commented Jan 12, 2015 at 14:58
  • just try copy pasting this code, i works for me fine. Make sure you don't have the id which is used in this code. It should work fine. Commented Jan 13, 2015 at 5:03
  • new attachment field is of what type?
    – Shiva
    Commented Jun 27, 2016 at 12:08
0

You don't need to reinvent it, the code is there and you need to call the function called "UploadAttachment()". For example, you can use a button to cal it:

<input type="button" value="Attach File" onclick="javascript:UploadAttachment()" />

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.