Home > SharePoint, SharePoint 2010, SharePoint Online > SharePoint Sandbox solution – Adding attachment to listitem

SharePoint Sandbox solution – Adding attachment to listitem

There are several limitations with SharePoint online or Sandbox solutions. File uploading is one of the major problem developers face. Recently I was working on a visual Web Part which needs to be packaged as a Sandbox solution. One of the main requirement was the ability to attach documents to custom list items. I tried several options and finally end up with below

For simplicity i am assuming clicking a button should bring up an option for user to select a document and upload it.

User control changes are below

<script type="text/javascript">
    function callback() {
        alert('document uploaded');
    }

    function openPopup() {
        var elem = document.getElementById( '<%= hdnURL.ClientID %>' );
	    var options = {
	        url: elem.value,
	        title: 'Modal Dialog',
	        allowMaximize: false,
	        showClose: true,
	        width: 600,
	        height: 500,
            dialogReturnValueCallback: callback
	    }
	    SP.UI.ModalDialog.showModalDialog(options);
	    return false;
    }
</script>

<input type="button" value="Upload" id="btnUpload" runat="server" onclick="return openPopup();" disabled="disabled"/>
<input type="hidden" runat="server" id="hdnURL"/>

From code behind we need to form the attachment url. Basically we need to fill in the LISTID and ITEMID

string urlFormat = "/_layouts/AttachFile.aspx?ListId={0}&ItemId={1}";
string url = string.Format(urlFormat, "{DC847F22-A785-42E5-9487-45419781B81B}", "1");
hdnURL.Value = url;
btnUpload.Disabled = false;
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment