I found the link below kind of useful, and it works with my sample code below, but the problem is, this is for an existing item... I want to know how to do it if the item has not been created yet in a custom new items html form. In this scenario there is no itemID to pass to the dialog (as per the code below), this would be the same even with standard new item form, the attachment button, and dialog out of the box from Microsoft, so how do we handle this? How does Microsoft handle this?
Reason I don't want to create the itme first is that a user may start a form then changes there mind and just closes the form, if we create an item to attach to then you have a new item that is left in the list, further, workflows would fire as well cause new item was created.
I could use the HTML FileInput Control and rest API, but was hoping to use the SharePoint Dialog Framework and understand how to replicate what MS is already doing.
Perhaps this is done with just adding it to a hidden folder I don't know about and then moving the item after the submit button is clicked, only thing I could think of but then how does that folder get cleaned up, and what would the move code look like.
PS I am currently using SharePoint 2013 Enterprise on-prem
Thanks in advance for the help and suggestions. SG.
Link reference:
Using Sharepoint client API to upload file as attachment in javascript
My code (Basically from link above but tested in my environment):
HTML File:
<!--Your Custom CSS reference-->
<link rel="stylesheet" type="text/css" href="/SiteAssets/CSS/CSS_File.css">
<link rel="stylesheet" type="text/css" href="/SiteAssets/CSS/jquery.ui.css">
<!--JQuery reference-->
<script type="text/javascript" src="/SiteAssets/JQuery/jquery.min.js"></script>
<script type="text/javascript" src="/SiteAssets/JQuery/jquery.ui.js"></script>
<!--SharePoint CSOM references-->
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.core.js"></script>
<script type="text/JavaScript" src="/_layouts/15/clienttemplates.js"></script>
<script type="text/JavaScript" src="/_layouts/15/clientforms.js"></script>
<script type="text/JavaScript" src="/_layouts/15/clientpeoplepicker.js"></script>
<script type="text/JavaScript" src="/_layouts/15/autofill.js"></script>
<!--Your Custom JS reference-->
<script type="text/javascript" src="/SiteAssets/JS/Getting-Started.js"></script>
<div>
<table>
<tr>
<td>
<span>Attachment Test</span><br />
</td>
</tr>
<tr>
<td>
<span>This form is for testing SharePoint Attachments</span><br /><br />
<div id="h_dummi1"><a href="javascript:openpageindialog();">Add attachment using dialog framework</a></div>
<div id="h_dummi2"></div>
</td>
</tr>
</table>
</div>
JS:
$(
function ()
{
//alert("Hello World from Attachment Form");
}
);
function openpageindialog() {
var options = {
url: '/_layouts/Attachfile.aspx?ListId={5505AAAD-B3D8-4D69-8E3D-EE3739D9CB08}&ItemId=1',
title: 'Add Attachment',
width:600,
height: 125,
dialogReturnValueCallback: function(result, fileAdded){
if(result == SP.UI.DialogResult.OK){
v_ItemAttachments = '<p>attachments added</p>';
$("#h_dummi2").html(v_ItemAttachments);
}
}
};
SP.UI.ModalDialog.showModalDialog(options);
}