DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Attachements to a list item using SharePoint Webservices

Posted by PANVEGA on December 16, 2007

In this short post I wanna describe how to add files as an Attachment to a SharePoint list item.

In my previous post Consuming lists.aspx Webservice from SharePoint I showed you how to manage a SharePoint List Webservice. Now I´m gonna add a little bit more tricky thing to the custom cosuming WS method.

Adding an Attachment you need the Add Attchment(..) Method in the lists.asmx Webservice.

public string AddAttachment (
    string listName,
    string listItemID,
    string fileName,
    byte[] attachment


A string that contains either the title or the GUID for the list.
A string that contains the ID of the item to which attachments are added. This value does not correspond to the index of the item within the collection of list items.
A string that contains the name of the file to add as an attachment.
A byte array that contains the file to attach by using base-64 encoding.

Return Value

A string that contains the URL for the attachment, which can subsequently be used to reference the attachment.

Here is an example where I developed a custom ASP.Net Webservice which consumes the Sharepoint lists.aspx Webservice:

[WebMethod(Description = “Set the job appliaction object”)]
public string setApplication(Applicant applicant, string comment, string attache_name, byte[] content)

//here follows the CAML XMLElement method, see my previous post for detailed information


XmlNode return_node = setservice.UpdateListItems(listname, element);

if (return_node.InnerText == “0×00000000″)

if (attache_name != null && content != null)
string return_value = setservice.AddAttachment(listname, return_node.SelectSingleNode(“//@ows_ID”).Value, attache_name, content);



the setApplication(..) function receives an object with all necessary attributes for a job applicant from a ASP.Net Website formular. After adding the job application object successfully to the custom list, go ahead and check the return message. In the return_node you fin the following XML structure:

<Results xmlns="">
   <Result ID="1,Update">
      <z:row ows_ID="4" ows_Title="Title" ows_Modified="2003-06-19 20:31:21"
         ows_Created="2003-06-18 10:15:58" ows_Author="3;#User1_Display_Name"
         ows_Editor="7;#User2_Display_Name" ows_owshiddenversion="3" ows_Attachments="-1"
         ows__ModerationStatus="0" ows_LinkTitleNoMenu="Title" ows_LinkTitle="Title"
         ows_SelectTitle="4" ows_Order="400.000000000000"
         ows_DateColumn="2003-09-04 00:00:00" ows_NumberColumn="791.00000000000000"
         xmlns:z="#RowsetSchema" />

Check the response ErrorCode message. If you receive this 0x00000000 everything was without any errors.

In the next step choose the setservice.AddAttachment(listname, return_node.SelectSingleNode(“//@ows_ID”).Value, attache_name, content); Method. You need the name or GUID from your SharePointlist.

Now comes the tricky thing, in the return node you see the ows_id attribute with the ID =4. This is the new Item ID you created in the previous step. The ID is important to identify the appopriate item in the list.

Using XPATH to catch the item id from the return node. Then you only need the name and the byte code of the file. Thats all you need to add a file to a Sharepoint item.

ASP.NET Web formular example:

The following code shows the ASP.NET web formular, where you have to call the setApplication method and read the FileBytes from an UploadFile for instance.

string attach_name = FileUpload1.PostedFile.FileName.Substring(3);
int stringlenght = (FileUpload1.PostedFile.FileName.Length) – 3;

String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions =  { “.pdf”, “.doc”, “.docx”, “.zip”, “.rar” };

for (int i = 0; i < allowedExtensions.Length; i++)
if (fileExtension == allowedExtensions[i])
string datatype = FileUpload1.PostedFile.FileName.Substring(stringlenght, 3);
byte[] contents = FileUpload1.FileBytes;
Application.Appl applicant = new Application.Appl();
applicant.Titel = DropList.Text;
applicant.Telnr = TelNr.Text.Trim();
applicant.Email = Email.Text.Trim();

Application.Recruting setservice = new Application.Recruting();
setservice.Credentials = System.Net.CredentialCache.DefaultCredentials;

string return_value = setservice.setApplication(applicant, attach_name, contents);
Label1.Text = “<br> Status: <br>” + return_value;


If you don not have a FileUpload Control you can also use the following code reading from a string URL:

FileStream fStream = File.OpenRead(srcUrl);
string fileName = fStream.Name.Substring(3);
byte[] contents = new byte[fStream.Length];

Good luck!

More Informations:


2 Responses to “Attachements to a list item using SharePoint Webservices”

  1. I am trying to write a function in Java to add attachments to a list. I have the UpdateListItem function to add the list entry working. I am having problems with the AddAttachment function. So far, all I get is 400 Bad Request from the server.

    Could you post a sample of the XML that does a successful call to the AddAttachmentmethod on Llists.asmx? If you could show the HTTP request header as well, even better.

    Thanks in advance

  2. Parth said

    I want to add the functionality of adding the attachments using javascrript ,
    may be you can help me out with this..!!?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: