1

This may be a poorly formed question, but I'm fairly new to sharepoint. What I'm doing is creating a custom page (just basic HTML and JS), and using that page to call SPAPI and SPServices to input different fields, etc into a list. What I dont know how to do, is to upload an attachment. I've explored some of the SPServices and SPAPI methods, but I've failed to encode a binary file, which is the way that both of those libraries need the byte array.

So, I switched gears and I'm trying to steal actual SP code. Does anyone know where the method to upload files (as attachments to a list item) lives, or how I can call it?

the closests I have gotten is calling the code below, stealing it from doing a bit of digging. This brings the modal popup up, from where I can edit an item and add an attachment. Not what I want really, but it gets me to the attachment code in a backwards kind of way.

Any suggestions? working in SP 2010. No Access to custom server side code. IE 8.

<a class="ms-addnew" href="http://sharepoint.fm.rbsgrp.net/teams/spbmc10/_layouts/listform.aspx?PageType=6&amp;ListId={f6f7ee9c-157f-4d79-9295-a7ca83688299}&amp;ID=7" onclick="javascript:NewItem2(event, &quot;http://sharepointsite/teams/blahblah/_layouts/listform.aspx?PageType=6&amp;ListId={f6f7ee9c-478h-4d78-9123-a7ca12345678}&amp;ID=7&quot;);javascript:return false;" target="_blank">Edit item</a>

1 Answer 1

1

These are 2 different operations you are trying:

  1. How to simply upload a file, aka create a new Document in a Document Library (which would also create a new SPListItem at the same time, etc.). There are also multiple ways to upload content, via Client Object Model (exists in 3 flavors - for Javascript, for Managed Code and Silverlight based components) or Server side code (e.g. deploy your web part and build server code). Here an example to upload via CSOM - http://zimmergren.net/technical/sp-2010-uploading-files-using-the-client-om-in-sharepoint-2010 or via Server-Side code http://msdn.microsoft.com/en-us/library/ms454491.aspx

  2. ..or you could Attach a document to a existing ListItem, e.g. to a Task, but you cannot create an attachment to another Document. See here bellow some Server Side Code (no full code, but along these lines)

    using (SPSite site = new SPSite("http://sp.local")) {

        using (SPWeb web = site.OpenWeb())
        {
    
          SPList list = web.Lists["customlist"];
          SPListItem item = list.GetItemById(itemId);
    
          using (FileStream strm = new FileInfo(attachFilePath).Open(FileMode.Open))
          {
            byte[] data = new byte[strm.Length];
            strm.Read(data, 0,(int)strm.Length);
            item.Attachments.Add(Path.GetFileName(attachFilePath), data);
    
          }
    
          item.Update();
    
        }
      }
    
2
  • Thanks for the answer, but it goes against some of my constraints, such as no server side code is available to me. Unfortunately I can't use any C#, which would make my life a lot easier in this scenario. I basically have to either call this attaching method that already lives somewhere in SP, or create my own browser side code to encode the file and pass it through something in the object model. I failed to find a way to do that in javascript, so I turned to trying to dig out the method of attachment that SP uses. so far, I've only been able to dig out the entire modal new/edit pages.
    – dah97765
    Commented Jul 25, 2012 at 21:49
  • Well the Client Object Model should allow you to upload easily the file at least. Haven't found anything on adding an attachment just yet Commented Jul 25, 2012 at 21:58

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.