Lists.AddAttachment Method
Adds an attachment to the specified list item in the specified list.
Web Service: ListsWeb Reference: http://<Site>/_vti_bin/Lists.asmx
Syntax
<SoapDocumentMethodAttribute("http://schemas.microsoft.com/sharepoint/soap/AddAttachment", RequestNamespace:="http://schemas.microsoft.com/sharepoint/soap/", ResponseNamespace:="http://schemas.microsoft.com/sharepoint/soap/", Use:=SoapBindingUse.Literal, ParameterStyle:=SoapParameterStyle.Wrapped)> _
Public Function AddAttachment ( _
listName As String, _
listItemID As String, _
fileName As String, _
attachment As Byte() _
) As String
Dim instance As Lists
Dim listName As String
Dim listItemID As String
Dim fileName As String
Dim attachment As Byte()
Dim returnValue As String
returnValue = instance.AddAttachment(listName, listItemID, fileName, attachment)
[SoapDocumentMethodAttribute("http://schemas.microsoft.com/sharepoint/soap/AddAttachment", RequestNamespace="http://schemas.microsoft.com/sharepoint/soap/", ResponseNamespace="http://schemas.microsoft.com/sharepoint/soap/", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Wrapped)]
public string AddAttachment (
string listName,
string listItemID,
string fileName,
byte[] attachment
)
Parameters
- listName
A string that contains either the title or the GUID for the list.
- listItemID
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.
- fileName
A string that contains the name of the file to add as an attachment.
- 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.
Example
The following code example adds a local file as an attachment to a specified list item. The example uses a System.IO.FileStream object to read the source file into a byte array that is passed as a parameter of the AddAttachment method.
This example requires that a using (Visual C#) or Imports (Visual Basic) directive be included for the System.IO namespace. The example also assumes the existence of a text box in the form of the Windows application.
Dim srcUrl As String = textBox1.Text
If Not File.Exists(srcUrl) Then
Throw New ArgumentException(String.Format("{0} does not exist",
srcUrl), "srcUrl")
End If
Dim fStream As FileStream = File.OpenRead(srcUrl)
Dim fileName As String = fStream.Name.Substring(3)
Dim contents(fStream.Length) As Byte
fStream.Read(contents, 0, CInt(fStream.Length))
fStream.Close()
Dim listService As New Web_Reference_Folder.Lists()
listService.Credentials = System.Net.CredentialCache.DefaultCredentials
Try
Dim addAttach As String = listService.AddAttachment("List_Name",
"3", fileName, contents)
MessageBox.Show(addAttach)
Catch ex As System.Web.Services.Protocols.SoapException
MessageBox.Show("Message:" + ControlChars.Lf + ex.Message +
ControlChars.Lf + _
"Detail:" + ControlChars.Lf + ex.Detail.InnerText +
ControlChars.Lf + _
"StackTrace:" + ControlChars.Lf + ex.StackTrace)
End Try
string srcUrl = textBox1.Text;
if (! File.Exists(srcUrl))
{
throw new ArgumentException(String.Format("{0} does not exist",
srcUrl), "srcUrl");
}
FileStream fStream = File.OpenRead(srcUrl);
string fileName = fStream.Name.Substring(3);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
Web_Reference_Folder.Lists listService =
new Web_Reference_Folder.Lists();
listService.Credentials= System.Net.CredentialCache.DefaultCredentials;
try
{
string addAttach = listService.AddAttachment("List_Name", "3",
fileName, contents);
MessageBox.Show(addAttach);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
MessageBox.Show("Message:\n" + ex.Message + "\nDetail:\n" +
ex.Detail.InnerText + "\nStackTrace:\n" + ex.StackTrace);
}